2018-03-23 11:46:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2024-03-26 02:21:44 +00:00
|
|
|
from django.db.models import Count, Q
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2023-01-16 11:02:09 +00:00
|
|
|
from rest_framework import serializers
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2024-05-21 06:44:48 +00:00
|
|
|
from assets.models.gateway import Gateway
|
2023-12-05 03:16:34 +00:00
|
|
|
from common.serializers import ResourceLabelsMixin
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.serializers.fields import ObjectRelatedField
|
2022-11-29 09:01:03 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2023-01-16 11:02:09 +00:00
|
|
|
from .gateway import GatewayWithAccountSecretSerializer
|
2024-01-02 08:11:56 +00:00
|
|
|
from ..models import Domain
|
2022-12-06 03:03:14 +00:00
|
|
|
|
2024-01-02 08:11:56 +00:00
|
|
|
__all__ = ['DomainSerializer', 'DomainWithGatewaySerializer', 'DomainListSerializer']
|
2018-03-23 11:46:46 +00:00
|
|
|
|
|
|
|
|
2023-12-05 03:16:34 +00:00
|
|
|
class DomainSerializer(ResourceLabelsMixin, BulkOrgResourceModelSerializer):
|
2022-12-06 03:03:14 +00:00
|
|
|
gateways = ObjectRelatedField(
|
2024-05-21 06:44:48 +00:00
|
|
|
many=True, required=False, label=_('Gateway'), queryset=Gateway.objects,
|
|
|
|
help_text=_(
|
|
|
|
"A gateway is a network proxy for a zone, and when connecting assets within the zone, "
|
|
|
|
"the connection is routed through the gateway.")
|
2022-12-06 03:03:14 +00:00
|
|
|
)
|
2024-02-01 08:02:31 +00:00
|
|
|
assets_amount = serializers.IntegerField(label=_('Assets amount'), read_only=True)
|
2018-03-23 11:46:46 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Domain
|
2020-06-16 09:55:18 +00:00
|
|
|
fields_mini = ['id', 'name']
|
2022-11-23 10:36:42 +00:00
|
|
|
fields_small = fields_mini + ['comment']
|
2024-02-01 08:02:31 +00:00
|
|
|
fields_m2m = ['assets', 'gateways', 'assets_amount']
|
2022-12-06 03:03:14 +00:00
|
|
|
read_only_fields = ['date_created']
|
2022-11-23 10:36:42 +00:00
|
|
|
fields = fields_small + fields_m2m + read_only_fields
|
2024-03-07 03:20:40 +00:00
|
|
|
extra_kwargs = {
|
2024-06-17 11:22:45 +00:00
|
|
|
'assets': {'required': False, 'label': _('Assets')},
|
2024-03-07 03:20:40 +00:00
|
|
|
}
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
def to_representation(self, instance):
|
|
|
|
data = super().to_representation(instance)
|
2024-01-02 08:11:56 +00:00
|
|
|
assets = data.get('assets')
|
|
|
|
if assets is None:
|
|
|
|
return data
|
2023-01-16 11:02:09 +00:00
|
|
|
gateway_ids = [str(i['id']) for i in data['gateways']]
|
|
|
|
data['assets'] = [i for i in assets if str(i['id']) not in gateway_ids]
|
|
|
|
return data
|
2018-03-25 13:47:29 +00:00
|
|
|
|
2024-03-26 02:21:44 +00:00
|
|
|
def create(self, validated_data):
|
2023-02-06 06:54:30 +00:00
|
|
|
assets = validated_data.pop('assets', [])
|
2024-03-26 02:21:44 +00:00
|
|
|
gateways = validated_data.pop('gateways', [])
|
|
|
|
validated_data['assets'] = assets + gateways
|
|
|
|
return super().create(validated_data)
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
assets = validated_data.pop('assets', list(instance.assets.all()))
|
|
|
|
gateways = validated_data.pop('gateways', list(instance.gateways.all()))
|
|
|
|
validated_data['assets'] = assets + gateways
|
|
|
|
return super().update(instance, validated_data)
|
2023-02-06 06:54:30 +00:00
|
|
|
|
2023-12-05 03:16:34 +00:00
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
queryset = queryset \
|
2024-02-01 08:02:31 +00:00
|
|
|
.annotate(assets_amount=Count('assets')) \
|
2023-12-05 03:16:34 +00:00
|
|
|
.prefetch_related('labels', 'labels__label')
|
|
|
|
return queryset
|
|
|
|
|
2018-03-25 13:47:29 +00:00
|
|
|
|
2024-01-02 08:11:56 +00:00
|
|
|
class DomainListSerializer(DomainSerializer):
|
|
|
|
class Meta(DomainSerializer.Meta):
|
|
|
|
fields = list(set(DomainSerializer.Meta.fields + ['assets_amount']) - {'assets'})
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
queryset = queryset.annotate(
|
2024-03-26 02:21:44 +00:00
|
|
|
assets_amount=Count('assets', filter=~Q(assets__platform__name='Gateway'), distinct=True),
|
2024-01-02 08:11:56 +00:00
|
|
|
)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
class DomainWithGatewaySerializer(serializers.ModelSerializer):
|
|
|
|
gateways = GatewayWithAccountSecretSerializer(many=True, read_only=True)
|
2018-03-25 13:47:29 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Domain
|
2018-03-23 11:46:46 +00:00
|
|
|
fields = '__all__'
|