article.py 21 KB

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