LogController.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from django.db.models import Q
  4. from django.utils.decorators import method_decorator
  5. from django.views import View
  6. from django.views.decorators.csrf import csrf_exempt
  7. from Model.models import LogModel
  8. from Object.ResponseObject import ResponseObject
  9. from Object.TokenObject import TokenObject
  10. class LogView(View):
  11. @method_decorator(csrf_exempt)
  12. def dispatch(self, request, *args, **kwargs):
  13. return super(LogView, self).dispatch(request, *args, **kwargs)
  14. def get(self, request, *args, **kwargs):
  15. request.encoding = 'utf-8'
  16. request_dict = request.GET
  17. operation = kwargs.get('operation')
  18. return self.validate(request_dict, operation)
  19. def post(self, request, *args, **kwargs):
  20. request.encoding = 'utf-8'
  21. request_dict = request.POST
  22. operation = kwargs.get('operation')
  23. return self.validate(request_dict, operation)
  24. def validate(self, request_dict, operation):
  25. token = TokenObject(request_dict.get('token', None))
  26. response = ResponseObject()
  27. if token.code != 0:
  28. return response.json(token.code)
  29. if operation == 'query':
  30. return self.do_query(request_dict, token, response)
  31. elif operation == 'queryAll':
  32. return self.do_query_all(request_dict, token, response)
  33. elif operation == 'delete':
  34. return self.do_delete(request_dict, response)
  35. elif operation == 'adminQuery':
  36. return self.do_admin_query(request_dict, token, response)
  37. else:
  38. return response.json(404)
  39. def do_query(self, request_dict, token: TokenObject, response):
  40. page = request_dict.get('page', None)
  41. line = request_dict.get('line', None)
  42. if page and line:
  43. log_qs = LogModel.objects.filter(
  44. Q(user__id=token.userID) & ~Q(operation='登录账号') & ~Q(operation='退出登录')).values('id', 'operation',
  45. 'time', 'ip',
  46. 'user__username')
  47. if log_qs.exists():
  48. page = int(page)
  49. line = int(line)
  50. start = (page - 1) * line
  51. count = log_qs.count()
  52. data = log_qs[start:(start + line)]
  53. return response.json(0, {'count': count, 'data': list(data)})
  54. else:
  55. return response.json(0, {'count': 0, 'data': []})
  56. else:
  57. return response.json(444)
  58. def do_query_all(self, request_dict, token: TokenObject, response: ResponseObject):
  59. log_qs = LogModel.objects.filter(user__id=token.userID).values('id', 'operation', 'time', 'ip')
  60. if log_qs.exists():
  61. count = log_qs.count()
  62. return response.json(0, {'count': count, 'data': list(log_qs)})
  63. else:
  64. return response.json(0, {'count': 0, 'data': []})
  65. def do_delete(self, request_dict, response):
  66. id = request_dict.get('id', None)
  67. if id:
  68. LogModel.objects.filter(id=id).delete()
  69. return response.json(0)
  70. else:
  71. return response.json(444)
  72. def do_admin_query(self, request_dict, token: TokenObject, response):
  73. page = request_dict.get('page', None)
  74. line = request_dict.get('limit', None)
  75. if page and line:
  76. log_qs = LogModel.objects.filter().values('id', 'operation', 'time', 'ip', 'user__username')
  77. if log_qs.exists():
  78. page = int(page)
  79. line = int(line)
  80. start = (page - 1) * line
  81. count = log_qs.count()
  82. data = log_qs[start:(start + line)]
  83. return response.json(0, {'count': count, 'data': list(data)})
  84. else:
  85. return response.json(0, {'count': 0, 'data': []})
  86. else:
  87. return response.json(444)