diff --git a/apps/assets/api/asset/asset.py b/apps/assets/api/asset/asset.py index 0f66d4b9a..1e5d2a0b6 100644 --- a/apps/assets/api/asset/asset.py +++ b/apps/assets/api/asset/asset.py @@ -32,6 +32,7 @@ __all__ = [ class AssetFilterSet(BaseFilterSet): platform = django_filters.CharFilter(method='filter_platform') + exclude_platform = django_filters.CharFilter(field_name="platform__name", lookup_expr='exact', exclude=True) domain = django_filters.CharFilter(method='filter_domain') type = django_filters.CharFilter(field_name="platform__type", lookup_expr="exact") category = django_filters.CharFilter(field_name="platform__category", lookup_expr="exact") diff --git a/apps/assets/api/domain.py b/apps/assets/api/domain.py index 46b586458..14979036a 100644 --- a/apps/assets/api/domain.py +++ b/apps/assets/api/domain.py @@ -30,6 +30,10 @@ class DomainViewSet(OrgBulkModelViewSet): return serializers.DomainWithGatewaySerializer return super().get_serializer_class() + def partial_update(self, request, *args, **kwargs): + kwargs['partial'] = True + return self.update(request, *args, **kwargs) + class GatewayViewSet(HostViewSet): perm_model = Gateway diff --git a/apps/assets/serializers/domain.py b/apps/assets/serializers/domain.py index 9138e23f9..4224e3983 100644 --- a/apps/assets/serializers/domain.py +++ b/apps/assets/serializers/domain.py @@ -1,12 +1,13 @@ # -*- coding: utf-8 -*- # -from django.db.models import Count +from django.db.models import Count, Q from django.utils.translation import gettext_lazy as _ from rest_framework import serializers from common.serializers import ResourceLabelsMixin from common.serializers.fields import ObjectRelatedField from orgs.mixins.serializers import BulkOrgResourceModelSerializer +from assets.models.gateway import Gateway from .gateway import GatewayWithAccountSecretSerializer from ..models import Domain @@ -15,7 +16,7 @@ __all__ = ['DomainSerializer', 'DomainWithGatewaySerializer', 'DomainListSeriali class DomainSerializer(ResourceLabelsMixin, BulkOrgResourceModelSerializer): gateways = ObjectRelatedField( - many=True, required=False, label=_('Gateway'), read_only=True, + many=True, required=False, label=_('Gateway'), queryset=Gateway.objects ) class Meta: @@ -38,12 +39,17 @@ class DomainSerializer(ResourceLabelsMixin, BulkOrgResourceModelSerializer): data['assets'] = [i for i in assets if str(i['id']) not in gateway_ids] return data - def update(self, instance, validated_data): + def create(self, validated_data): assets = validated_data.pop('assets', []) - assets = assets + list(instance.gateways) - validated_data['assets'] = assets - instance = super().update(instance, validated_data) - return instance + 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) @classmethod def setup_eager_loading(cls, queryset): @@ -61,7 +67,7 @@ class DomainListSerializer(DomainSerializer): @classmethod def setup_eager_loading(cls, queryset): queryset = queryset.annotate( - assets_amount=Count('assets', distinct=True), + assets_amount=Count('assets', filter=~Q(assets__platform__name='Gateway'), distinct=True), ) return queryset