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

107 lines
3.7 KiB
Python
Raw Normal View History

2016-03-01 11:23:03 -05:00
from rest_framework import generics
2017-03-08 16:12:14 -05:00
from rest_framework.decorators import detail_route
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
2016-03-01 11:23:03 -05:00
2016-08-22 13:20:30 -04:00
from django.contrib.contenttypes.models import ContentType
from django.http import Http404, HttpResponse
2016-03-01 11:23:03 -05:00
from django.shortcuts import get_object_or_404
from circuits.models import Provider
2017-03-08 16:30:32 -05:00
from dcim.models import Site, Interface
2017-03-08 16:12:14 -05:00
from extras import filters
from extras.models import Graph, TopologyMap, GRAPH_TYPE_INTERFACE, GRAPH_TYPE_PROVIDER, GRAPH_TYPE_SITE, UserAction
2017-03-08 16:12:14 -05:00
from utilities.api import WritableSerializerMixin
from . import serializers
2016-03-01 11:23:03 -05:00
2017-01-24 17:12:16 -05:00
class CustomFieldModelViewSet(ModelViewSet):
2016-08-22 13:20:30 -04:00
"""
Include the applicable set of CustomFields in the ModelViewSet context.
2016-08-22 13:20:30 -04:00
"""
2017-01-24 17:12:16 -05:00
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')
2016-08-22 15:16:49 -04:00
# Cache all relevant CustomFieldChoices. This saves us from having to do a lookup per select field per object.
custom_field_choices = {}
2017-01-24 17:12:16 -05:00
for field in custom_fields:
2016-08-22 15:16:49 -04:00
for cfc in field.choices.all():
custom_field_choices[cfc.id] = cfc.value
2017-01-24 17:12:16 -05:00
custom_field_choices = custom_field_choices
context = super(CustomFieldModelViewSet, self).get_serializer_context()
context.update({
2017-01-24 17:12:16 -05:00
'custom_fields': custom_fields,
'custom_field_choices': custom_field_choices,
})
return context
2017-01-24 17:12:16 -05:00
def get_queryset(self):
# Prefetch custom field values
return super(CustomFieldModelViewSet, self).get_queryset().prefetch_related('custom_field_values__field')
2016-08-22 13:20:30 -04:00
2016-03-01 11:23:03 -05:00
class GraphListView(generics.ListAPIView):
"""
Returns a list of relevant graphs
"""
2017-03-08 16:12:14 -05:00
serializer_class = serializers.GraphSerializer
2016-03-01 11:23:03 -05:00
def get_serializer_context(self):
cls = {
GRAPH_TYPE_INTERFACE: Interface,
GRAPH_TYPE_PROVIDER: Provider,
GRAPH_TYPE_SITE: Site,
}
obj = get_object_or_404(cls[self.kwargs.get('type')], pk=self.kwargs['pk'])
2016-03-01 11:23:03 -05:00
context = super(GraphListView, self).get_serializer_context()
context.update({
'graphed_object': obj,
})
2016-03-01 11:23:03 -05:00
return context
def get_queryset(self):
graph_type = self.kwargs.get('type', None)
if not graph_type:
raise Http404()
queryset = Graph.objects.filter(type=graph_type)
return queryset
2017-03-08 16:12:14 -05:00
class TopologyMapViewSet(WritableSerializerMixin, ModelViewSet):
queryset = TopologyMap.objects.select_related('site')
serializer_class = serializers.TopologyMapSerializer
write_serializer_class = serializers.WritableTopologyMapSerializer
filter_class = filters.TopologyMapFilter
2017-03-08 16:12:14 -05:00
@detail_route()
def render(self, request, pk):
2017-03-08 16:12:14 -05:00
tmap = get_object_or_404(TopologyMap, pk=pk)
2017-03-08 16:30:32 -05:00
img_format = 'png'
2016-06-24 14:01:57 -04:00
try:
2017-03-08 16:30:32 -05:00
data = tmap.render(img_format=img_format)
2016-06-24 14:01:57 -04:00
except:
2017-03-08 16:12:14 -05:00
return HttpResponse(
"There was an error generating the requested graph. Ensure that the GraphViz executables have been "
"installed correctly."
)
2017-03-08 16:30:32 -05:00
response = HttpResponse(data, content_type='image/{}'.format(img_format))
response['Content-Disposition'] = 'inline; filename="{}.{}"'.format(tmap.slug, img_format)
return response
class RecentActivityViewSet(ReadOnlyModelViewSet):
"""
List all UserActions to provide a log of recent activity.
"""
queryset = UserAction.objects.all()
serializer_class = serializers.UserActionSerializer
filter_class = filters.UserActionFilter