LogController.py 2.5 KB

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