views.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from rest_framework.decorators import detail_route
  2. from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
  3. from django.contrib.contenttypes.models import ContentType
  4. from django.http import HttpResponse
  5. from django.shortcuts import get_object_or_404
  6. from extras import filters
  7. from extras.models import ExportTemplate, Graph, ImageAttachment, TopologyMap, UserAction
  8. from utilities.api import WritableSerializerMixin
  9. from . import serializers
  10. class CustomFieldModelViewSet(ModelViewSet):
  11. """
  12. Include the applicable set of CustomFields in the ModelViewSet context.
  13. """
  14. def get_serializer_context(self):
  15. # Gather all custom fields for the model
  16. content_type = ContentType.objects.get_for_model(self.queryset.model)
  17. custom_fields = content_type.custom_fields.prefetch_related('choices')
  18. # Cache all relevant CustomFieldChoices. This saves us from having to do a lookup per select field per object.
  19. custom_field_choices = {}
  20. for field in custom_fields:
  21. for cfc in field.choices.all():
  22. custom_field_choices[cfc.id] = cfc.value
  23. custom_field_choices = custom_field_choices
  24. context = super(CustomFieldModelViewSet, self).get_serializer_context()
  25. context.update({
  26. 'custom_fields': custom_fields,
  27. 'custom_field_choices': custom_field_choices,
  28. })
  29. return context
  30. def get_queryset(self):
  31. # Prefetch custom field values
  32. return super(CustomFieldModelViewSet, self).get_queryset().prefetch_related('custom_field_values__field')
  33. class GraphViewSet(WritableSerializerMixin, ModelViewSet):
  34. queryset = Graph.objects.all()
  35. serializer_class = serializers.GraphSerializer
  36. write_serializer_class = serializers.WritableGraphSerializer
  37. filter_class = filters.GraphFilter
  38. class ExportTemplateViewSet(WritableSerializerMixin, ModelViewSet):
  39. queryset = ExportTemplate.objects.all()
  40. serializer_class = serializers.ExportTemplateSerializer
  41. filter_class = filters.ExportTemplateFilter
  42. class TopologyMapViewSet(WritableSerializerMixin, ModelViewSet):
  43. queryset = TopologyMap.objects.select_related('site')
  44. serializer_class = serializers.TopologyMapSerializer
  45. write_serializer_class = serializers.WritableTopologyMapSerializer
  46. filter_class = filters.TopologyMapFilter
  47. @detail_route()
  48. def render(self, request, pk):
  49. tmap = get_object_or_404(TopologyMap, pk=pk)
  50. img_format = 'png'
  51. try:
  52. data = tmap.render(img_format=img_format)
  53. except:
  54. return HttpResponse(
  55. "There was an error generating the requested graph. Ensure that the GraphViz executables have been "
  56. "installed correctly."
  57. )
  58. response = HttpResponse(data, content_type='image/{}'.format(img_format))
  59. response['Content-Disposition'] = 'inline; filename="{}.{}"'.format(tmap.slug, img_format)
  60. return response
  61. class ImageAttachmentViewSet(WritableSerializerMixin, ModelViewSet):
  62. queryset = ImageAttachment.objects.all()
  63. serializer_class = serializers.ImageAttachmentSerializer
  64. write_serializer_class = serializers.WritableImageAttachmentSerializer
  65. class RecentActivityViewSet(ReadOnlyModelViewSet):
  66. """
  67. List all UserActions to provide a log of recent activity.
  68. """
  69. queryset = UserAction.objects.all()
  70. serializer_class = serializers.UserActionSerializer
  71. filter_class = filters.UserActionFilter