1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fixed API tests

This commit is contained in:
Jeremy Stretch
2016-08-22 17:15:20 -04:00
parent b9dcf9ca12
commit 3b36a35b9a
5 changed files with 24 additions and 8 deletions

View File

@ -93,7 +93,7 @@ class RackDetailSerializer(RackSerializer):
class Meta(RackSerializer.Meta):
fields = ['id', 'name', 'facility_id', 'display_name', 'site', 'group', 'tenant', 'role', 'type', 'width',
'u_height', 'comments', 'front_units', 'rear_units']
'u_height', 'comments', 'custom_fields', 'front_units', 'rear_units']
def get_front_units(self, obj):
units = obj.get_rack_units(face=RACK_FACE_FRONT)

View File

@ -5,6 +5,7 @@ from rest_framework.settings import api_settings
from rest_framework.views import APIView
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.http import Http404
from django.shortcuts import get_object_or_404
@ -430,6 +431,13 @@ class RelatedConnectionsView(APIView):
Retrieve all connections related to a given console/power/interface connection
"""
def __init__(self):
super(RelatedConnectionsView, self).__init__()
# Custom fields
self.content_type = ContentType.objects.get_for_model(Device)
self.custom_fields = self.content_type.custom_fields.prefetch_related('choices')
def get(self, request):
peer_device = request.GET.get('peer-device')
@ -454,7 +462,7 @@ class RelatedConnectionsView(APIView):
# Initialize response skeleton
response = {
'device': serializers.DeviceSerializer(device).data,
'device': serializers.DeviceSerializer(device, context={'view': self}).data,
'console-ports': [],
'power-ports': [],
'interfaces': [],

View File

@ -21,6 +21,7 @@ class SiteTest(APITestCase):
'physical_address',
'shipping_address',
'comments',
'custom_fields',
'count_prefixes',
'count_vlans',
'count_racks',
@ -46,7 +47,8 @@ class SiteTest(APITestCase):
'type',
'width',
'u_height',
'comments'
'comments',
'custom_fields',
]
graph_fields = [
@ -125,7 +127,8 @@ class RackTest(APITestCase):
'type',
'width',
'u_height',
'comments'
'comments',
'custom_fields',
]
detail_fields = [
@ -141,6 +144,7 @@ class RackTest(APITestCase):
'width',
'u_height',
'comments',
'custom_fields',
'front_units',
'rear_units'
]
@ -337,6 +341,7 @@ class DeviceTest(APITestCase):
'primary_ip4',
'primary_ip6',
'comments',
'custom_fields',
]
nested_fields = ['id', 'name', 'display_name']

View File

@ -1,6 +1,6 @@
from rest_framework import serializers
from extras.models import CF_TYPE_SELECT, CustomFieldChoice, CustomFieldValue, Graph
from extras.models import CF_TYPE_SELECT, CustomFieldChoice, Graph
class CustomFieldSerializer(serializers.Serializer):
@ -17,14 +17,17 @@ class CustomFieldSerializer(serializers.Serializer):
# Attach any defined CustomFieldValues to their respective CustomFields
for cfv in obj.custom_field_values.all():
# Suppress database lookups for CustomFieldChoices. Instead, use the cached choice set from the view
# Attempt to suppress database lookups for CustomFieldChoices by using the cached choice set from the view
# context.
if cfv.field.type == CF_TYPE_SELECT:
if cfv.field.type == CF_TYPE_SELECT and hasattr(self, 'custom_field_choices'):
cfc = {
'id': int(cfv.serialized_value),
'value': self.context['view'].custom_field_choices[int(cfv.serialized_value)]
}
fields[cfv.field.name] = CustomFieldChoiceSerializer(instance=cfc).data
# Fall back to hitting the database in case we're in a view that doesn't inherit CustomFieldModelAPIView.
elif cfv.field.type == CF_TYPE_SELECT:
fields[cfv.field.name] = CustomFieldChoiceSerializer(instance=cfv.value).data
else:
fields[cfv.field.name] = cfv.value

View File

@ -9,7 +9,7 @@ from django.shortcuts import get_object_or_404
from circuits.models import Provider
from dcim.models import Site, Device, Interface, InterfaceConnection
from extras.models import CustomFieldChoice, Graph, TopologyMap, GRAPH_TYPE_INTERFACE, GRAPH_TYPE_PROVIDER, GRAPH_TYPE_SITE
from extras.models import Graph, TopologyMap, GRAPH_TYPE_INTERFACE, GRAPH_TYPE_PROVIDER, GRAPH_TYPE_SITE
from .serializers import GraphSerializer