LogMiddleware.py 3.7 KB

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