LogMiddleware.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import json
  4. import threading
  5. import time
  6. from django.utils.deprecation import MiddlewareMixin
  7. from Model.models import UserModel, LogModel
  8. from Object import TokenObject
  9. from Object.TokenObject import TokenObject
  10. from Service.CommonService import CommonService
  11. class LogMiddleware(MiddlewareMixin):
  12. # def process_request(self, request):
  13. # if request.path == '/upload':
  14. # request.encoding = 'utf-8'
  15. # request_dict = request.POST
  16. # print(request.POST)
  17. # request.POST = request_dict
  18. def process_response(self, request, response):
  19. if request.path != '/favicon.ico':
  20. self.start_log_thread(request, response)
  21. return response
  22. def start_log_thread(self, request, response):
  23. print('start_log_thread')
  24. asy = threading.Thread(target=add_log, args=(request, response))
  25. asy.start()
  26. def add_log(request, response):
  27. request.encoding = 'utf-8'
  28. if request.method == 'GET':
  29. request_dict = request.GET
  30. elif request.method == 'POST':
  31. request_dict = request.POST
  32. else:
  33. return
  34. # print(response.content.decode().strip())
  35. request_path = request.path.strip().strip('/')
  36. print(request_path)
  37. jsonObject = {}
  38. if request_path == 'download' or request_path == 'uid/download':
  39. if response.status_code != 200:
  40. return
  41. else:
  42. try:
  43. jsonObject = json.loads(response.content.decode().strip())
  44. code = jsonObject.get('code')
  45. except Exception as e:
  46. print(repr(e))
  47. return
  48. if code is None or code != 0 and response.status_code != 200:
  49. print('code is {code}'.format(code=code))
  50. return
  51. token = request_dict.get('token', None)
  52. # print(token)
  53. token = TokenObject(token)
  54. status = response.status_code
  55. # 去除密码
  56. contentDict = dict(request_dict)
  57. # print(contentDict)
  58. password = contentDict.get('password')
  59. if password:
  60. contentDict.pop('password')
  61. content = json.dumps(contentDict)
  62. ip = CommonService.get_ip_address(request)
  63. now_time = time.time()
  64. if token.code == 0:
  65. user_qs = UserModel.objects.filter(id=token.userID)
  66. else:
  67. # print(token.code)
  68. username = request_dict.get('username', None)
  69. if username is None:
  70. print('username')
  71. return
  72. user_qs = UserModel.objects.filter(username=username)
  73. if not user_qs.exists():
  74. # print('exists')
  75. return
  76. user = user_qs[0]
  77. operation = ''
  78. # print(request_path)
  79. if request_path == 'user/login':
  80. operation = '登录账号'
  81. elif request_path == 'user/logout':
  82. operation = '退出登录'
  83. elif request_path == 'user/modify':
  84. operation = '修改密码'
  85. elif request_path == 'upload':
  86. area = request_dict.get('area', None)
  87. count = jsonObject['res']['count']
  88. operation = formatOperation('上传', int(count), int(area))
  89. elif request_path == 'uid/allot':
  90. area = request_dict.get('area', None)
  91. quantity = request_dict.get('quantity', None)
  92. if area and quantity:
  93. operation = formatOperation('分配', int(quantity), int(area))
  94. elif request_path == 'download':
  95. area = request_dict.get('area', None)
  96. quantity = request_dict.get('quantity', None)
  97. if area and quantity:
  98. operation = formatOperation('下载', int(quantity), int(area))
  99. else:
  100. return
  101. log = {
  102. 'status': status,
  103. 'content': content,
  104. 'ip': ip,
  105. 'time': now_time,
  106. 'url': request_path,
  107. 'operation': operation,
  108. 'user': user
  109. }
  110. try:
  111. LogModel.objects.create(**log)
  112. except Exception as e:
  113. print(repr(e))
  114. def formatOperation(operation, quantity, area):
  115. str = '{operation}{quantity}个{area}UID'
  116. if area == 0:
  117. return str.format(operation=operation, quantity=quantity, area='国内')
  118. else:
  119. return str.format(operation=operation, quantity=quantity, area='国外')