mirror of https://github.com/jumpserver/jumpserver
fix: 操作日志的国际化应该根据查询对象的语言决定
parent
bb62e19ea8
commit
0a5766edd3
|
@ -51,7 +51,7 @@ class OperateLogStore(object):
|
|||
for k, v in raw_diff.items():
|
||||
before, after = v.split(cls.SEP, 1)
|
||||
diff_list.append({
|
||||
'field': k,
|
||||
'field': _(k),
|
||||
'before': before if before else _('empty'),
|
||||
'after': after if after else _('empty'),
|
||||
})
|
||||
|
|
|
@ -80,6 +80,8 @@ class OperateLogActionDetailSerializer(serializers.ModelSerializer):
|
|||
|
||||
class OperateLogSerializer(serializers.ModelSerializer):
|
||||
action = LabeledChoiceField(choices=ActionChoices.choices, label=_("Action"))
|
||||
resource = serializers.SerializerMethodField(label=_("Resource"))
|
||||
resource_type = serializers.SerializerMethodField(label=_('Resource'))
|
||||
|
||||
class Meta:
|
||||
model = models.OperateLog
|
||||
|
@ -91,6 +93,14 @@ class OperateLogSerializer(serializers.ModelSerializer):
|
|||
]
|
||||
fields = fields_small
|
||||
|
||||
@staticmethod
|
||||
def get_resource_type(instance):
|
||||
return _(instance.resource_type)
|
||||
|
||||
@staticmethod
|
||||
def get_resource(instance):
|
||||
return i18n_trans(instance.resource)
|
||||
|
||||
|
||||
class PasswordChangeLogSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
|
|
|
@ -5,6 +5,7 @@ import uuid
|
|||
from django.apps import apps
|
||||
from django.db.models.signals import post_save, pre_save, m2m_changed, pre_delete
|
||||
from django.dispatch import receiver
|
||||
from django.utils import translation
|
||||
|
||||
from audits.handler import (
|
||||
get_instance_current_with_cache_diff, cache_instance_before_data,
|
||||
|
@ -29,39 +30,40 @@ def on_m2m_changed(sender, action, instance, reverse, model, pk_set, **kwargs):
|
|||
if not instance:
|
||||
return
|
||||
|
||||
resource_type = instance._meta.verbose_name
|
||||
current_instance = model_to_dict(instance, include_model_fields=False)
|
||||
with translation.override('en'):
|
||||
resource_type = instance._meta.verbose_name
|
||||
current_instance = model_to_dict(instance, include_model_fields=False)
|
||||
|
||||
instance_id = current_instance.get('id')
|
||||
log_id, before_instance = get_instance_dict_from_cache(instance_id)
|
||||
instance_id = current_instance.get('id')
|
||||
log_id, before_instance = get_instance_dict_from_cache(instance_id)
|
||||
|
||||
field_name = str(model._meta.verbose_name)
|
||||
objs = model.objects.filter(pk__in=pk_set)
|
||||
objs_display = [str(o) for o in objs]
|
||||
action = M2M_ACTION[action]
|
||||
changed_field = current_instance.get(field_name, [])
|
||||
field_name = str(model._meta.verbose_name)
|
||||
objs = model.objects.filter(pk__in=pk_set)
|
||||
objs_display = [str(o) for o in objs]
|
||||
action = M2M_ACTION[action]
|
||||
changed_field = current_instance.get(field_name, [])
|
||||
|
||||
after, before, before_value = None, None, None
|
||||
if action == ActionChoices.create:
|
||||
before_value = list(set(changed_field) - set(objs_display))
|
||||
elif action == ActionChoices.delete:
|
||||
before_value = list(
|
||||
set(changed_field).symmetric_difference(set(objs_display))
|
||||
after, before, before_value = None, None, None
|
||||
if action == ActionChoices.create:
|
||||
before_value = list(set(changed_field) - set(objs_display))
|
||||
elif action == ActionChoices.delete:
|
||||
before_value = list(
|
||||
set(changed_field).symmetric_difference(set(objs_display))
|
||||
)
|
||||
|
||||
if changed_field:
|
||||
after = {field_name: changed_field}
|
||||
if before_value:
|
||||
before = {field_name: before_value}
|
||||
|
||||
if sorted(str(before)) == sorted(str(after)):
|
||||
return
|
||||
|
||||
create_or_update_operate_log(
|
||||
ActionChoices.update, resource_type,
|
||||
resource=instance, log_id=log_id, before=before, after=after
|
||||
)
|
||||
|
||||
if changed_field:
|
||||
after = {field_name: changed_field}
|
||||
if before_value:
|
||||
before = {field_name: before_value}
|
||||
|
||||
if sorted(str(before)) == sorted(str(after)):
|
||||
return
|
||||
|
||||
create_or_update_operate_log(
|
||||
ActionChoices.update, resource_type,
|
||||
resource=instance, log_id=log_id, before=before, after=after
|
||||
)
|
||||
|
||||
|
||||
def signal_of_operate_log_whether_continue(
|
||||
sender, instance, created, update_fields=None
|
||||
|
@ -93,18 +95,18 @@ def on_object_pre_create_or_update(
|
|||
)
|
||||
if not ok:
|
||||
return
|
||||
with translation.override('en'):
|
||||
# users.PrivateToken Model 没有 id 有 pk字段
|
||||
instance_id = getattr(instance, 'id', getattr(instance, 'pk', None))
|
||||
instance_before_data = {'id': instance_id}
|
||||
raw_instance = type(instance).objects.filter(pk=instance_id).first()
|
||||
|
||||
# users.PrivateToken Model 没有 id 有 pk字段
|
||||
instance_id = getattr(instance, 'id', getattr(instance, 'pk', None))
|
||||
instance_before_data = {'id': instance_id}
|
||||
raw_instance = type(instance).objects.filter(pk=instance_id).first()
|
||||
|
||||
if raw_instance:
|
||||
instance_before_data = model_to_dict(raw_instance)
|
||||
operate_log_id = str(uuid.uuid4())
|
||||
instance_before_data['operate_log_id'] = operate_log_id
|
||||
setattr(instance, 'operate_log_id', operate_log_id)
|
||||
cache_instance_before_data(instance_before_data)
|
||||
if raw_instance:
|
||||
instance_before_data = model_to_dict(raw_instance)
|
||||
operate_log_id = str(uuid.uuid4())
|
||||
instance_before_data['operate_log_id'] = operate_log_id
|
||||
setattr(instance, 'operate_log_id', operate_log_id)
|
||||
cache_instance_before_data(instance_before_data)
|
||||
|
||||
|
||||
@receiver(post_save)
|
||||
|
@ -116,23 +118,23 @@ def on_object_created_or_update(
|
|||
)
|
||||
if not ok:
|
||||
return
|
||||
with translation.override('en'):
|
||||
log_id, before, after = None, None, None
|
||||
if created:
|
||||
action = ActionChoices.create
|
||||
after = model_to_dict(instance)
|
||||
log_id = getattr(instance, 'operate_log_id', None)
|
||||
else:
|
||||
action = ActionChoices.update
|
||||
current_instance = model_to_dict(instance)
|
||||
log_id, before, after = get_instance_current_with_cache_diff(current_instance)
|
||||
|
||||
log_id, before, after = None, None, None
|
||||
if created:
|
||||
action = ActionChoices.create
|
||||
after = model_to_dict(instance)
|
||||
log_id = getattr(instance, 'operate_log_id', None)
|
||||
else:
|
||||
action = ActionChoices.update
|
||||
current_instance = model_to_dict(instance)
|
||||
log_id, before, after = get_instance_current_with_cache_diff(current_instance)
|
||||
|
||||
resource_type = sender._meta.verbose_name
|
||||
object_name = sender._meta.object_name
|
||||
create_or_update_operate_log(
|
||||
action, resource_type, resource=instance, log_id=log_id,
|
||||
before=before, after=after, object_name=object_name
|
||||
)
|
||||
resource_type = sender._meta.verbose_name
|
||||
object_name = sender._meta.object_name
|
||||
create_or_update_operate_log(
|
||||
action, resource_type, resource=instance, log_id=log_id,
|
||||
before=before, after=after, object_name=object_name
|
||||
)
|
||||
|
||||
|
||||
@receiver(pre_delete)
|
||||
|
@ -141,11 +143,12 @@ def on_object_delete(sender, instance=None, **kwargs):
|
|||
if not ok:
|
||||
return
|
||||
|
||||
resource_type = sender._meta.verbose_name
|
||||
create_or_update_operate_log(
|
||||
ActionChoices.delete, resource_type,
|
||||
resource=instance, before=model_to_dict(instance)
|
||||
)
|
||||
with translation.override('en'):
|
||||
resource_type = sender._meta.verbose_name
|
||||
create_or_update_operate_log(
|
||||
ActionChoices.delete, resource_type,
|
||||
resource=instance, before=model_to_dict(instance)
|
||||
)
|
||||
|
||||
|
||||
@receiver(django_ready)
|
||||
|
|
|
@ -45,15 +45,6 @@ def write_login_log(*args, **kwargs):
|
|||
UserLoginLog.objects.create(**kwargs)
|
||||
|
||||
|
||||
def get_resource_display(resource):
|
||||
resource_display = str(resource)
|
||||
setting_serializer = SettingsSerializer()
|
||||
label = setting_serializer.get_field_label(resource_display)
|
||||
if label is not None:
|
||||
resource_display = label
|
||||
return resource_display
|
||||
|
||||
|
||||
def _get_instance_field_value(
|
||||
instance, include_model_fields,
|
||||
model_need_continue_fields, exclude_fields=None
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from django.utils import translation
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.contrib.auth.mixins import UserPassesTestMixin
|
||||
from django.http.response import JsonResponse
|
||||
|
@ -66,8 +67,9 @@ class RecordViewLogMixin:
|
|||
|
||||
def list(self, request, *args, **kwargs):
|
||||
response = super().list(request, *args, **kwargs)
|
||||
resource_display = self.get_resource_display(request)
|
||||
resource_type = self.model._meta.verbose_name
|
||||
with translation.override('en'):
|
||||
resource_display = self.get_resource_display(request)
|
||||
resource_type = self.model._meta.verbose_name
|
||||
create_or_update_operate_log(
|
||||
self.ACTION, resource_type, force=True,
|
||||
resource_display=resource_display
|
||||
|
@ -76,8 +78,9 @@ class RecordViewLogMixin:
|
|||
|
||||
def retrieve(self, request, *args, **kwargs):
|
||||
response = super().retrieve(request, *args, **kwargs)
|
||||
resource_type = self.model._meta.verbose_name
|
||||
create_or_update_operate_log(
|
||||
self.ACTION, resource_type, force=True, resource=self.get_object()
|
||||
)
|
||||
with translation.override('en'):
|
||||
resource_type = self.model._meta.verbose_name
|
||||
create_or_update_operate_log(
|
||||
self.ACTION, resource_type, force=True, resource=self.get_object()
|
||||
)
|
||||
return response
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
# coding: utf-8
|
||||
from django.core.cache import cache
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils import translation
|
||||
from django.utils.translation import gettext_noop, ugettext_lazy as _
|
||||
|
||||
from common.utils import i18n_fmt
|
||||
from .basic import BasicSettingSerializer
|
||||
from .other import OtherSettingSerializer
|
||||
from .email import EmailSettingSerializer, EmailContentSettingSerializer
|
||||
|
@ -55,13 +57,16 @@ class SettingsSerializer(
|
|||
|
||||
# 单次计算量不大,搞个缓存,以防操作日志大量写入时,这里影响性能
|
||||
def get_field_label(self, field_name):
|
||||
self.fields_label_mapping = cache.get(self.CACHE_KEY, None)
|
||||
if self.fields_label_mapping is None:
|
||||
self.fields_label_mapping = {}
|
||||
for subclass in SettingsSerializer.__bases__:
|
||||
prefix = getattr(subclass, 'PREFIX_TITLE', _('Setting'))
|
||||
fields = subclass().get_fields()
|
||||
for name, item in fields.items():
|
||||
label = '[%s] %s' % (prefix, getattr(item, 'label', ''))
|
||||
self.fields_label_mapping[name] = label
|
||||
with translation.override('en'):
|
||||
for subclass in SettingsSerializer.__bases__:
|
||||
prefix = getattr(subclass, 'PREFIX_TITLE', _('Setting'))
|
||||
fields = subclass().get_fields()
|
||||
for name, item in fields.items():
|
||||
label = getattr(item, 'label', '')
|
||||
detail = i18n_fmt(gettext_noop('[%s] %s'), prefix, label)
|
||||
self.fields_label_mapping[name] = detail
|
||||
cache.set(self.CACHE_KEY, self.fields_label_mapping, 3600 * 24)
|
||||
return self.fields_label_mapping.get(field_name)
|
||||
|
|
Loading…
Reference in New Issue