LogController.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. else:
  35. return response.json(404)
  36. def do_query(self, request_dict, token: TokenObject, response):
  37. page = request_dict.get('page', None)
  38. line = request_dict.get('limit', None)
  39. if page and line:
  40. log_qs = LogModel.objects.filter(user__id=token.userID).values('id', 'operation', 'time', 'ip', 'user__username')
  41. if log_qs.exists():
  42. page = int(page)
  43. line = int(line)
  44. start = (page - 1) * line
  45. count = log_qs.count()
  46. data = log_qs[start:(start + line)]
  47. return response.json(0, {'count': count, 'data': list(data)})
  48. else:
  49. return response.json(0, {'count': 0, 'data': []})
  50. else:
  51. return response.json(444)
  52. def do_query_all(self, request_dict, token: TokenObject, response: ResponseObject):
  53. log_qs = LogModel.objects.filter(user__id=token.userID).values('id', 'operation', 'time', 'ip')
  54. if log_qs.exists():
  55. count = log_qs.count()
  56. return response.json(0, {'count': count, 'data': list(log_qs)})
  57. else:
  58. return response.json(0, {'count': 0, 'data': []})
  59. def do_delete(self, request_dict, response):
  60. id = request_dict.get('id', None)
  61. if id:
  62. LogModel.objects.filter(id=id).delete()
  63. return response.json(0)
  64. else:
  65. return response.json(444)