Merge pull request #9385 from jumpserver/pr@dev@perf_user_create

perf: 优化用户创建
pull/9388/head
老广 2 years ago committed by GitHub
commit d1f46768a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,17 +1,14 @@
import csv
import codecs
import csv
from itertools import chain
from django.http import HttpResponse
from django.db import models
from django.http import HttpResponse
from settings.serializers import SettingsSerializer
from common.utils import validate_ip, get_ip_city, get_logger
from common.db import fields
from settings.serializers import SettingsSerializer
from .const import DEFAULT_CITY
logger = get_logger(__name__)
@ -95,7 +92,7 @@ def _get_instance_field_value(
def model_to_dict_for_operate_log(
instance, include_model_fields=True, include_related_fields=True
instance, include_model_fields=True, include_related_fields=False
):
model_need_continue_fields = ['date_updated']
m2m_need_continue_fields = ['history_passwords']
@ -106,7 +103,7 @@ def model_to_dict_for_operate_log(
if include_related_fields:
opts = instance._meta
for f in chain(opts.many_to_many, opts.related_objects):
for f in opts.many_to_many:
value = []
if instance.pk is not None:
related_name = getattr(f, 'attname', '') or getattr(f, 'related_name', '')

@ -5,8 +5,8 @@ from functools import partial
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from common.serializers.fields import EncryptedField, ObjectRelatedField, LabeledChoiceField
from common.serializers import CommonBulkSerializerMixin
from common.serializers.fields import EncryptedField, ObjectRelatedField, LabeledChoiceField
from common.utils import pretty_string, get_logger
from common.validators import PhoneValidator
from rbac.builtin import BuiltinRole
@ -25,24 +25,25 @@ __all__ = [
logger = get_logger(__file__)
def default_system_roles():
return [BuiltinRole.system_user.get_role()]
def default_org_roles():
return [BuiltinRole.org_user.get_role()]
class RolesSerializerMixin(serializers.Serializer):
system_roles = ObjectRelatedField(
queryset=Role.system_roles, attrs=('id', 'display_name'),
label=_("System roles"), many=True
label=_("System roles"), many=True, default=default_system_roles
)
org_roles = ObjectRelatedField(
queryset=Role.org_roles, attrs=('id', 'display_name'),
label=_("Org roles"), many=True
label=_("Org roles"), many=True, required=False,
default=default_org_roles
)
@staticmethod
def get_system_roles_display(user):
return user.system_roles.display
@staticmethod
def get_org_roles_display(user):
return user.org_roles.display
def pop_roles_if_need(self, fields):
request = self.context.get("request")
view = self.context.get("view")
@ -55,6 +56,7 @@ class RolesSerializerMixin(serializers.Serializer):
action = view.action or "list"
if action in ("partial_bulk_update", "bulk_update", "partial_update", "update"):
action = "create"
model_cls_field_mapper = {
SystemRoleBinding: ["system_roles"],
OrgRoleBinding: ["org_roles"],

Loading…
Cancel
Save