Add SVG rendering to cable path tests

This commit is contained in:
jeremystretch 2022-06-27 15:53:34 -04:00
parent 9d35a9d912
commit 8bbc592261
5 changed files with 27 additions and 8 deletions

View File

@ -13,6 +13,7 @@ from rest_framework.viewsets import ViewSet
from circuits.models import Circuit from circuits.models import Circuit
from dcim import filtersets from dcim import filtersets
from dcim.constants import CABLE_TRACE_SVG_DEFAULT_WIDTH
from dcim.models import * from dcim.models import *
from extras.api.views import ConfigContextQuerySetMixin from extras.api.views import ConfigContextQuerySetMixin
from ipam.models import Prefix, VLAN from ipam.models import Prefix, VLAN
@ -54,9 +55,9 @@ class PathEndpointMixin(object):
if request.GET.get('render', None) == 'svg': if request.GET.get('render', None) == 'svg':
# Render SVG # Render SVG
try: try:
width = min(int(request.GET.get('width')), 1600) width = int(request.GET.get('width', CABLE_TRACE_SVG_DEFAULT_WIDTH))
except (ValueError, TypeError): except (ValueError, TypeError):
width = None width = CABLE_TRACE_SVG_DEFAULT_WIDTH
drawing = obj.get_trace_svg( drawing = obj.get_trace_svg(
base_url=request.build_absolute_uri('/'), base_url=request.build_absolute_uri('/'),
width=width width=width

View File

@ -94,6 +94,8 @@ MODULAR_COMPONENT_MODELS = Q(
# Cabling and connections # Cabling and connections
# #
CABLE_TRACE_SVG_DEFAULT_WIDTH = 400
# Cable endpoint types # Cable endpoint types
CABLE_TERMINATION_MODELS = Q( CABLE_TERMINATION_MODELS = Q(
Q(app_label='circuits', model__in=( Q(app_label='circuits', model__in=(

View File

@ -215,11 +215,8 @@ class PathEndpoint(models.Model):
# Return the path as a list of three-tuples (A termination(s), cable(s), B termination(s)) # Return the path as a list of three-tuples (A termination(s), cable(s), B termination(s))
return list(zip(*[iter(path)] * 3)) return list(zip(*[iter(path)] * 3))
def get_trace_svg(self, base_url=None, width=None): def get_trace_svg(self, base_url=None, width=CABLE_TRACE_SVG_DEFAULT_WIDTH):
if width is not None: trace = CableTraceSVG(self, base_url=base_url, width=width)
trace = CableTraceSVG(self, base_url=base_url, width=width)
else:
trace = CableTraceSVG(self, base_url=base_url)
return trace.render() return trace.render()
@property @property

View File

@ -5,6 +5,7 @@ from svgwrite.text import Text
from django.conf import settings from django.conf import settings
from dcim.constants import CABLE_TRACE_SVG_DEFAULT_WIDTH
from utilities.utils import foreground_color from utilities.utils import foreground_color
@ -115,7 +116,7 @@ class CableTraceSVG:
:param width: Width of the generated image (in pixels) :param width: Width of the generated image (in pixels)
:param base_url: Base URL for links within the SVG document. If none, links will be relative. :param base_url: Base URL for links within the SVG document. If none, links will be relative.
""" """
def __init__(self, origin, width=400, base_url=None): def __init__(self, origin, width=CABLE_TRACE_SVG_DEFAULT_WIDTH, base_url=None):
self.origin = origin self.origin = origin
self.width = width self.width = width
self.base_url = base_url.rstrip('/') if base_url is not None else '' self.base_url = base_url.rstrip('/') if base_url is not None else ''

View File

@ -106,6 +106,9 @@ class CablePathTestCase(TestCase):
self.assertPathIsSet(interface1, path1) self.assertPathIsSet(interface1, path1)
self.assertPathIsSet(interface2, path2) self.assertPathIsSet(interface2, path2)
# Test SVG generation
interface1.get_trace_svg()
# Delete cable 1 # Delete cable 1
cable1.delete() cable1.delete()
@ -142,6 +145,9 @@ class CablePathTestCase(TestCase):
self.assertPathIsSet(consoleport1, path1) self.assertPathIsSet(consoleport1, path1)
self.assertPathIsSet(consoleserverport1, path2) self.assertPathIsSet(consoleserverport1, path2)
# Test SVG generation
consoleport1.get_trace_svg()
# Delete cable 1 # Delete cable 1
cable1.delete() cable1.delete()
@ -178,6 +184,9 @@ class CablePathTestCase(TestCase):
self.assertPathIsSet(powerport1, path1) self.assertPathIsSet(powerport1, path1)
self.assertPathIsSet(poweroutlet1, path2) self.assertPathIsSet(poweroutlet1, path2)
# Test SVG generation
powerport1.get_trace_svg()
# Delete cable 1 # Delete cable 1
cable1.delete() cable1.delete()
@ -214,6 +223,9 @@ class CablePathTestCase(TestCase):
self.assertPathIsSet(powerport1, path1) self.assertPathIsSet(powerport1, path1)
self.assertPathIsSet(powerfeed1, path2) self.assertPathIsSet(powerfeed1, path2)
# Test SVG generation
powerport1.get_trace_svg()
# Delete cable 1 # Delete cable 1
cable1.delete() cable1.delete()
@ -254,6 +266,9 @@ class CablePathTestCase(TestCase):
self.assertPathIsSet(interface2, path2) self.assertPathIsSet(interface2, path2)
self.assertPathIsSet(interface3, path2) self.assertPathIsSet(interface3, path2)
# Test SVG generation
interface1.get_trace_svg()
# Delete cable 1 # Delete cable 1
cable1.delete() cable1.delete()
@ -303,6 +318,9 @@ class CablePathTestCase(TestCase):
self.assertPathIsSet(interface3, path2) self.assertPathIsSet(interface3, path2)
self.assertPathIsSet(interface4, path2) self.assertPathIsSet(interface4, path2)
# Test SVG generation
interface1.get_trace_svg()
# Delete cable 1 # Delete cable 1
cable1.delete() cable1.delete()