jumpserver/apps/assets/serializers/system_user.py

91 lines
2.4 KiB
Python
Raw Normal View History

2018-02-07 15:25:15 +00:00
from rest_framework import serializers
from common.serializers import AdaptedBulkListSerializer
from ..models import SystemUser, Asset
from .base import AuthSerializer
2018-02-07 15:25:15 +00:00
class SystemUserSerializer(serializers.ModelSerializer):
"""
系统用户
"""
unreachable_amount = serializers.SerializerMethodField()
reachable_amount = serializers.SerializerMethodField()
unreachable_assets = serializers.SerializerMethodField()
reachable_assets = serializers.SerializerMethodField()
assets_amount = serializers.SerializerMethodField()
class Meta:
model = SystemUser
exclude = ('_password', '_private_key', '_public_key')
list_serializer_class = AdaptedBulkListSerializer
2018-02-07 15:25:15 +00:00
def get_field_names(self, declared_fields, info):
fields = super(SystemUserSerializer, self).get_field_names(declared_fields, info)
fields.extend([
'login_mode_display',
])
return fields
2018-02-07 15:25:15 +00:00
@staticmethod
def get_unreachable_assets(obj):
return obj.assets_unreachable
2018-02-07 15:25:15 +00:00
@staticmethod
def get_reachable_assets(obj):
return obj.assets_reachable
2018-02-07 15:25:15 +00:00
def get_unreachable_amount(self, obj):
return len(self.get_unreachable_assets(obj))
def get_reachable_amount(self, obj):
return len(self.get_reachable_assets(obj))
@staticmethod
def get_assets_amount(obj):
return len(obj.get_related_assets())
2018-02-07 15:25:15 +00:00
class SystemUserAuthSerializer(AuthSerializer):
"""
系统用户认证信息
"""
class Meta:
model = SystemUser
fields = [
"id", "name", "username", "protocol",
2018-07-17 04:06:47 +00:00
"login_mode", "password", "private_key",
]
2018-02-07 15:25:15 +00:00
class AssetSystemUserSerializer(serializers.ModelSerializer):
"""
查看授权的资产系统用户的数据结构这个和AssetSerializer不同字段少
"""
actions = serializers.SerializerMethodField()
2018-02-07 15:25:15 +00:00
class Meta:
model = SystemUser
fields = (
'id', 'name', 'username', 'priority',
'protocol', 'comment', 'login_mode', 'actions',
)
2018-02-07 15:25:15 +00:00
@staticmethod
def get_actions(obj):
return [action.name for action in obj.actions]
2018-02-07 15:25:15 +00:00
class SystemUserSimpleSerializer(serializers.ModelSerializer):
"""
系统用户最基本信息的数据结构
"""
class Meta:
model = SystemUser
fields = ('id', 'name', 'username')