LogMiddleware.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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':
  39. if response.status_code != 200:
  40. return
  41. else:
  42. jsonObject = json.loads(response.content.decode().strip())
  43. code = jsonObject.get('code')
  44. if code is None or code != 0 and response.status_code != 200:
  45. print('code is {code}'.format(code=code))
  46. return
  47. token = request_dict.get('token', None)
  48. # print(token)
  49. token = TokenObject(token)
  50. status = response.status_code
  51. # 去除密码
  52. contentDict = dict(request_dict)
  53. # print(contentDict)
  54. password = contentDict.get('password')
  55. if password:
  56. contentDict.pop('password')
  57. content = json.dumps(contentDict)
  58. ip = CommonService.get_ip_address(request)
  59. now_time = time.time()
  60. if token.code == 0:
  61. user_qs = UserModel.objects.filter(id=token.userID)
  62. else:
  63. # print(token.code)
  64. username = request_dict.get('username', None)
  65. if username is None:
  66. print('username')
  67. return
  68. user_qs = UserModel.objects.filter(username=username)
  69. if not user_qs.exists():
  70. # print('exists')
  71. return
  72. user = user_qs[0]
  73. operation = ''
  74. # print(request_path)
  75. if request_path == 'user/login':
  76. operation = '登录账号'
  77. elif request_path == 'user/logout':
  78. operation = '退出登录'
  79. elif request_path == 'user/modify':
  80. operation = '修改密码'
  81. elif request_path == 'upload':
  82. area = request_dict.get('area', None)
  83. count = jsonObject['res']['count']
  84. operation = formatOperation('上传', int(count), int(area))
  85. elif request_path == 'uid/allot':
  86. area = request_dict.get('area', None)
  87. quantity = request_dict.get('quantity', None)
  88. if area and quantity:
  89. operation = formatOperation('分配', int(quantity), int(area))
  90. elif request_path == 'download':
  91. area = request_dict.get('area', None)
  92. quantity = request_dict.get('quantity', None)
  93. if area and quantity:
  94. operation = formatOperation('下载', int(quantity), int(area))
  95. else:
  96. return
  97. log = {
  98. 'status': status,
  99. 'content': content,
  100. 'ip': ip,
  101. 'time': now_time,
  102. 'url': request_path,
  103. 'operation': operation,
  104. 'user': user
  105. }
  106. try:
  107. LogModel.objects.create(**log)
  108. except Exception as e:
  109. print(repr(e))
  110. def formatOperation(operation, quantity, area):
  111. str = '{operation}{quantity}个{area}UID'
  112. if area == 0:
  113. return str.format(operation=operation, quantity=quantity, area='国内')
  114. else:
  115. return str.format(operation=operation, quantity=quantity, area='国外')