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

Initial work on API v2.0

This commit is contained in:
Jeremy Stretch
2017-01-24 17:12:16 -05:00
parent fbfa3cf619
commit 062a5bfe8d
14 changed files with 216 additions and 386 deletions

View File

@@ -27,6 +27,8 @@ class BINDZoneRenderer(renderers.BaseRenderer):
def render(self, data, media_type=None, renderer_context=None):
records = []
if not isinstance(data, (list, tuple)):
data = (data,)
for record in data:
if record.get('name') and record.get('primary_ip'):
try:

View File

@@ -12,22 +12,20 @@ class CustomFieldSerializer(serializers.Serializer):
def get_custom_fields(self, obj):
# Gather all CustomFields applicable to this object
fields = {cf.name: None for cf in self.context['view'].custom_fields}
fields = {cf.name: None for cf in self.context['custom_fields']}
custom_field_choices = self.context['custom_field_choices']
# Attach any defined CustomFieldValues to their respective CustomFields
for cfv in obj.custom_field_values.all():
# Attempt to suppress database lookups for CustomFieldChoices by using the cached choice set from the view
# context.
if cfv.field.type == CF_TYPE_SELECT and hasattr(self, 'custom_field_choices'):
if cfv.field.type == CF_TYPE_SELECT:
cfc = {
'id': int(cfv.serialized_value),
'value': self.context['view'].custom_field_choices[int(cfv.serialized_value)]
'value': 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

@@ -1,6 +1,7 @@
import graphviz
from rest_framework import generics
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q
@@ -14,22 +15,32 @@ from extras.models import Graph, TopologyMap, GRAPH_TYPE_INTERFACE, GRAPH_TYPE_P
from .serializers import GraphSerializer
class CustomFieldModelAPIView(object):
class CustomFieldModelViewSet(ModelViewSet):
"""
Include the applicable set of CustomField in the view context.
Include the applicable set of CustomField in the ModelViewSet context.
"""
def __init__(self):
super(CustomFieldModelAPIView, self).__init__()
self.content_type = ContentType.objects.get_for_model(self.queryset.model)
self.custom_fields = self.content_type.custom_fields.prefetch_related('choices')
def get_serializer_context(self):
# Gather all custom fields for the model
content_type = ContentType.objects.get_for_model(self.queryset.model)
custom_fields = content_type.custom_fields.prefetch_related('choices')
# Cache all relevant CustomFieldChoices. This saves us from having to do a lookup per select field per object.
custom_field_choices = {}
for field in self.custom_fields:
for field in custom_fields:
for cfc in field.choices.all():
custom_field_choices[cfc.id] = cfc.value
self.custom_field_choices = custom_field_choices
custom_field_choices = custom_field_choices
return {
'custom_fields': custom_fields,
'custom_field_choices': custom_field_choices,
}
def get_queryset(self):
# Prefetch custom field values
return super(CustomFieldModelViewSet, self).get_queryset().prefetch_related('custom_field_values__field')
class GraphListView(generics.ListAPIView):