LogController.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. # 过滤登录登出,获取/重置序列号日志
  44. log_qs = LogModel.objects.filter(
  45. Q(user__id=token.userID) & ~Q(url='user/login') & ~Q(url='user/logout') & ~Q(
  46. url='serialNumber/getSerial') & ~Q(url='uploadSerialNumber')).\
  47. values('id', 'operation', 'time', 'ip', 'user__username')
  48. if log_qs.exists():
  49. page = int(page)
  50. line = int(line)
  51. start = (page - 1) * line
  52. count = log_qs.count()
  53. data = log_qs[start:(start + line)]
  54. return response.json(0, {'count': count, 'data': list(data)})
  55. else:
  56. return response.json(0, {'count': 0, 'data': []})
  57. else:
  58. return response.json(444)
  59. def do_query_all(self, request_dict, token: TokenObject, response: ResponseObject):
  60. log_qs = LogModel.objects.filter(user__id=token.userID).values('id', 'operation', 'time', 'ip')
  61. if log_qs.exists():
  62. count = log_qs.count()
  63. return response.json(0, {'count': count, 'data': list(log_qs)})
  64. else:
  65. return response.json(0, {'count': 0, 'data': []})
  66. def do_delete(self, request_dict, response):
  67. id = request_dict.get('id', None)
  68. if id:
  69. LogModel.objects.filter(id=id).delete()
  70. return response.json(0)
  71. else:
  72. return response.json(444)
  73. def do_admin_query(self, request_dict, token: TokenObject, response):
  74. page = request_dict.get('page', None)
  75. line = request_dict.get('limit', None)
  76. if page and line:
  77. log_qs = LogModel.objects.filter().values('id', 'operation', 'time', 'ip', 'user__username')
  78. if log_qs.exists():
  79. page = int(page)
  80. line = int(line)
  81. start = (page - 1) * line
  82. count = log_qs.count()
  83. data = log_qs[start:(start + line)]
  84. return response.json(0, {'count': count, 'data': list(data)})
  85. else:
  86. return response.json(0, {'count': 0, 'data': []})
  87. else:
  88. return response.json(444)