1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- from django.utils.decorators import method_decorator
- from django.views import View
- from django.views.decorators.csrf import csrf_exempt
- from Model.models import LogModel
- from Object.ResponseObject import ResponseObject
- from Object.TokenObject import TokenObject
- class LogView(View):
- @method_decorator(csrf_exempt)
- def dispatch(self, request, *args, **kwargs):
- return super(LogView, self).dispatch(request, *args, **kwargs)
- def get(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- request_dict = request.GET
- operation = kwargs.get('operation')
- return self.validate(request_dict, operation)
- def post(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- request_dict = request.POST
- operation = kwargs.get('operation')
- return self.validate(request_dict, operation)
- def validate(self, request_dict, operation):
- token = TokenObject(request_dict.get('token', None))
- response = ResponseObject()
- if token.code != 0:
- return response.json(token.code)
- if operation == 'query':
- return self.do_query(request_dict, token, response)
- elif operation == 'queryAll':
- return self.do_query_all(request_dict, token, response)
- else:
- return response.json(404)
- def do_query(self, request_dict, token: TokenObject, response):
- page = request_dict.get('page', None)
- line = request_dict.get('line', None)
- if page and line:
- log_qs = LogModel.objects.filter(user__id=token.userID).values('id', 'operation', 'time', 'ip')
- if log_qs.exists():
- page = int(page)
- line = int(line)
- start = (page - 1) * line
- count = log_qs.count()
- data = log_qs[start:(start + line)]
- return response.json(0, {'count': count, 'data': list(data)})
- else:
- return response.json(0, {'count': 0, 'data': []})
- else:
- return response.json(444)
- def do_query_all(self, request_dict, token: TokenObject, response: ResponseObject):
- log_qs = LogModel.objects.filter(user__id=token.userID).values('id', 'operation', 'time', 'ip')
- if log_qs.exists():
- count = log_qs.count()
- return response.json(0, {'count': count, 'data': list(log_qs)})
- else:
- return response.json(0, {'count': 0, 'data': []})
|