views.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from django.http import JsonResponse
  2. from django.contrib.auth.decorators import login_required
  3. from .models import Article, Tag, ArticleClass, Draft
  4. @login_required(login_url='/account/login/')
  5. def post_article(request):
  6. if request.method == 'POST':
  7. data = request.POST
  8. article_class = ArticleClass.objects.filter(Name=data['class'])
  9. if article_class:
  10. new_article = Article()
  11. new_article.Publisher = request.user
  12. new_article.Title = data['title']
  13. new_article.Content = data['content']
  14. new_article.Cover = data['cover']
  15. new_article.Class = article_class.get()
  16. new_article.Read = 0
  17. new_article.Like = 0
  18. new_article.save()
  19. for i in range(0, 100):
  20. try:
  21. tag_name = data['tag[' + str(i) + ']']
  22. tag = Tag.objects.filter(Name=tag_name)
  23. if tag:
  24. new_article.Tag.add(tag.get())
  25. else:
  26. new_article.Tag.create(Name=tag_name)
  27. except Exception as e:
  28. break
  29. new_article.save()
  30. res = {
  31. 'code': '402',
  32. 'message': 'Publish Successfully'
  33. }
  34. return JsonResponse(res)
  35. else:
  36. res = {
  37. 'code': '403',
  38. 'message': 'Invalid data'
  39. }
  40. return JsonResponse(res)
  41. else:
  42. # 请求方法错误,请使用POST
  43. res = {
  44. 'code': '401',
  45. 'message': 'Please Use POST'
  46. }
  47. return JsonResponse(res)
  48. def article_list(request):
  49. articles = Article.object.all()
  50. res = {
  51. 'code': '402',
  52. 'articles': []
  53. }
  54. for article in articles:
  55. res[articles].append({
  56. 'Id': article.ArticleId,
  57. 'Title': article.Title,
  58. 'Content': article.Content,
  59. 'Publisher': article.Publisher.username,
  60. 'Read': article.Read,
  61. 'Like': article.Like,
  62. 'Publish_Time': article.Publish_Time,
  63. 'Edit_Time': article.Edit_Time,
  64. })
  65. return JsonResponse(res, safe=False)
  66. @login_required(login_url='/account/login/')
  67. def get_article_classes(request):
  68. aclasses = ArticleClass.objects.all()
  69. classes = []
  70. for aclass in aclasses:
  71. classes.append(aclass.Name)
  72. res = {
  73. 'code': '402',
  74. 'classes': classes
  75. }
  76. return JsonResponse(res)
  77. @login_required(login_url='/account/login/')
  78. def get_article_tags(request):
  79. tags = []
  80. for tag in Tag.objects.all():
  81. tags.append(tag.Name)
  82. res = {
  83. 'code': '402',
  84. 'tags': tags
  85. }
  86. return JsonResponse(res)
  87. @login_required(login_url='/account/login/')
  88. def save_draft(request):
  89. if request.method == 'POST':
  90. data = request.POST
  91. draft = Draft.objects.filter(Title=data['title'], Poster=request.user)
  92. if draft:
  93. draft = draft.get()
  94. draft.Content = data['content']
  95. draft.save()
  96. else:
  97. new_draft = Draft()
  98. new_draft.Title = data['title']
  99. new_draft.Content = data['content']
  100. new_draft.Poster = request.user
  101. new_draft.save()
  102. res = {
  103. 'code': '402',
  104. 'message': 'Saved Successfully'
  105. }
  106. return JsonResponse(res)
  107. else:
  108. # 请求方法错误,请使用POST
  109. res = {
  110. 'code': '401',
  111. 'message': 'Please Use POST'
  112. }
  113. return JsonResponse(res)