1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import datetime
- import re
- import jwt
- from django.db import models
- # Create your models here.
- from TestLaboratory.settings import SECRET_KEY
- from apps.user.models import User, Role
- from utils.util_add_id import get_id, gen_next_id
- class Log(models.Model):
- id = models.CharField(max_length=128, primary_key=True, verbose_name='日志id', default='TestLaboratory_V1_Log_1')
- action = models.CharField(max_length=100, verbose_name='操作名称')
- entity_class = models.CharField(max_length=50, verbose_name='实体类型')
- entity_name = models.CharField(max_length=50, null=True, verbose_name='实体名称')
- method = models.CharField(max_length=30, verbose_name='操作方法')
- executor_id = models.CharField(max_length=50, verbose_name='操作者id')
- executor_username = models.CharField(max_length=50, verbose_name='操作者名称')
- executor_identify = models.IntegerField(verbose_name='操作者身份')
- create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
- update_time = models.DateTimeField(auto_now=True, verbose_name='修改时间')
- delete = models.BooleanField(default=False)
- def __str__(self):
- return self.executor_username + self.action
- class Meta:
- verbose_name = '日志'
- verbose_name_plural = verbose_name
- db_table = 'log'
- def gen_log(action, entity_class, entity_name, method, executor):
- log_id = get_id(Log, "TestLaboratory_V1_Log_1")
- create_time = update_time = datetime.datetime.now()
- try:
- log_new = Log.objects.create(id=log_id, action=action, entity_class=entity_class, entity_name=entity_name,
- method=method, executor_id=executor.id, executor_username=executor.username,
- executor_identify=executor.identify, create_time=create_time,
- update_time=update_time)
- except:
- log_id = gen_next_id(log_id)
- log_new = Log.objects.create(id=log_id, action=action, entity_class=entity_class, entity_name=entity_name,
- method=method, executor_id=executor.id, executor_username=executor.username,
- executor_identify=executor.identify, create_time=create_time,
- update_time=update_time)
- def get_log(request):
- token = request.META.get('HTTP_ACCESSTOKEN')
- SALT = SECRET_KEY
- payload = jwt.decode(token, SALT, True)
- user = User.objects.get(id=payload['userid'])
- role = Role.objects.get(id="TestLaboratory_V1_Role_" + str(user.identify + 1))
- permission = role.permissions.filter(method=request.method)
- # print(user.username)
- perm = None
- for per in permission:
- path = request.path
- if request.path[-1] == '/':
- path = request.path[:-1]
- # print(per.url, per.name, path, re.search(per.url, path))
- if re.search(per.url, path):
- # print("匹配", per.url, request.path)
- if not perm:
- perm = per
- elif len(per.url) > len(perm.url):
- perm = per
- # print(perm.url, perm.name, perm.method)
- return user, perm.name, perm.method
|