diff --git a/src/bk-user/bkuser/apis/web/audit/serializers.py b/src/bk-user/bkuser/apis/web/audit/serializers.py index 582f609a1..9dc861481 100644 --- a/src/bk-user/bkuser/apis/web/audit/serializers.py +++ b/src/bk-user/bkuser/apis/web/audit/serializers.py @@ -18,7 +18,7 @@ from rest_framework import serializers -from bkuser.apps.audit.constants import ObjectTypeEnum, OperationEnum +from bkuser.apps.audit.constants import OBJECT_TYPE_MAP, OPERATION_MAP, ObjectTypeEnum, OperationEnum class OperationAuditRecordListInputSerializer(serializers.Serializer): @@ -40,8 +40,8 @@ class OperationAuditRecordListOutputSerializer(serializers.Serializer): def get_operation(self, obj): # 从 operation_map 中提取 operation 对应的中文标识 - return self.context["operation_map"][obj.operation] + return OPERATION_MAP[obj.operation] def get_object_type(self, obj): # 从 object_type_map 中提取 object_type 对应的中文标识 - return self.context["object_type_map"][obj.object_type] + return OBJECT_TYPE_MAP[obj.object_type] diff --git a/src/bk-user/bkuser/apis/web/audit/views.py b/src/bk-user/bkuser/apis/web/audit/views.py index fdd3755d0..83c585aec 100644 --- a/src/bk-user/bkuser/apis/web/audit/views.py +++ b/src/bk-user/bkuser/apis/web/audit/views.py @@ -21,7 +21,6 @@ from rest_framework.permissions import IsAuthenticated from bkuser.apis.web.mixins import CurrentUserTenantMixin -from bkuser.apps.audit.constants import ObjectTypeEnum, OperationEnum from bkuser.apps.audit.models import OperationAuditRecord from bkuser.apps.permission.constants import PermAction from bkuser.apps.permission.permissions import perm_class @@ -57,12 +56,6 @@ def get_queryset(self): return OperationAuditRecord.objects.filter(filters) - def get_serializer_context(self): - context = super().get_serializer_context() - context["operation_map"] = dict(OperationEnum.get_choices()) - context["object_type_map"] = dict(ObjectTypeEnum.get_choices()) - return context - @swagger_auto_schema( tags=["audit"], operation_description="操作审计列表", diff --git a/src/bk-user/bkuser/apps/audit/constants.py b/src/bk-user/bkuser/apps/audit/constants.py index 2312e72b4..818a04b95 100644 --- a/src/bk-user/bkuser/apps/audit/constants.py +++ b/src/bk-user/bkuser/apps/audit/constants.py @@ -86,3 +86,7 @@ class OperationEnum(str, StructuredEnum): CREATE_VIRTUAL_USER = EnumField("create_virtual_user", label=_("创建虚拟用户")) MODIFY_VIRTUAL_USER = EnumField("modify_virtual_user", label=_("修改虚拟用户信息")) DELETE_VIRTUAL_USER = EnumField("delete_virtual_user", label=_("删除虚拟用户")) + + +OPERATION_MAP = dict(OperationEnum.get_choices()) +OBJECT_TYPE_MAP = dict(ObjectTypeEnum.get_choices()) diff --git a/src/bk-user/bkuser/apps/audit/recorder.py b/src/bk-user/bkuser/apps/audit/recorder.py index e1f65495a..f542a842d 100644 --- a/src/bk-user/bkuser/apps/audit/recorder.py +++ b/src/bk-user/bkuser/apps/audit/recorder.py @@ -50,9 +50,10 @@ def add_audit_record( """ data_before = data_before or {} data_after = data_after or {} + extras = extras or {} # 若有数据变更,则添加记录 - if data_before != data_after or extras != {}: + if data_before != data_after or extras: OperationAuditRecord.objects.create( creator=operator, tenant_id=tenant_id, @@ -62,7 +63,7 @@ def add_audit_record( object_name=object_name, data_before=data_before, data_after=data_after, - extras=extras or {}, + extras=extras, ) @@ -96,7 +97,7 @@ def batch_add_audit_records( ) for obj in objects # 若有数据变更,则添加记录 - if obj.data_before != obj.data_after or obj.extras != {} + if obj.data_before != obj.data_after or obj.extras ] OperationAuditRecord.objects.bulk_create(records, batch_size=100)