tag.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 tags_page(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. tags = models.Tags.objects.all().values('id','name')
  23. else:
  24. tags = models.Tags.objects.filter(name__contains=search_name).values('id','name')
  25. if page <= 0:
  26. page = 1
  27. paginator = Paginator(tags, 15)
  28. try:
  29. tags = paginator.page(page)
  30. except PageNotAnInteger:
  31. tags = paginator.page(1)
  32. except EmptyPage:
  33. tags = paginator.page(paginator.num_pages)
  34. return render(request, 'management/tags/tags.html', context={'tags': tags})
  35. def current_tag(request, name):
  36. tag_obj = models.Tags.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': tag_obj.name,
  42. 'articles': article.get_article(is_paginator=True, page=int(page), tag=tag_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_tag(request):
  49. tags = serializers.serialize("json", models.Tags.objects.all())
  50. return JsonResponse(json.loads(tags), safe=False)
  51. @login_required(login_url='/')
  52. def search(request):
  53. term = request.GET.get("term")
  54. if term is not None and term != '':
  55. arr = []
  56. tags = models.Tags.objects.filter(name__contains=term)
  57. for item in tags:
  58. arr.append(item.name)
  59. return JsonResponse(arr, safe=False)
  60. else:
  61. arr = []
  62. tags = models.Tags.objects.all()
  63. for item in tags:
  64. arr.append(item.name)
  65. return JsonResponse(arr, safe=False)
  66. @login_required(login_url='/')
  67. def add_tag(request):
  68. if request.method == 'POST':
  69. tag_name = request.POST.get("tag_name")
  70. if tag_name == '':
  71. return JsonResponse({'success': False}, safe=False)
  72. else:
  73. obj = models.Tags(name=tag_name)
  74. obj.save()
  75. return JsonResponse({'success': True}, safe=False)
  76. else:
  77. raise ServerException("错误的请求")
  78. @login_required(login_url='/')
  79. def delete_tags(request):
  80. if request.method == 'GET':
  81. ids_str = request.GET.get("id")
  82. # models.Tags.objects.raw('DELETE FROM blog_tags WHERE id IN (%s)', ids_str)
  83. models.Tags.objects.get(id=int(ids_str)).delete()
  84. return HttpResponseRedirect('/management/tags') # 跳转到主界面
  85. else:
  86. raise ServerException("错误的请求")