Temporary work to swap in serializer

This commit is contained in:
Daniel Sheppard 2024-08-25 15:27:27 -05:00
parent eb4ef18d61
commit c4fafa27ad
3 changed files with 25 additions and 38 deletions

View File

@ -16,40 +16,10 @@ __all__ = (
'CableSerializer', 'CableSerializer',
'CableTerminationSerializer', 'CableTerminationSerializer',
'CabledObjectSerializer', 'CabledObjectSerializer',
'GenericObjectTerminationSerializer',
'TracedCableSerializer', 'TracedCableSerializer',
) )
class GenericObjectTerminationSerializer(GenericObjectSerializer):
id = serializers.IntegerField(required=False)
def to_representation(self, instance):
data = super().to_representation(instance)
termination = instance.cable_terminations.first()
if termination:
data['id'] = termination.pk
return data
class CableSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='dcim-api:cable-detail')
a_terminations = GenericObjectTerminationSerializer(many=True, required=False)
b_terminations = GenericObjectTerminationSerializer(many=True, required=False)
status = ChoiceField(choices=LinkStatusChoices, required=False)
tenant = TenantSerializer(nested=True, required=False, allow_null=True)
length_unit = ChoiceField(choices=CableLengthUnitChoices, allow_blank=True, required=False, allow_null=True)
class Meta:
model = Cable
fields = [
'id', 'url', 'display', 'type', 'a_terminations', 'b_terminations', 'status', 'tenant', 'label', 'color',
'length', 'length_unit', 'description', 'comments', 'tags', 'custom_fields', 'created', 'last_updated',
]
brief_fields = ('id', 'url', 'display', 'label', 'description')
class TracedCableSerializer(serializers.ModelSerializer): class TracedCableSerializer(serializers.ModelSerializer):
""" """
Used only while tracing a cable path. Used only while tracing a cable path.
@ -84,6 +54,29 @@ class CableTerminationSerializer(NetBoxModelSerializer):
return serializer(obj.termination, nested=True, context=context).data return serializer(obj.termination, nested=True, context=context).data
class CableSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='dcim-api:cable-detail')
a_terminations = GenericObjectSerializer(many=True, required=False)
b_terminations = GenericObjectSerializer(many=True, required=False)
terminations = CableTerminationSerializer(
many=True,
required=False,
nested=True,
fields=('id', 'url', 'cable_end', 'termination_type', 'termination_id', 'termination')
)
status = ChoiceField(choices=LinkStatusChoices, required=False)
tenant = TenantSerializer(nested=True, required=False, allow_null=True)
length_unit = ChoiceField(choices=CableLengthUnitChoices, allow_blank=True, required=False, allow_null=True)
class Meta:
model = Cable
fields = [
'id', 'url', 'display', 'type', 'a_terminations', 'b_terminations', 'terminations', 'status', 'tenant', 'label', 'color',
'length', 'length_unit', 'description', 'comments', 'tags', 'custom_fields', 'created', 'last_updated',
]
brief_fields = ('id', 'url', 'display', 'label', 'description')
class CablePathSerializer(serializers.ModelSerializer): class CablePathSerializer(serializers.ModelSerializer):
path = serializers.SerializerMethodField(read_only=True) path = serializers.SerializerMethodField(read_only=True)

View File

@ -2038,14 +2038,6 @@ class CableTest(APIViewTestCases.APIViewTestCase):
}, },
] ]
def test_cable_termination_pk_exist(self):
self.add_permissions('dcim.view_cable')
cable = Cable.objects.first()
url = reverse('dcim-api:cable-detail', kwargs={'pk': cable.pk})
response = self.client.get(url, {}, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_200_OK)
self.assertIsNotNone(response.data['a_terminations'][0]['id'])
class ConnectedDeviceTest(APITestCase): class ConnectedDeviceTest(APITestCase):

View File

@ -13,6 +13,8 @@ __all__ = (
class GenericObjectSerializer(serializers.Serializer): class GenericObjectSerializer(serializers.Serializer):
id = serializers.IntegerField(required=False)
""" """
Minimal representation of some generic object identified by ContentType and PK. Minimal representation of some generic object identified by ContentType and PK.
""" """