diff --git a/netbox/dcim/api/views.py b/netbox/dcim/api/views.py index 4a528b647..4df89d55d 100644 --- a/netbox/dcim/api/views.py +++ b/netbox/dcim/api/views.py @@ -13,6 +13,7 @@ from rest_framework.viewsets import ViewSet from circuits.models import Circuit from dcim import filtersets +from dcim.constants import CABLE_TRACE_SVG_DEFAULT_WIDTH from dcim.models import * from extras.api.views import ConfigContextQuerySetMixin from ipam.models import Prefix, VLAN @@ -54,9 +55,9 @@ class PathEndpointMixin(object): if request.GET.get('render', None) == 'svg': # Render SVG try: - width = min(int(request.GET.get('width')), 1600) + width = int(request.GET.get('width', CABLE_TRACE_SVG_DEFAULT_WIDTH)) except (ValueError, TypeError): - width = None + width = CABLE_TRACE_SVG_DEFAULT_WIDTH drawing = obj.get_trace_svg( base_url=request.build_absolute_uri('/'), width=width diff --git a/netbox/dcim/constants.py b/netbox/dcim/constants.py index 68bbd1dbe..aa921ccc6 100644 --- a/netbox/dcim/constants.py +++ b/netbox/dcim/constants.py @@ -94,6 +94,8 @@ MODULAR_COMPONENT_MODELS = Q( # Cabling and connections # +CABLE_TRACE_SVG_DEFAULT_WIDTH = 400 + # Cable endpoint types CABLE_TERMINATION_MODELS = Q( Q(app_label='circuits', model__in=( diff --git a/netbox/dcim/models/device_components.py b/netbox/dcim/models/device_components.py index c2cd31a2a..72be19b10 100644 --- a/netbox/dcim/models/device_components.py +++ b/netbox/dcim/models/device_components.py @@ -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 list(zip(*[iter(path)] * 3)) - def get_trace_svg(self, base_url=None, width=None): - if width is not None: - trace = CableTraceSVG(self, base_url=base_url, width=width) - else: - trace = CableTraceSVG(self, base_url=base_url) + def get_trace_svg(self, base_url=None, width=CABLE_TRACE_SVG_DEFAULT_WIDTH): + trace = CableTraceSVG(self, base_url=base_url, width=width) return trace.render() @property diff --git a/netbox/dcim/svg/cables.py b/netbox/dcim/svg/cables.py index b9d7c42ed..f9c614b67 100644 --- a/netbox/dcim/svg/cables.py +++ b/netbox/dcim/svg/cables.py @@ -5,6 +5,7 @@ from svgwrite.text import Text from django.conf import settings +from dcim.constants import CABLE_TRACE_SVG_DEFAULT_WIDTH from utilities.utils import foreground_color @@ -115,7 +116,7 @@ class CableTraceSVG: :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. """ - 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.width = width self.base_url = base_url.rstrip('/') if base_url is not None else '' diff --git a/netbox/dcim/tests/test_cablepaths.py b/netbox/dcim/tests/test_cablepaths.py index 6d158bb10..e5b441c51 100644 --- a/netbox/dcim/tests/test_cablepaths.py +++ b/netbox/dcim/tests/test_cablepaths.py @@ -106,6 +106,9 @@ class CablePathTestCase(TestCase): self.assertPathIsSet(interface1, path1) self.assertPathIsSet(interface2, path2) + # Test SVG generation + interface1.get_trace_svg() + # Delete cable 1 cable1.delete() @@ -142,6 +145,9 @@ class CablePathTestCase(TestCase): self.assertPathIsSet(consoleport1, path1) self.assertPathIsSet(consoleserverport1, path2) + # Test SVG generation + consoleport1.get_trace_svg() + # Delete cable 1 cable1.delete() @@ -178,6 +184,9 @@ class CablePathTestCase(TestCase): self.assertPathIsSet(powerport1, path1) self.assertPathIsSet(poweroutlet1, path2) + # Test SVG generation + powerport1.get_trace_svg() + # Delete cable 1 cable1.delete() @@ -214,6 +223,9 @@ class CablePathTestCase(TestCase): self.assertPathIsSet(powerport1, path1) self.assertPathIsSet(powerfeed1, path2) + # Test SVG generation + powerport1.get_trace_svg() + # Delete cable 1 cable1.delete() @@ -254,6 +266,9 @@ class CablePathTestCase(TestCase): self.assertPathIsSet(interface2, path2) self.assertPathIsSet(interface3, path2) + # Test SVG generation + interface1.get_trace_svg() + # Delete cable 1 cable1.delete() @@ -303,6 +318,9 @@ class CablePathTestCase(TestCase): self.assertPathIsSet(interface3, path2) self.assertPathIsSet(interface4, path2) + # Test SVG generation + interface1.get_trace_svg() + # Delete cable 1 cable1.delete()