2018-03-23 11:46:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from rest_framework import serializers
|
2020-11-11 02:27:18 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2021-10-14 14:27:02 +00:00
|
|
|
from common.validators import alphanumeric
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2022-05-17 13:17:17 +00:00
|
|
|
from common.drf.serializers import SecretReadableMixin
|
2018-03-23 11:46:46 +00:00
|
|
|
from ..models import Domain, Gateway
|
2022-09-06 11:57:03 +00:00
|
|
|
from .base import AuthValidateMixin
|
2018-03-23 11:46:46 +00:00
|
|
|
|
|
|
|
|
2019-06-19 08:45:14 +00:00
|
|
|
class DomainSerializer(BulkOrgResourceModelSerializer):
|
2021-07-30 11:13:47 +00:00
|
|
|
asset_count = serializers.SerializerMethodField(label=_('Assets amount'))
|
2020-11-11 02:27:18 +00:00
|
|
|
gateway_count = serializers.SerializerMethodField(label=_('Gateways count'))
|
2018-03-23 11:46:46 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Domain
|
2020-06-16 09:55:18 +00:00
|
|
|
fields_mini = ['id', 'name']
|
|
|
|
fields_small = fields_mini + [
|
|
|
|
'comment', 'date_created'
|
|
|
|
]
|
|
|
|
fields_m2m = [
|
2022-08-16 08:34:16 +00:00
|
|
|
'asset_count', 'assets', 'gateway_count',
|
2019-07-11 10:21:19 +00:00
|
|
|
]
|
2020-06-16 09:55:18 +00:00
|
|
|
fields = fields_small + fields_m2m
|
|
|
|
read_only_fields = ('asset_count', 'gateway_count', 'date_created')
|
|
|
|
extra_kwargs = {
|
2020-11-11 02:27:18 +00:00
|
|
|
'assets': {'required': False, 'label': _('Assets')},
|
2020-06-16 09:55:18 +00:00
|
|
|
}
|
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()
|
|
|
|
|
|
|
|
|
2022-09-06 11:57:03 +00:00
|
|
|
class GatewaySerializer(AuthValidateMixin, BulkOrgResourceModelSerializer):
|
2022-04-01 08:52:50 +00:00
|
|
|
is_connective = serializers.BooleanField(required=False, label=_('Connectivity'))
|
2021-07-26 08:32:01 +00:00
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
class Meta:
|
|
|
|
model = Gateway
|
2022-09-06 11:57:03 +00:00
|
|
|
fields_mini = ['id', 'username']
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_write_only = [
|
2021-12-30 02:47:46 +00:00
|
|
|
'password', 'private_key', 'public_key', 'passphrase'
|
2021-04-29 11:10:45 +00:00
|
|
|
]
|
|
|
|
fields_small = fields_mini + fields_write_only + [
|
2022-09-06 11:57:03 +00:00
|
|
|
'ip', 'port', 'protocol',
|
2021-07-26 08:32:01 +00:00
|
|
|
'is_active', 'is_connective',
|
2021-04-29 11:10:45 +00:00
|
|
|
'date_created', 'date_updated',
|
|
|
|
'created_by', 'comment',
|
2018-03-25 13:47:29 +00:00
|
|
|
]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_fk = ['domain']
|
2021-05-21 07:13:43 +00:00
|
|
|
fields = fields_small + fields_fk
|
2020-10-27 11:54:41 +00:00
|
|
|
extra_kwargs = {
|
2021-10-14 14:27:02 +00:00
|
|
|
'username': {"validators": [alphanumeric]},
|
2021-07-19 03:10:09 +00:00
|
|
|
'password': {'write_only': True},
|
2021-05-19 09:33:04 +00:00
|
|
|
'private_key': {"write_only": True},
|
|
|
|
'public_key': {"write_only": True},
|
2020-10-27 11:54:41 +00:00
|
|
|
}
|
2018-03-25 13:47:29 +00:00
|
|
|
|
|
|
|
|
2022-05-17 13:17:17 +00:00
|
|
|
class GatewayWithAuthSerializer(SecretReadableMixin, GatewaySerializer):
|
2021-05-21 07:13:43 +00:00
|
|
|
class Meta(GatewaySerializer.Meta):
|
|
|
|
extra_kwargs = {
|
2021-07-19 03:10:09 +00:00
|
|
|
'password': {'write_only': False},
|
2021-05-21 07:13:43 +00:00
|
|
|
'private_key': {"write_only": False},
|
|
|
|
'public_key': {"write_only": False},
|
|
|
|
}
|
2018-03-25 13:47:29 +00:00
|
|
|
|
|
|
|
|
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__'
|