Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for v4.1.0 - deprecated nested serializers #54

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions phonebox_plugin/api/nested_serializers.py

This file was deleted.

31 changes: 14 additions & 17 deletions phonebox_plugin/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@
from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator
from ..models import Number, VoiceCircuit
from tenancy.api.nested_serializers import NestedTenantSerializer
from dcim.api.nested_serializers import NestedRegionSerializer, NestedSiteSerializer
from circuits.api.nested_serializers import NestedProviderSerializer
from tenancy.api.serializers import TenantSerializer
from dcim.api.serializers import RegionSerializer, SiteSerializer
from circuits.api.serializers import ProviderSerializer
from extras.api.serializers import TagSerializer
from .nested_serializers import NestedNumberSerializer
from extras.api.nested_serializers import NestedTagSerializer
from netbox.api.fields import ContentTypeField
from netbox.constants import NESTED_SERIALIZER_PREFIX
from utilities.api import get_serializer_for_model
from ..choices import VOICE_CIRCUIT_ASSIGNMENT_MODELS


class NumberSerializer(TagSerializer, serializers.ModelSerializer):

label = serializers.CharField(source='number', read_only=True)
tenant = NestedTenantSerializer(required=True, allow_null=False)
region = NestedRegionSerializer(required=False, allow_null=True)
provider = NestedProviderSerializer(required=False, allow_null=True)
forward_to = NestedNumberSerializer(required=False, allow_null=True)
tags = NestedTagSerializer(many=True, required=False)
tenant = TenantSerializer(required=True, allow_null=False, nested=True)
region = RegionSerializer(required=False, allow_null=True, nested=True)
provider = ProviderSerializer(required=False, allow_null=True, nested=True)
forward_to = serializers.PrimaryKeyRelatedField(queryset=Number.objects.all(), required=False, allow_null=True)
tags = TagSerializer(many=True, required=False, nested=True)

class Meta:
model = Number
Expand All @@ -34,23 +31,23 @@ class Meta:
class VoiceCircuitSerializer(TagSerializer, serializers.ModelSerializer):

label = serializers.CharField(source='voice_circuit', read_only=True)
tenant = NestedTenantSerializer(required=True, allow_null=False)
region = NestedRegionSerializer(required=False, allow_null=True)
site = NestedSiteSerializer(required=False, allow_null=True)
provider = NestedProviderSerializer(required=False, allow_null=True)
tenant = TenantSerializer(required=True, allow_null=False, nested=True)
region = RegionSerializer(required=False, allow_null=True, nested=True)
site = SiteSerializer(required=False, allow_null=True, nested=True)
provider = ProviderSerializer(required=False, allow_null=True, nested=True)
assigned_object_type = ContentTypeField(
queryset=ContentType.objects.filter(VOICE_CIRCUIT_ASSIGNMENT_MODELS),
required=True,
allow_null=False
)
assigned_object = serializers.SerializerMethodField(read_only=True)
tags = NestedTagSerializer(many=True, required=False)
tags = TagSerializer(many=True, required=False, nested=True)

@extend_schema_field(serializers.JSONField(allow_null=True))
def get_assigned_object(self, obj):
if obj.assigned_object is None:
return None
serializer = get_serializer_for_model(obj.assigned_object, prefix=NESTED_SERIALIZER_PREFIX)
serializer = get_serializer_for_model(obj.assigned_object)
context = {'request': self.context['request']}
return serializer(obj.assigned_object, context=context).data

Expand Down