mirror of https://github.com/jumpserver/jumpserver
perf: 修改 m2m 日志记录
parent
deba0c9057
commit
984b8dfb28
|
@ -29,7 +29,7 @@ class HostInfoSerializer(serializers.Serializer):
|
||||||
|
|
||||||
|
|
||||||
class HostSerializer(AssetSerializer):
|
class HostSerializer(AssetSerializer):
|
||||||
info = HostInfoSerializer(allow_null=True)
|
info = HostInfoSerializer(required=False)
|
||||||
|
|
||||||
class Meta(AssetSerializer.Meta):
|
class Meta(AssetSerializer.Meta):
|
||||||
model = Host
|
model = Host
|
||||||
|
|
|
@ -64,39 +64,39 @@ AUTH_BACKEND_LABEL_MAPPING = AuthBackendLabelMapping()
|
||||||
|
|
||||||
|
|
||||||
M2M_NEED_RECORD = {
|
M2M_NEED_RECORD = {
|
||||||
User.groups.through._meta.object_name: (
|
User.groups.through.__name__: (
|
||||||
_('User and Group'),
|
_('User and Group'),
|
||||||
_('{User} JOINED {UserGroup}'),
|
_('{User} JOINED {UserGroup}'),
|
||||||
_('{User} LEFT {UserGroup}')
|
_('{User} LEFT {UserGroup}')
|
||||||
),
|
),
|
||||||
Asset.nodes.through._meta.object_name: (
|
Asset.nodes.through.__name__: (
|
||||||
_('Node and Asset'),
|
_('Node and Asset'),
|
||||||
_('{Node} ADD {Asset}'),
|
_('{Node} ADD {Asset}'),
|
||||||
_('{Node} REMOVE {Asset}')
|
_('{Node} REMOVE {Asset}')
|
||||||
),
|
),
|
||||||
AssetPermission.users.through._meta.object_name: (
|
AssetPermission.users.through.__name__: (
|
||||||
_('User asset permissions'),
|
_('User asset permissions'),
|
||||||
_('{AssetPermission} ADD {User}'),
|
_('{AssetPermission} ADD {User}'),
|
||||||
_('{AssetPermission} REMOVE {User}'),
|
_('{AssetPermission} REMOVE {User}'),
|
||||||
),
|
),
|
||||||
AssetPermission.user_groups.through._meta.object_name: (
|
AssetPermission.user_groups.through.__name__: (
|
||||||
_('User group asset permissions'),
|
_('User group asset permissions'),
|
||||||
_('{AssetPermission} ADD {UserGroup}'),
|
_('{AssetPermission} ADD {UserGroup}'),
|
||||||
_('{AssetPermission} REMOVE {UserGroup}'),
|
_('{AssetPermission} REMOVE {UserGroup}'),
|
||||||
),
|
),
|
||||||
AssetPermission.assets.through._meta.object_name: (
|
AssetPermission.assets.through.__name__: (
|
||||||
_('Asset permission'),
|
_('Asset permission'),
|
||||||
_('{AssetPermission} ADD {Asset}'),
|
_('{AssetPermission} ADD {Asset}'),
|
||||||
_('{AssetPermission} REMOVE {Asset}'),
|
_('{AssetPermission} REMOVE {Asset}'),
|
||||||
),
|
),
|
||||||
AssetPermission.nodes.through._meta.object_name: (
|
AssetPermission.nodes.through.__name__: (
|
||||||
_('Node permission'),
|
_('Node permission'),
|
||||||
_('{AssetPermission} ADD {Node}'),
|
_('{AssetPermission} ADD {Node}'),
|
||||||
_('{AssetPermission} REMOVE {Node}'),
|
_('{AssetPermission} REMOVE {Node}'),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
M2M_ACTION = {
|
M2M_ACTION_MAPER = {
|
||||||
POST_ADD: OperateLog.ACTION_CREATE,
|
POST_ADD: OperateLog.ACTION_CREATE,
|
||||||
POST_REMOVE: OperateLog.ACTION_DELETE,
|
POST_REMOVE: OperateLog.ACTION_DELETE,
|
||||||
POST_CLEAR: OperateLog.ACTION_DELETE,
|
POST_CLEAR: OperateLog.ACTION_DELETE,
|
||||||
|
@ -104,34 +104,40 @@ M2M_ACTION = {
|
||||||
|
|
||||||
|
|
||||||
@receiver(m2m_changed)
|
@receiver(m2m_changed)
|
||||||
def on_m2m_changed(sender, action, instance, reverse, model, pk_set, **kwargs):
|
def on_m2m_changed(sender, action, instance, model, pk_set, **kwargs):
|
||||||
if action not in M2M_ACTION:
|
if action not in M2M_ACTION_MAPER:
|
||||||
return
|
return
|
||||||
|
|
||||||
user = current_request.user if current_request else None
|
user = current_request.user if current_request else None
|
||||||
if not user or not user.is_authenticated:
|
if not user or not user.is_authenticated:
|
||||||
return
|
return
|
||||||
|
|
||||||
sender_name = sender._meta.object_name
|
sender_name = sender.__name__
|
||||||
if sender_name in M2M_NEED_RECORD:
|
if sender_name in M2M_NEED_RECORD:
|
||||||
org_id = current_org.id
|
org_id = current_org.id
|
||||||
remote_addr = get_request_ip(current_request)
|
remote_addr = get_request_ip(current_request)
|
||||||
user = str(user)
|
user = str(user)
|
||||||
resource_type, resource_tmpl_add, resource_tmpl_remove = M2M_NEED_RECORD[sender_name]
|
resource_type, resource_tmpl_add, resource_tmpl_remove = M2M_NEED_RECORD[sender_name]
|
||||||
action = M2M_ACTION[action]
|
|
||||||
|
action = M2M_ACTION_MAPER[action]
|
||||||
if action == OperateLog.ACTION_CREATE:
|
if action == OperateLog.ACTION_CREATE:
|
||||||
resource_tmpl = resource_tmpl_add
|
resource_tmpl = resource_tmpl_add
|
||||||
elif action == OperateLog.ACTION_DELETE:
|
elif action == OperateLog.ACTION_DELETE:
|
||||||
resource_tmpl = resource_tmpl_remove
|
resource_tmpl = resource_tmpl_remove
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
|
||||||
to_create = []
|
to_create = []
|
||||||
objs = model.objects.filter(pk__in=pk_set)
|
objs = model.objects.filter(pk__in=pk_set)
|
||||||
|
|
||||||
instance_name = instance._meta.object_name
|
if isinstance(instance, Asset):
|
||||||
|
instance_name = Asset.__name__
|
||||||
|
else:
|
||||||
|
instance_name = instance.__class__.__name__
|
||||||
instance_value = str(instance)
|
instance_value = str(instance)
|
||||||
|
model_name = model.__name__
|
||||||
|
|
||||||
model_name = model._meta.object_name
|
print("Instace name: ", instance_name, instance_value)
|
||||||
|
|
||||||
for obj in objs:
|
for obj in objs:
|
||||||
resource = resource_tmpl.format(**{
|
resource = resource_tmpl.format(**{
|
||||||
instance_name: instance_value,
|
instance_name: instance_value,
|
||||||
|
|
|
@ -4,6 +4,7 @@ import six
|
||||||
|
|
||||||
from rest_framework.fields import ChoiceField
|
from rest_framework.fields import ChoiceField
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
|
|
||||||
from common.utils import decrypt_password
|
from common.utils import decrypt_password
|
||||||
|
@ -64,6 +65,12 @@ class LabeledChoiceField(ChoiceField):
|
||||||
|
|
||||||
|
|
||||||
class ObjectRelatedField(serializers.RelatedField):
|
class ObjectRelatedField(serializers.RelatedField):
|
||||||
|
default_error_messages = {
|
||||||
|
'required': _('This field is required.'),
|
||||||
|
'does_not_exist': _('Invalid pk "{pk_value}" - object does not exist.'),
|
||||||
|
'incorrect_type': _('Incorrect type. Expected pk value, received {data_type}.'),
|
||||||
|
}
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
self.attrs = kwargs.pop('attrs', None) or ('id', 'name')
|
self.attrs = kwargs.pop('attrs', None) or ('id', 'name')
|
||||||
self.many = kwargs.get('many', False)
|
self.many = kwargs.get('many', False)
|
||||||
|
@ -76,10 +83,10 @@ class ObjectRelatedField(serializers.RelatedField):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def to_internal_value(self, data):
|
def to_internal_value(self, data):
|
||||||
if isinstance(data, dict):
|
if not isinstance(data, dict):
|
||||||
pk = data.get(self.attrs[0])
|
|
||||||
else:
|
|
||||||
pk = data
|
pk = data
|
||||||
|
else:
|
||||||
|
pk = data.get('id') or data.get('pk') or data.get(self.attrs[0])
|
||||||
queryset = self.get_queryset()
|
queryset = self.get_queryset()
|
||||||
try:
|
try:
|
||||||
if isinstance(data, bool):
|
if isinstance(data, bool):
|
||||||
|
|
Loading…
Reference in New Issue