models.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import datetime
  2. import re
  3. import jwt
  4. from django.db import models
  5. # Create your models here.
  6. from TestLaboratory.settings import SECRET_KEY
  7. from apps.user.models import User, Role
  8. from utils.util_add_id import get_id, gen_next_id
  9. class Log(models.Model):
  10. id = models.CharField(max_length=128, primary_key=True, verbose_name='日志id', default='TestLaboratory_V1_Log_1')
  11. action = models.CharField(max_length=100, verbose_name='操作名称')
  12. entity_class = models.CharField(max_length=50, verbose_name='实体类型')
  13. entity_name = models.CharField(max_length=50, null=True, verbose_name='实体名称')
  14. method = models.CharField(max_length=30, verbose_name='操作方法')
  15. executor_id = models.CharField(max_length=50, verbose_name='操作者id')
  16. executor_username = models.CharField(max_length=50, verbose_name='操作者名称')
  17. executor_identify = models.IntegerField(verbose_name='操作者身份')
  18. create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
  19. update_time = models.DateTimeField(auto_now=True, verbose_name='修改时间')
  20. delete = models.BooleanField(default=False)
  21. def __str__(self):
  22. return self.executor_username + self.action
  23. class Meta:
  24. verbose_name = '日志'
  25. verbose_name_plural = verbose_name
  26. db_table = 'log'
  27. def gen_log(action, entity_class, entity_name, method, executor):
  28. log_id = get_id(Log, "TestLaboratory_V1_Log_1")
  29. create_time = update_time = datetime.datetime.now()
  30. try:
  31. log_new = Log.objects.create(id=log_id, action=action, entity_class=entity_class, entity_name=entity_name,
  32. method=method, executor_id=executor.id, executor_username=executor.username,
  33. executor_identify=executor.identify, create_time=create_time,
  34. update_time=update_time)
  35. except:
  36. log_id = gen_next_id(log_id)
  37. log_new = Log.objects.create(id=log_id, action=action, entity_class=entity_class, entity_name=entity_name,
  38. method=method, executor_id=executor.id, executor_username=executor.username,
  39. executor_identify=executor.identify, create_time=create_time,
  40. update_time=update_time)
  41. def get_log(request):
  42. token = request.META.get('HTTP_ACCESSTOKEN')
  43. SALT = SECRET_KEY
  44. payload = jwt.decode(token, SALT, True)
  45. user = User.objects.get(id=payload['userid'])
  46. role = Role.objects.get(id="TestLaboratory_V1_Role_" + str(user.identify + 1))
  47. permission = role.permissions.filter(method=request.method)
  48. # print(user.username)
  49. perm = None
  50. for per in permission:
  51. path = request.path
  52. if request.path[-1] == '/':
  53. path = request.path[:-1]
  54. # print(per.url, per.name, path, re.search(per.url, path))
  55. if re.search(per.url, path):
  56. # print("匹配", per.url, request.path)
  57. if not perm:
  58. perm = per
  59. elif len(per.url) > len(perm.url):
  60. perm = per
  61. # print(perm.url, perm.name, perm.method)
  62. return user, perm.name, perm.method