perf: 优化部分 field

pull/9699/head
ibuler 2023-02-22 20:30:43 +08:00
parent 042108a9c0
commit ea31de0b2b
6 changed files with 24 additions and 26 deletions

View File

@ -1,9 +1,8 @@
from jumpserver.utils import has_valid_xpack_license
from assets.const import AllTypes
from assets.models import Platform
from assets.serializers import PlatformSerializer
from common.api import JMSModelViewSet
from common.serializers import GroupedChoiceSerializer
from assets.models import Platform
from assets.const import AllTypes
from assets.serializers import PlatformSerializer
__all__ = ['AssetPlatformViewSet']

View File

@ -16,9 +16,6 @@ __all__ = [
class PaginatedResponseMixin:
paginate_queryset: Callable
get_serializer: Callable
get_paginated_response: Callable
def get_paginated_response_from_queryset(self, queryset):
page = self.paginate_queryset(queryset)

View File

@ -59,6 +59,8 @@ class RoleViewSet(JMSModelViewSet):
@staticmethod
def set_users_amount(queryset):
"""设置角色的用户绑定数量,以减少查询"""
ids = [role.id for role in queryset]
queryset = Role.objects.filter(id__in=ids)
org_id = current_org.id
q = Q(role__scope=Role.Scope.system) | Q(role__scope=Role.Scope.org, org_id=org_id)
role_bindings = RoleBinding.objects.filter(q).values_list('role_id').annotate(user_count=Count('user_id'))
@ -69,12 +71,11 @@ class RoleViewSet(JMSModelViewSet):
role.users_amount = role_user_amount_mapper.get(role.id, 0)
return queryset
def paginate_queryset(self, queryset):
page_queryset = super().paginate_queryset(queryset) # 返回是 list 对象
page_queryset_ids = [str(i.id) for i in page_queryset]
queryset = queryset.filter(id__in=page_queryset_ids)
queryset = self.set_users_amount(queryset)
return queryset
def get_serializer(self, *args, **kwargs):
if len(args) == 1:
queryset = self.set_users_amount(args[0])
args = (queryset,)
return super().get_serializer(*args, **kwargs)
def perform_update(self, serializer):
instance = serializer.instance

View File

@ -51,10 +51,11 @@ class UserViewSet(CommonApiMixin, UserQuerysetMixin, SuggestionMixin, BulkModelV
queryset = super().get_queryset().prefetch_related('groups')
return queryset
def paginate_queryset(self, queryset):
page = super().paginate_queryset(queryset)
self.set_users_roles_for_cache(page or queryset)
return page
def get_serializer(self, *args, **kwargs):
if len(args) == 0:
queryset = self.set_users_roles_for_cache(args[0])
args = (queryset,)
return super().get_serializer(*args, **kwargs)
@action(methods=['get'], detail=False, url_path='suggestions')
def match(self, request, *args, **kwargs):

View File

@ -1,13 +1,12 @@
# -*- coding: utf-8 -*-
#
from django.utils.translation import ugettext_lazy as _
from django.db.models import Prefetch
from rest_framework import serializers
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
from django.db.models import Count
from ..models import User, UserGroup
from django.utils.translation import ugettext_lazy as _
from common.serializers.mixin import ObjectRelatedField
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
from .. import utils
from ..models import User, UserGroup
__all__ = [
'UserGroupSerializer',
@ -15,9 +14,8 @@ __all__ = [
class UserGroupSerializer(BulkOrgResourceModelSerializer):
users = serializers.PrimaryKeyRelatedField(
users = ObjectRelatedField(
required=False, many=True, queryset=User.objects, label=_('User'),
# write_only=True, # group can return many to many on detail
)
class Meta:

View File

@ -39,7 +39,7 @@ class RolesSerializerMixin(serializers.Serializer):
label=_("System roles"), many=True, default=default_system_roles
)
org_roles = ObjectRelatedField(
queryset=Role.org_roles, attrs=('id', 'display_name'),
queryset=Role.org_roles, attrs=('id', 'display_name', 'name'),
label=_("Org roles"), many=True, required=False,
default=default_org_roles
)
@ -91,6 +91,8 @@ class UserSerializer(RolesSerializerMixin, CommonBulkSerializerMixin, serializer
)
login_blocked = serializers.BooleanField(read_only=True, label=_("Login blocked"))
is_expired = serializers.BooleanField(read_only=True, label=_("Is expired"))
is_valid = serializers.BooleanField(read_only=True, label=_("Is valid"))
is_otp_secret_key_bound = serializers.BooleanField(read_only=True, label=_("Is OTP bound"))
can_public_key_auth = serializers.BooleanField(
source="can_use_ssh_key_login", label=_("Can public key authentication"),
read_only=True