2018-03-23 11:46:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
2019-04-25 02:11:50 +00:00
|
|
|
from common.serializers import AdaptedBulkListSerializer
|
2019-06-19 08:45:14 +00:00
|
|
|
from orgs.mixins import BulkOrgResourceModelSerializer
|
2019-04-25 02:11:50 +00:00
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
from ..models import Domain, Gateway
|
2019-07-09 09:30:53 +00:00
|
|
|
from .base import AuthSerializerMixin
|
2018-03-23 11:46:46 +00:00
|
|
|
|
|
|
|
|
2019-06-19 08:45:14 +00:00
|
|
|
class DomainSerializer(BulkOrgResourceModelSerializer):
|
2018-03-23 11:46:46 +00:00
|
|
|
asset_count = serializers.SerializerMethodField()
|
|
|
|
gateway_count = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Domain
|
2019-07-11 10:21:19 +00:00
|
|
|
fields = [
|
|
|
|
'id', 'name', 'asset_count', 'gateway_count', 'comment', 'assets',
|
|
|
|
'date_created'
|
|
|
|
]
|
|
|
|
read_only_fields = ( 'asset_count', 'gateway_count', 'date_created')
|
2019-04-25 02:11:50 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
2019-07-09 09:30:53 +00:00
|
|
|
class GatewaySerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer):
|
2018-03-23 11:46:46 +00:00
|
|
|
class Meta:
|
|
|
|
model = Gateway
|
2019-04-25 02:11:50 +00:00
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
2018-03-25 13:47:29 +00:00
|
|
|
fields = [
|
2019-07-09 09:30:53 +00:00
|
|
|
'id', 'name', 'ip', 'port', 'protocol', 'username', 'password',
|
|
|
|
'private_key', 'public_key', 'domain', 'is_active', 'date_created',
|
|
|
|
'date_updated', 'created_by', 'comment',
|
2018-03-25 13:47:29 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-06-19 08:45:14 +00:00
|
|
|
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__'
|