Просмотр исходного кода

Closes #2034: Include the ID when showing nested interface connections

Jeremy Stretch 7 лет назад
Родитель
Сommit
b0985ebd42
1 измененных файлов с 20 добавлено и 4 удалено
  1. 20 4
      netbox/dcim/api/serializers.py

+ 20 - 4
netbox/dcim/api/serializers.py

@@ -609,10 +609,11 @@ class InterfaceSerializer(ValidatedModelSerializer):
 
     def get_interface_connection(self, obj):
         if obj.connection:
-            return OrderedDict((
-                ('interface', NestedInterfaceSerializer(obj.connected_interface, context=self.context).data),
-                ('status', obj.connection.connection_status),
-            ))
+            context = {
+                'request': self.context['request'],
+                'interface': obj.connected_interface,
+            }
+            return ContextualInterfaceConnectionSerializer(obj.connection, context=context).data
         return None
 
 
@@ -677,6 +678,21 @@ class NestedInterfaceConnectionSerializer(WritableNestedSerializer):
         fields = ['id', 'url', 'connection_status']
 
 
+class ContextualInterfaceConnectionSerializer(serializers.ModelSerializer):
+    """
+    A read-only representation of an InterfaceConnection from the perspective of either of its two connected Interfaces.
+    """
+    interface = serializers.SerializerMethodField(read_only=True)
+    connection_status = ChoiceFieldSerializer(choices=CONNECTION_STATUS_CHOICES, read_only=True)
+
+    class Meta:
+        model = InterfaceConnection
+        fields = ['id', 'interface', 'connection_status']
+
+    def get_interface(self, obj):
+        return NestedInterfaceSerializer(self.context['interface'], context=self.context).data
+
+
 #
 # Virtual chassis
 #