diff --git a/apps/assets/api/system_user.py b/apps/assets/api/system_user.py index 6ffd1c3c5..03a506640 100644 --- a/apps/assets/api/system_user.py +++ b/apps/assets/api/system_user.py @@ -39,7 +39,6 @@ class SystemUserViewSet(OrgBulkModelViewSet): serializer_class = serializers.SystemUserSerializer serializer_classes = { 'default': serializers.SystemUserSerializer, - 'list': serializers.SystemUserListSerializer, } permission_classes = (IsOrgAdminOrAppUser,) diff --git a/apps/assets/serializers/admin_user.py b/apps/assets/serializers/admin_user.py index bf16004f4..c72670ba8 100644 --- a/apps/assets/serializers/admin_user.py +++ b/apps/assets/serializers/admin_user.py @@ -1,40 +1,25 @@ # -*- coding: utf-8 -*- # -from django.utils.translation import ugettext_lazy as _ - from ..models import SystemUser -from orgs.mixins.serializers import BulkOrgResourceModelSerializer - -from .base import AuthSerializerMixin +from .system_user import SystemUserSerializer as SuS -class AdminUserSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer): +class AdminUserSerializer(SuS): """ 管理用户 """ - class Meta: - model = SystemUser - fields_mini = ['id', 'name', 'username'] - fields_write_only = ['password', 'private_key', 'public_key'] - fields_small = fields_mini + fields_write_only + [ - 'date_created', 'date_updated', - 'comment', 'created_by' - ] - fields_fk = ['assets_amount'] - fields = fields_small + fields_fk - read_only_fields = ['date_created', 'date_updated', 'created_by', 'assets_amount'] + class Meta(SuS.Meta): + fields = SuS.Meta.fields_mini + \ + SuS.Meta.fields_write_only + \ + SuS.Meta.fields_m2m + \ + [ + 'type', 'protocol', "priority", 'sftp_root', 'ssh_key_fingerprint', + 'date_created', 'date_updated', 'comment', 'created_by', + ] - extra_kwargs = { - 'username': {"required": True}, - 'password': {"write_only": True}, - 'private_key': {"write_only": True}, - 'public_key': {"write_only": True}, - 'assets_amount': {'label': _('Asset')}, - } + def validate_type(self, val): + return SystemUser.Type.admin - def create(self, validated_data): - data = {k: v for k, v in validated_data.items()} - data['protocol'] = 'ssh' - data['type'] = SystemUser.Type.admin - return super().create(data) + def validate_protocol(self, val): + return 'ssh' diff --git a/apps/assets/serializers/system_user.py b/apps/assets/serializers/system_user.py index 12f383944..76589e84b 100644 --- a/apps/assets/serializers/system_user.py +++ b/apps/assets/serializers/system_user.py @@ -9,7 +9,7 @@ from ..models import SystemUser, Asset from .base import AuthSerializerMixin __all__ = [ - 'SystemUserSerializer', 'SystemUserListSerializer', + 'SystemUserSerializer', 'SystemUserSimpleSerializer', 'SystemUserAssetRelationSerializer', 'SystemUserNodeRelationSerializer', 'SystemUserTaskSerializer', 'SystemUserUserRelationSerializer', 'SystemUserWithAuthInfoSerializer', @@ -22,7 +22,7 @@ class SystemUserSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer): 系统用户 """ auto_generate_key = serializers.BooleanField(initial=True, required=False, write_only=True) - type_display = serializers.ReadOnlyField(source='get_type_display') + type_display = serializers.ReadOnlyField(source='get_type_display', label=_('Type display')) ssh_key_fingerprint = serializers.ReadOnlyField(label=_('SSH key fingerprint')) class Meta: @@ -173,33 +173,6 @@ class SystemUserSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer): attrs = self.validate_gen_key(attrs) return attrs - -class SystemUserListSerializer(SystemUserSerializer): - - class Meta(SystemUserSerializer.Meta): - fields_mini = ['id', 'name', 'username'] - fields_write_only = ['password', 'public_key', 'private_key'] - fields_small = fields_mini + fields_write_only + [ - 'protocol', 'login_mode', 'login_mode_display', 'priority', - 'sudo', 'shell', 'home', 'system_groups', - 'ad_domain', 'sftp_root', 'ssh_key_fingerprint', - "username_same_with_user", 'auto_push', 'auto_generate_key', - 'date_created', 'date_updated', - 'comment', 'created_by', - ] - fields_m2m = ["assets_amount"] - fields = fields_small + fields_m2m - extra_kwargs = { - 'password': {"write_only": True}, - 'public_key': {"write_only": True}, - 'private_key': {"write_only": True}, - 'nodes_amount': {'label': _('Nodes amount')}, - 'assets_amount': {'label': _('Assets amount')}, - 'login_mode_display': {'label': _('Login mode display')}, - 'created_by': {'read_only': True}, - 'ad_domain': {'label': _('Ad domain')}, - } - @classmethod def setup_eager_loading(cls, queryset): """ Perform necessary eager loading of data. """