article.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import json
  2. from datetime import datetime
  3. from django.contrib.auth.decorators import login_required
  4. from django.core import serializers
  5. from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
  6. from django.http import JsonResponse, HttpResponseRedirect
  7. from django.shortcuts import render
  8. import blog
  9. from blog import models
  10. from system.error.ServerException import ServerException
  11. @login_required(login_url='/login')
  12. def article(request):
  13. page = request.GET.get('page')
  14. search_title = request.GET.get('search_title')
  15. search_content = request.GET.get('search_content')
  16. if page is None:
  17. page = 0
  18. articles = blog.controller.article.get_article(is_paginator=True, page=int(page), search_title=search_title,
  19. search_content=search_content)
  20. return render(request, 'management/article/article.html', context={'articles': articles})
  21. @login_required(login_url='/login')
  22. def draft(request):
  23. page = request.GET.get('page')
  24. search_title = request.GET.get('search_title')
  25. search_content = request.GET.get('search_content')
  26. if page is None:
  27. page = 0
  28. articles = blog.controller.article.get_article(is_paginator=True, page=int(page), search_title=search_title,
  29. search_content=search_content, draft=True)
  30. return render(request, 'management/article/draft.html', context={'articles': articles})
  31. def new(request):
  32. return render(request, 'management/article/new_article.html')
  33. def show_article(request, pk):
  34. article = models.Article.objects.filter(id=int(pk)).values('id', 'title', 'intro', 'category',
  35. 'user', 'created_time', 'type',
  36. 'cover__file_net_path',
  37. 'music__file_net_path', 'status',
  38. 'category__name',
  39. 'user__first_name', 'user_id',
  40. 'user__last_name', 'markdown_text', 'is_top',
  41. 'html_text').first()
  42. from blog.views import get_record_and_tags
  43. result = get_record_and_tags()
  44. return render(request, 'blog.html', context={'article': article, 'records': result['records'],
  45. 'tags': result['tags']})
  46. @login_required(login_url='/')
  47. def to_edit(request, pk):
  48. article = models.Article.objects.filter(id=int(pk)).values('id', 'title', 'intro', 'category',
  49. 'user', 'created_time', 'type',
  50. 'cover__file_net_path',
  51. 'music__file_net_path', 'status',
  52. 'category__name',
  53. 'user__first_name', 'user_id',
  54. 'user__last_name', 'markdown_text', 'html_text',
  55. 'is_top').first()
  56. tags = models.Tags.objects.raw(
  57. 'select * from blog_tags left join blog_article_tags bat on blog_tags.id = bat.tags_id where bat.article_id = {0}'.format(
  58. int(pk)))
  59. tags_str = []
  60. for tag in tags:
  61. tags_str.append(tag.name)
  62. return render(request, 'management/article/edit_article.html',
  63. context={'article': article, 'tags': ','.join(tags_str)})
  64. @login_required(login_url='/')
  65. def edit_article(request):
  66. if request.method == 'POST':
  67. title = request.POST['title']
  68. intro = request.POST['intro']
  69. category = request.POST['category']
  70. html_text = request.POST['html']
  71. markdown_text = request.POST['markdown']
  72. type_ = request.POST['type']
  73. status = request.POST['status']
  74. tags = request.POST['tags']
  75. pk = request.POST['pk']
  76. is_top = request.POST['is_top']
  77. category_obj = models.Category.objects.get(id=category)
  78. article_obj = models.Article.objects.get(id=int(pk))
  79. tags_id = []
  80. for tag in str(tags).split(','):
  81. if tag == '':
  82. break
  83. else:
  84. tag_obj = models.Tags.objects.filter(name=tag).first()
  85. if tag_obj is None:
  86. obj = models.Tags(name=tag)
  87. obj.save()
  88. tags_id.append(obj.pk)
  89. else:
  90. tags_id.append(tag_obj.pk)
  91. if len(tags_id) > 0:
  92. article_obj.tags.set(tags_id)
  93. # models.Article.objects.filter(id=int(pk)).update(title=title, intro=intro, category=category_obj,
  94. # html_text=html_text,
  95. # markdown_text=markdown_text,
  96. # type=type_, status=status,
  97. # tags=str(tags).split(',')
  98. # )
  99. article_obj.title = title
  100. article_obj.intro = intro
  101. article_obj.category = category_obj
  102. article_obj.html_text = html_text
  103. article_obj.markdown_text = markdown_text
  104. article_obj.type = type_
  105. article_obj.status = status
  106. article_obj.is_top = is_top
  107. article_obj.save()
  108. return JsonResponse({"success": True, "message": "修改成功"})
  109. else:
  110. raise ServerException("错误的请求")
  111. @login_required(login_url='/')
  112. def add_article(request):
  113. if request.method == 'POST':
  114. title = request.POST['title']
  115. intro = request.POST['intro']
  116. category = request.POST['category']
  117. html_text = request.POST['html']
  118. markdown_text = request.POST['markdown']
  119. type_ = request.POST['type']
  120. status = request.POST['status']
  121. tags = request.POST['tags']
  122. is_top = request.POST['is_top']
  123. category_obj = models.Category.objects.get(id=category)
  124. article_obj = models.Article.objects.create(title=title, intro=intro, category=category_obj,
  125. html_text=html_text,
  126. markdown_text=markdown_text,
  127. type=type_, status=status, user=request.user,
  128. created_time=datetime.now(),
  129. is_top=is_top
  130. )
  131. tags_id = []
  132. for tag in str(tags).split(','):
  133. if tag == '':
  134. break
  135. else:
  136. tag_obj = models.Tags.objects.filter(name=tag).first()
  137. if tag_obj is None:
  138. obj = models.Tags(name=tag)
  139. obj.save()
  140. tags_id.append(obj.pk)
  141. else:
  142. tags_id.append(tag_obj.pk)
  143. if len(tags_id) > 0:
  144. article_obj.tags.set(tags_id)
  145. return JsonResponse({"success": True, "message": "添加成功"})
  146. else:
  147. raise ServerException("错误的请求")
  148. @login_required(login_url='/')
  149. def add_media(request):
  150. if request.method == 'POST':
  151. title = request.POST['title']
  152. category = request.POST['category']
  153. type_ = request.POST['type']
  154. status = request.POST['status']
  155. tags = request.POST['tags']
  156. cover = request.POST['cover_id']
  157. music_id = request.POST['music_id']
  158. cover_obj = models.FileRecord.objects.get(id=cover)
  159. music_obj = models.FileRecord.objects.get(id=music_id)
  160. category_obj = models.Category.objects.get(id=category)
  161. article_obj = models.Article.objects.create(title=title, category=category_obj,
  162. type=type_, status=status, user=request.user,
  163. created_time=datetime.now(),
  164. cover=cover_obj, music=music_obj
  165. )
  166. if tags != 'null':
  167. article_obj.tags.set(str(tags).split(','))
  168. return JsonResponse({"success": True, "message": "添加成功"})
  169. else:
  170. raise ServerException("错误的请求")
  171. @login_required(login_url='/')
  172. def add_music(request):
  173. if request.method == 'POST':
  174. title = request.POST['title']
  175. category = request.POST['category']
  176. type_ = request.POST['type']
  177. status = request.POST['status']
  178. tags = request.POST['tags']
  179. cover = request.POST['cover_id']
  180. music_id = request.POST['music_id']
  181. cover_obj = models.FileRecord.objects.get(id=cover)
  182. music_obj = models.FileRecord.objects.get(id=music_id)
  183. category_obj = models.Category.objects.get(id=category)
  184. article_obj = models.Article.objects.create(title=title, category=category_obj,
  185. type=type_, status=status, user=request.user,
  186. created_time=datetime.now(),
  187. cover=cover_obj, music=music_obj
  188. )
  189. if tags != 'null':
  190. article_obj.tags.set(str(tags).split(','))
  191. return JsonResponse({"success": True, "message": "添加成功"})
  192. else:
  193. raise ServerException("错误的请求")
  194. def set_tag(articles):
  195. for i in range(len(articles)):
  196. tags = models.Tags.objects.raw(
  197. 'select * from blog_tags left join blog_article_tags bat on blog_tags.id = bat.tags_id where bat.article_id = {0}'.format(
  198. articles[i]['id']))
  199. tags_field = []
  200. for item in tags:
  201. tags_field.append(item.name)
  202. articles[i]['tags'] = tags_field
  203. def get_article(top: int = -1, page: int = -1, is_paginator: bool = False, category: models.Category = None,
  204. tag: models.Tags = None, search_title: str = None, search_content: str = None, date_record: str = None,
  205. draft: bool = False):
  206. if category is None and tag is None and date_record is None and draft == False:
  207. search_dict = dict()
  208. if search_title:
  209. search_dict['title__contains'] = search_title
  210. if search_content:
  211. search_dict['markdown_text__contains'] = search_content
  212. articles = models.Article.objects.filter(**search_dict).order_by('-created_time').values(
  213. 'id', 'title', 'intro', 'category',
  214. 'user', 'created_time', 'type',
  215. 'cover__file_net_path',
  216. 'music__file_net_path', 'status',
  217. 'category__name',
  218. 'user__first_name', 'user_id',
  219. 'user__last_name',
  220. 'is_top', 'html_text')
  221. elif date_record is not None:
  222. date_format = "%Y-%m"
  223. date = datetime.strptime(date_record, date_format)
  224. articles = models.Article.objects.filter(created_time__year=date.year, created_time__month=date.month).order_by(
  225. '-created_time').values('id', 'title',
  226. 'intro',
  227. 'category',
  228. 'user',
  229. 'created_time',
  230. 'type',
  231. 'cover__file_net_path',
  232. 'music__file_net_path',
  233. 'status',
  234. 'category__name',
  235. 'user__first_name',
  236. 'user_id',
  237. 'user__last_name',
  238. 'is_top',
  239. 'html_text').distinct()
  240. elif tag is not None:
  241. articles = models.Article.objects.filter(tags__name=tag.name).order_by('-created_time').values('id', 'title',
  242. 'intro',
  243. 'category',
  244. 'user',
  245. 'created_time',
  246. 'type',
  247. 'cover__file_net_path',
  248. 'music__file_net_path',
  249. 'status',
  250. 'category__name',
  251. 'user__first_name',
  252. 'user_id',
  253. 'user__last_name',
  254. 'tags__name',
  255. 'is_top',
  256. 'html_text')
  257. elif draft:
  258. search_dict = dict()
  259. search_dict['status'] = 0
  260. if search_title:
  261. search_dict['title__contains'] = search_title
  262. if search_content:
  263. search_dict['markdown_text__contains'] = search_content
  264. articles = models.Article.objects.filter(**search_dict).order_by('-created_time').values('id', 'title',
  265. 'intro',
  266. 'category',
  267. 'user',
  268. 'created_time',
  269. 'type',
  270. 'cover__file_net_path',
  271. 'music__file_net_path',
  272. 'status',
  273. 'category__name',
  274. 'user__first_name',
  275. 'user_id',
  276. 'user__last_name',
  277. 'is_top',
  278. 'html_text').distinct()
  279. else:
  280. articles = models.Article.objects.filter(category=category).order_by('-created_time').values('id', 'title',
  281. 'intro',
  282. 'category',
  283. 'user',
  284. 'created_time',
  285. 'type',
  286. 'cover__file_net_path',
  287. 'music__file_net_path',
  288. 'status',
  289. 'category__name',
  290. 'user__first_name',
  291. 'user_id',
  292. 'user__last_name',
  293. 'is_top',
  294. 'html_text')
  295. if is_paginator:
  296. if page <= 0:
  297. page = 1
  298. paginator = Paginator(articles, 15)
  299. try:
  300. articles = paginator.page(page)
  301. except PageNotAnInteger:
  302. articles = paginator.page(1)
  303. except EmptyPage:
  304. articles = paginator.page(paginator.num_pages)
  305. set_tag(articles=articles)
  306. return articles
  307. else:
  308. articles = list(articles)
  309. if top > -1:
  310. articles = articles[:top]
  311. set_tag(articles=articles)
  312. return articles
  313. # return JsonResponse({'all_article': json.loads(json.dumps(articles, default=str))})
  314. @login_required(login_url='/')
  315. def get_all_article(request):
  316. articles = models.Article.objects.order_by('-created_time').values('id', 'title',
  317. 'intro',
  318. 'category',
  319. 'user',
  320. 'created_time',
  321. 'type',
  322. 'cover__file_net_path',
  323. 'music__file_net_path',
  324. 'status',
  325. 'category__name',
  326. 'user__first_name', 'user_id',
  327. 'user__last_name')
  328. return JsonResponse(json.loads(json.dumps(list(articles), default=str)), safe=False)
  329. @login_required(login_url='/')
  330. def delete_article(request):
  331. if request.method == 'GET':
  332. id = request.GET.get("id")
  333. article = models.Article.objects.get(id=int(id))
  334. article.tags.clear()
  335. article.delete()
  336. return HttpResponseRedirect('/management/article') # 跳转到主界面
  337. else:
  338. raise ServerException("错误的请求")