This commit is contained in:
Jeremy Stretch 2017-03-08 16:30:32 -05:00
parent a6ceaf8d96
commit c19725506d
2 changed files with 7 additions and 9 deletions

View File

@ -1,15 +1,13 @@
import graphviz
from rest_framework import generics from rest_framework import generics
from rest_framework.decorators import detail_route from rest_framework.decorators import detail_route
from rest_framework.viewsets import ModelViewSet from rest_framework.viewsets import ModelViewSet
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db.models import Q
from django.http import Http404, HttpResponse from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from circuits.models import Provider from circuits.models import Provider
from dcim.models import Site, Device, Interface, InterfaceConnection from dcim.models import Site, Interface
from extras import filters from extras import filters
from extras.models import 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 utilities.api import WritableSerializerMixin from utilities.api import WritableSerializerMixin
@ -83,17 +81,17 @@ class TopologyMapViewSet(WritableSerializerMixin, ModelViewSet):
def render(self, request, pk): def render(self, request, pk):
tmap = get_object_or_404(TopologyMap, pk=pk) tmap = get_object_or_404(TopologyMap, pk=pk)
format = 'png' img_format = 'png'
try: try:
data = tmap.render(format=format) data = tmap.render(img_format=img_format)
except: except:
return HttpResponse( return HttpResponse(
"There was an error generating the requested graph. Ensure that the GraphViz executables have been " "There was an error generating the requested graph. Ensure that the GraphViz executables have been "
"installed correctly." "installed correctly."
) )
response = HttpResponse(data, content_type='image/{}'.format(format)) response = HttpResponse(data, content_type='image/{}'.format(img_format))
response['Content-Disposition'] = 'inline; filename="{}.{}"'.format(tmap.slug, format) response['Content-Disposition'] = 'inline; filename="{}.{}"'.format(tmap.slug, img_format)
return response return response

View File

@ -312,7 +312,7 @@ class TopologyMap(models.Model):
return None return None
return [line.strip() for line in self.device_patterns.split('\n')] return [line.strip() for line in self.device_patterns.split('\n')]
def render(self, format='png'): def render(self, img_format='png'):
from dcim.models import Device, InterfaceConnection from dcim.models import Device, InterfaceConnection
@ -356,7 +356,7 @@ class TopologyMap(models.Model):
for c in connections: for c in connections:
graph.edge(c.interface_a.device.name, c.interface_b.device.name) graph.edge(c.interface_a.device.name, c.interface_b.device.name)
return graph.pipe(format=format) return graph.pipe(format=img_format)
# #