article.py 18 KB

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