category.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import json
  2. from django.contrib.auth.decorators import login_required
  3. from django.core import serializers
  4. from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
  5. from django.http import JsonResponse, HttpResponseRedirect
  6. from django.shortcuts import render
  7. from blog.views import get_record_and_tags
  8. from system.error.ServerException import ServerException
  9. from blog import models
  10. from blog.controller import article
  11. @login_required(login_url='/')
  12. def category(request):
  13. # page: int = -1, is_paginator: bool = False, search_name: str = None
  14. if request.method == 'GET':
  15. page = request.GET.get('page')
  16. if page is None:
  17. page = 1
  18. else:
  19. page = int(page)
  20. search_name = request.GET.get('search_name')
  21. if search_name is None:
  22. category = models.Category.objects.all().values('id', 'name','seq')
  23. else:
  24. category = models.Category.objects.filter(name__contains=search_name).values('id', 'name','seq')
  25. if page <= 0:
  26. page = 1
  27. paginator = Paginator(category, 15)
  28. try:
  29. category = paginator.page(page)
  30. except PageNotAnInteger:
  31. category = paginator.page(1)
  32. except EmptyPage:
  33. category = paginator.page(paginator.num_pages)
  34. return render(request, 'management/category/category.html', context={'category': category})
  35. def current_category(request, name):
  36. category_obj = models.Category.objects.get(name=name)
  37. page = request.GET.get('page')
  38. if page is None:
  39. page = 0
  40. result = get_record_and_tags()
  41. context = {'current_category': category_obj.name,
  42. 'articles': article.get_article(is_paginator=True, page=int(page), category=category_obj),
  43. 'records': result['records'],
  44. 'tags': result['tags']
  45. }
  46. return render(request, 'category.html', context=context)
  47. @login_required(login_url='/')
  48. def get_all_category(request):
  49. category = serializers.serialize("json", models.Category.objects.all())
  50. return JsonResponse(json.loads(category), safe=False)
  51. @login_required(login_url='/')
  52. def add_category(request):
  53. if request.method == 'POST':
  54. category_name = request.POST.get("category_name")
  55. seq = request.POST.get("seq")
  56. if category_name == '':
  57. return HttpResponseRedirect('/management/category')
  58. else:
  59. obj = models.Category(name=category_name, seq=int(seq))
  60. obj.save()
  61. return HttpResponseRedirect('/management/category')
  62. else:
  63. raise ServerException("错误的请求")
  64. @login_required(login_url='/')
  65. def delete_category(request):
  66. if request.method == 'GET':
  67. ids_str = request.GET.get("id")
  68. models.Category.objects.get(id=int(ids_str)).delete()
  69. return HttpResponseRedirect('/management/category') # 跳转到主界面
  70. else:
  71. raise ServerException("错误的请求")