mirror of https://github.com/jumpserver/jumpserver
perf: 优化部分 field
parent
042108a9c0
commit
ea31de0b2b
|
@ -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.api import JMSModelViewSet
|
||||||
from common.serializers import GroupedChoiceSerializer
|
from common.serializers import GroupedChoiceSerializer
|
||||||
from assets.models import Platform
|
|
||||||
from assets.const import AllTypes
|
|
||||||
from assets.serializers import PlatformSerializer
|
|
||||||
|
|
||||||
__all__ = ['AssetPlatformViewSet']
|
__all__ = ['AssetPlatformViewSet']
|
||||||
|
|
||||||
|
|
|
@ -16,9 +16,6 @@ __all__ = [
|
||||||
|
|
||||||
|
|
||||||
class PaginatedResponseMixin:
|
class PaginatedResponseMixin:
|
||||||
paginate_queryset: Callable
|
|
||||||
get_serializer: Callable
|
|
||||||
get_paginated_response: Callable
|
|
||||||
|
|
||||||
def get_paginated_response_from_queryset(self, queryset):
|
def get_paginated_response_from_queryset(self, queryset):
|
||||||
page = self.paginate_queryset(queryset)
|
page = self.paginate_queryset(queryset)
|
||||||
|
|
|
@ -59,6 +59,8 @@ class RoleViewSet(JMSModelViewSet):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def set_users_amount(queryset):
|
def set_users_amount(queryset):
|
||||||
"""设置角色的用户绑定数量,以减少查询"""
|
"""设置角色的用户绑定数量,以减少查询"""
|
||||||
|
ids = [role.id for role in queryset]
|
||||||
|
queryset = Role.objects.filter(id__in=ids)
|
||||||
org_id = current_org.id
|
org_id = current_org.id
|
||||||
q = Q(role__scope=Role.Scope.system) | Q(role__scope=Role.Scope.org, org_id=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'))
|
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)
|
role.users_amount = role_user_amount_mapper.get(role.id, 0)
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
def paginate_queryset(self, queryset):
|
def get_serializer(self, *args, **kwargs):
|
||||||
page_queryset = super().paginate_queryset(queryset) # 返回是 list 对象
|
if len(args) == 1:
|
||||||
page_queryset_ids = [str(i.id) for i in page_queryset]
|
queryset = self.set_users_amount(args[0])
|
||||||
queryset = queryset.filter(id__in=page_queryset_ids)
|
args = (queryset,)
|
||||||
queryset = self.set_users_amount(queryset)
|
return super().get_serializer(*args, **kwargs)
|
||||||
return queryset
|
|
||||||
|
|
||||||
def perform_update(self, serializer):
|
def perform_update(self, serializer):
|
||||||
instance = serializer.instance
|
instance = serializer.instance
|
||||||
|
|
|
@ -51,10 +51,11 @@ class UserViewSet(CommonApiMixin, UserQuerysetMixin, SuggestionMixin, BulkModelV
|
||||||
queryset = super().get_queryset().prefetch_related('groups')
|
queryset = super().get_queryset().prefetch_related('groups')
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
def paginate_queryset(self, queryset):
|
def get_serializer(self, *args, **kwargs):
|
||||||
page = super().paginate_queryset(queryset)
|
if len(args) == 0:
|
||||||
self.set_users_roles_for_cache(page or queryset)
|
queryset = self.set_users_roles_for_cache(args[0])
|
||||||
return page
|
args = (queryset,)
|
||||||
|
return super().get_serializer(*args, **kwargs)
|
||||||
|
|
||||||
@action(methods=['get'], detail=False, url_path='suggestions')
|
@action(methods=['get'], detail=False, url_path='suggestions')
|
||||||
def match(self, request, *args, **kwargs):
|
def match(self, request, *args, **kwargs):
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- 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 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 .. import utils
|
||||||
|
from ..models import User, UserGroup
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'UserGroupSerializer',
|
'UserGroupSerializer',
|
||||||
|
@ -15,9 +14,8 @@ __all__ = [
|
||||||
|
|
||||||
|
|
||||||
class UserGroupSerializer(BulkOrgResourceModelSerializer):
|
class UserGroupSerializer(BulkOrgResourceModelSerializer):
|
||||||
users = serializers.PrimaryKeyRelatedField(
|
users = ObjectRelatedField(
|
||||||
required=False, many=True, queryset=User.objects, label=_('User'),
|
required=False, many=True, queryset=User.objects, label=_('User'),
|
||||||
# write_only=True, # group can return many to many on detail
|
|
||||||
)
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
|
@ -39,7 +39,7 @@ class RolesSerializerMixin(serializers.Serializer):
|
||||||
label=_("System roles"), many=True, default=default_system_roles
|
label=_("System roles"), many=True, default=default_system_roles
|
||||||
)
|
)
|
||||||
org_roles = ObjectRelatedField(
|
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,
|
label=_("Org roles"), many=True, required=False,
|
||||||
default=default_org_roles
|
default=default_org_roles
|
||||||
)
|
)
|
||||||
|
@ -91,6 +91,8 @@ class UserSerializer(RolesSerializerMixin, CommonBulkSerializerMixin, serializer
|
||||||
)
|
)
|
||||||
login_blocked = serializers.BooleanField(read_only=True, label=_("Login blocked"))
|
login_blocked = serializers.BooleanField(read_only=True, label=_("Login blocked"))
|
||||||
is_expired = serializers.BooleanField(read_only=True, label=_("Is expired"))
|
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(
|
can_public_key_auth = serializers.BooleanField(
|
||||||
source="can_use_ssh_key_login", label=_("Can public key authentication"),
|
source="can_use_ssh_key_login", label=_("Can public key authentication"),
|
||||||
read_only=True
|
read_only=True
|
||||||
|
|
Loading…
Reference in New Issue