jumpserver/apps/assets/serializers/domain.py

55 lines
1.5 KiB
Python
Raw Normal View History

2018-03-23 11:46:46 +00:00
# -*- coding: utf-8 -*-
#
from rest_framework import serializers
from common.serializers import AdaptedBulkListSerializer
from orgs.mixins import BulkOrgResourceModelSerializer
2018-03-23 11:46:46 +00:00
from ..models import Domain, Gateway
class DomainSerializer(BulkOrgResourceModelSerializer):
2018-03-23 11:46:46 +00:00
asset_count = serializers.SerializerMethodField()
gateway_count = serializers.SerializerMethodField()
class Meta:
model = Domain
fields = '__all__'
list_serializer_class = AdaptedBulkListSerializer
2018-03-23 11:46:46 +00:00
@staticmethod
def get_asset_count(obj):
return obj.assets.count()
@staticmethod
def get_gateway_count(obj):
return obj.gateway_set.all().count()
class GatewaySerializer(BulkOrgResourceModelSerializer):
2018-03-23 11:46:46 +00:00
class Meta:
model = Gateway
list_serializer_class = AdaptedBulkListSerializer
2018-03-25 13:47:29 +00:00
fields = [
'id', 'name', 'ip', 'port', 'protocol', 'username',
'domain', 'is_active', 'date_created', 'date_updated',
'created_by', 'comment',
]
class GatewayWithAuthSerializer(GatewaySerializer):
def get_field_names(self, declared_fields, info):
fields = super().get_field_names(declared_fields, info)
fields.extend(
['password', 'private_key']
)
return fields
class DomainWithGatewaySerializer(BulkOrgResourceModelSerializer):
2018-03-25 13:47:29 +00:00
gateways = GatewayWithAuthSerializer(many=True, read_only=True)
class Meta:
model = Domain
2018-03-23 11:46:46 +00:00
fields = '__all__'