LogController.py 3.7 KB

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