mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-08-22 00:04:06 +00:00
* perf: 整合系统用户和管理用户 * stash stash perf: 优化系统用户和资产的表结构 * perf: 添加信号 * perf: 添加算法 * perf: 去掉 asset user backends * perf: 整理系统用户api * perfF: 暂存一下 * stash * perf: 暂存一下 * perf: 暂存 * xxx * perf: ... * stash it * xxx * xxx * xxx * xxx * xxx * stash it * 修改Protocols * perf: 修改创建authbook信号 * perf: 添加auth info * .stash * perf: 基本完成 * perf: 修复完成 * perf: 修复更改的id * perf: 修复迁移过去数量不对的问题 * perf: 修改systemuser * fix: 修复批量编辑近期的问题 * fix: 修复authbook加载的问题 * xxx Co-authored-by: ibuler <ibuler@qq.com>
54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
# -*- 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 .. import utils
|
|
|
|
__all__ = [
|
|
'UserGroupSerializer',
|
|
]
|
|
|
|
|
|
class UserGroupSerializer(BulkOrgResourceModelSerializer):
|
|
users = serializers.PrimaryKeyRelatedField(
|
|
required=False, many=True, queryset=User.objects, label=_('User'),
|
|
# write_only=True, # group can return many to many on detail
|
|
)
|
|
|
|
class Meta:
|
|
model = UserGroup
|
|
fields_mini = ['id', 'name']
|
|
fields_small = fields_mini + [
|
|
'comment', 'date_created', 'created_by'
|
|
]
|
|
fields = fields_mini + fields_small + [
|
|
'users', 'users_amount',
|
|
]
|
|
extra_kwargs = {
|
|
'created_by': {'label': _('Created by'), 'read_only': True},
|
|
'users_amount': {'label': _('Users amount')},
|
|
'id': {'label': _('ID')},
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.set_fields_queryset()
|
|
|
|
def set_fields_queryset(self):
|
|
users_field = self.fields.get('users')
|
|
if users_field:
|
|
users_field.child_relation.queryset = utils.get_current_org_members()
|
|
|
|
@classmethod
|
|
def setup_eager_loading(cls, queryset):
|
|
""" Perform necessary eager loading of data. """
|
|
queryset = queryset.prefetch_related(
|
|
Prefetch('users', queryset=User.objects.only('id'))
|
|
).annotate(users_amount=Count('users'))
|
|
return queryset
|