mirror of https://github.com/jumpserver/jumpserver
perf: 优化最后更新
parent
88638187a5
commit
55de785947
|
@ -1,15 +1,16 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
#
|
#
|
||||||
import re
|
|
||||||
import os
|
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.signals import request_finished
|
from django.core.signals import request_finished
|
||||||
from django.db import connection
|
from django.db import connection
|
||||||
|
from django.db.models.signals import pre_save
|
||||||
|
from django.dispatch import receiver
|
||||||
|
|
||||||
from jumpserver.utils import get_current_request
|
from jumpserver.utils import get_current_request
|
||||||
|
|
||||||
from .local import thread_local
|
from .local import thread_local
|
||||||
|
|
||||||
pattern = re.compile(r'FROM `(\w+)`')
|
pattern = re.compile(r'FROM `(\w+)`')
|
||||||
|
@ -83,6 +84,36 @@ def on_request_finished_release_local(sender, **kwargs):
|
||||||
thread_local.__release_local__()
|
thread_local.__release_local__()
|
||||||
|
|
||||||
|
|
||||||
|
def _get_request_user_name():
|
||||||
|
user_name = 'System'
|
||||||
|
current_request = get_current_request()
|
||||||
|
if current_request and current_request.user.is_authenticated:
|
||||||
|
user_name = current_request.user.name
|
||||||
|
if isinstance(user_name, str):
|
||||||
|
user_name = user_name[:30]
|
||||||
|
return user_name
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(pre_save)
|
||||||
|
def on_create_set_created_by(sender, instance=None, **kwargs):
|
||||||
|
if getattr(instance, '_ignore_auto_created_by', False):
|
||||||
|
return
|
||||||
|
if not hasattr(instance, 'created_by') or instance.created_by:
|
||||||
|
return
|
||||||
|
user_name = _get_request_user_name()
|
||||||
|
instance.created_by = user_name
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(pre_save)
|
||||||
|
def on_update_set_updated_by(sender, instance=None, created=False, **kwargs):
|
||||||
|
if getattr(instance, '_ignore_auto_updated_by', False):
|
||||||
|
return
|
||||||
|
if not hasattr(instance, 'updated_by'):
|
||||||
|
return
|
||||||
|
user_name = _get_request_user_name()
|
||||||
|
instance.updated_by = user_name
|
||||||
|
|
||||||
|
|
||||||
if settings.DEBUG_DEV:
|
if settings.DEBUG_DEV:
|
||||||
request_finished.connect(on_request_finished_logging_db_query)
|
request_finished.connect(on_request_finished_logging_db_query)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -2887,7 +2887,7 @@ msgstr "忽略时间"
|
||||||
|
|
||||||
#: common/db/models.py:34
|
#: common/db/models.py:34
|
||||||
msgid "Updated by"
|
msgid "Updated by"
|
||||||
msgstr "更新人"
|
msgstr "最后更新者"
|
||||||
|
|
||||||
#: common/db/validators.py:9
|
#: common/db/validators.py:9
|
||||||
#, fuzzy
|
#, fuzzy
|
||||||
|
|
|
@ -3,18 +3,15 @@
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from django.conf import LazySettings
|
from django.conf import LazySettings
|
||||||
from django.db.models.signals import post_save, pre_save
|
from django.db.models.signals import post_save
|
||||||
from django.db.utils import ProgrammingError, OperationalError
|
from django.db.utils import ProgrammingError, OperationalError
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
from django.utils.functional import LazyObject
|
from django.utils.functional import LazyObject
|
||||||
|
|
||||||
from jumpserver.utils import current_request
|
|
||||||
|
|
||||||
from common.decorator import on_transaction_commit
|
from common.decorator import on_transaction_commit
|
||||||
from common.signals import django_ready
|
from common.signals import django_ready
|
||||||
from common.utils import get_logger, ssh_key_gen
|
from common.utils import get_logger, ssh_key_gen
|
||||||
from common.utils.connection import RedisPubSub
|
from common.utils.connection import RedisPubSub
|
||||||
|
|
||||||
from .models import Setting
|
from .models import Setting
|
||||||
|
|
||||||
logger = get_logger(__file__)
|
logger = get_logger(__file__)
|
||||||
|
@ -58,19 +55,6 @@ def auto_generate_terminal_host_key(sender, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@receiver(pre_save, dispatch_uid="my_unique_identifier")
|
|
||||||
def on_create_set_created_by(sender, instance=None, **kwargs):
|
|
||||||
if getattr(instance, '_ignore_auto_created_by', False) is True:
|
|
||||||
return
|
|
||||||
if not hasattr(instance, 'created_by') or instance.created_by:
|
|
||||||
return
|
|
||||||
if current_request and current_request.user.is_authenticated:
|
|
||||||
user_name = current_request.user.name
|
|
||||||
if isinstance(user_name, str):
|
|
||||||
user_name = user_name[:30]
|
|
||||||
instance.created_by = user_name
|
|
||||||
|
|
||||||
|
|
||||||
@receiver(django_ready)
|
@receiver(django_ready)
|
||||||
def subscribe_settings_change(sender, **kwargs):
|
def subscribe_settings_change(sender, **kwargs):
|
||||||
logger.debug("Start subscribe setting change")
|
logger.debug("Start subscribe setting change")
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
# Generated by Django 3.2.14 on 2023-02-03 04:01
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('users', '0041_auto_20221220_1956'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='user',
|
||||||
|
name='date_updated',
|
||||||
|
field=models.DateTimeField(auto_now=True, verbose_name='Date updated'),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='user',
|
||||||
|
name='updated_by',
|
||||||
|
field=models.CharField(blank=True, default='', max_length=30, verbose_name='Updated by'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -718,9 +718,8 @@ class User(AuthMixin, TokenMixin, RoleMixin, MFAMixin, AbstractUser):
|
||||||
default=date_expired_default, blank=True, null=True,
|
default=date_expired_default, blank=True, null=True,
|
||||||
db_index=True, verbose_name=_('Date expired')
|
db_index=True, verbose_name=_('Date expired')
|
||||||
)
|
)
|
||||||
created_by = models.CharField(
|
created_by = models.CharField(max_length=30, default='', blank=True, verbose_name=_('Created by'))
|
||||||
max_length=30, default='', blank=True, verbose_name=_('Created by')
|
updated_by = models.CharField(max_length=30, default='', blank=True, verbose_name=_('Updated by'))
|
||||||
)
|
|
||||||
source = models.CharField(
|
source = models.CharField(
|
||||||
max_length=30, default=Source.local,
|
max_length=30, default=Source.local,
|
||||||
choices=Source.choices,
|
choices=Source.choices,
|
||||||
|
@ -733,6 +732,7 @@ class User(AuthMixin, TokenMixin, RoleMixin, MFAMixin, AbstractUser):
|
||||||
need_update_password = models.BooleanField(
|
need_update_password = models.BooleanField(
|
||||||
default=False, verbose_name=_('Need update password')
|
default=False, verbose_name=_('Need update password')
|
||||||
)
|
)
|
||||||
|
date_updated = models.DateTimeField(auto_now=True, verbose_name=_('Date updated'))
|
||||||
wecom_id = models.CharField(null=True, default=None, unique=True, max_length=128, verbose_name=_('WeCom'))
|
wecom_id = models.CharField(null=True, default=None, unique=True, max_length=128, verbose_name=_('WeCom'))
|
||||||
dingtalk_id = models.CharField(null=True, default=None, unique=True, max_length=128, verbose_name=_('DingTalk'))
|
dingtalk_id = models.CharField(null=True, default=None, unique=True, max_length=128, verbose_name=_('DingTalk'))
|
||||||
feishu_id = models.CharField(null=True, default=None, unique=True, max_length=128, verbose_name=_('FeiShu'))
|
feishu_id = models.CharField(null=True, default=None, unique=True, max_length=128, verbose_name=_('FeiShu'))
|
||||||
|
|
|
@ -115,17 +115,21 @@ class UserSerializer(RolesSerializerMixin, CommonBulkSerializerMixin, serializer
|
||||||
# small 指的是 不需要计算的直接能从一张表中获取到的数据
|
# small 指的是 不需要计算的直接能从一张表中获取到的数据
|
||||||
fields_small = fields_mini + fields_write_only + [
|
fields_small = fields_mini + fields_write_only + [
|
||||||
"email", "wechat", "phone", "mfa_level", "source",
|
"email", "wechat", "phone", "mfa_level", "source",
|
||||||
"need_update_password", "mfa_enabled",
|
"wecom_id", "dingtalk_id", "feishu_id",
|
||||||
|
"created_by", "updated_by", "comment", # 通用字段
|
||||||
|
]
|
||||||
|
fields_date = [
|
||||||
|
"date_expired", "date_joined",
|
||||||
|
"last_login", "date_updated" # 日期字段
|
||||||
|
]
|
||||||
|
fields_bool = [
|
||||||
"is_service_account", "is_valid",
|
"is_service_account", "is_valid",
|
||||||
"is_expired", "is_active", # 布尔字段
|
"is_expired", "is_active", # 布尔字段
|
||||||
"is_otp_secret_key_bound", "can_public_key_auth",
|
"is_otp_secret_key_bound", "can_public_key_auth",
|
||||||
"date_expired", "date_joined",
|
"mfa_enabled", "need_update_password",
|
||||||
"last_login", # 日期字段
|
|
||||||
"created_by", "comment", # 通用字段
|
|
||||||
"wecom_id", "dingtalk_id", "feishu_id",
|
|
||||||
]
|
]
|
||||||
# 包含不太常用的字段,可以没有
|
# 包含不太常用的字段,可以没有
|
||||||
fields_verbose = fields_small + [
|
fields_verbose = fields_small + fields_date + fields_bool + [
|
||||||
"mfa_force_enabled", "is_first_login",
|
"mfa_force_enabled", "is_first_login",
|
||||||
"date_password_last_updated", "avatar_url",
|
"date_password_last_updated", "avatar_url",
|
||||||
]
|
]
|
||||||
|
@ -281,7 +285,6 @@ class ServiceAccountSerializer(serializers.ModelSerializer):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
from authentication.serializers import AccessKeySerializer
|
from authentication.serializers import AccessKeySerializer
|
||||||
|
|
||||||
self.fields["access_key"] = AccessKeySerializer(read_only=True)
|
self.fields["access_key"] = AccessKeySerializer(read_only=True)
|
||||||
|
|
||||||
def get_username(self):
|
def get_username(self):
|
||||||
|
|
Loading…
Reference in New Issue