Closes #4147: Use absolute URLs in rack elevation SVG renderings

This commit is contained in:
Jeremy Stretch 2020-04-03 13:16:35 -04:00
parent 1f3a21ba20
commit 721368ea8d
4 changed files with 15 additions and 6 deletions

View File

@ -5,6 +5,7 @@
### Enhancements ### Enhancements
* [#3676](https://github.com/netbox-community/netbox/issues/3676) - Reference VRF by name rather than RD during IP/prefix import * [#3676](https://github.com/netbox-community/netbox/issues/3676) - Reference VRF by name rather than RD during IP/prefix import
* [#4147](https://github.com/netbox-community/netbox/issues/4147) - Use absolute URLs in rack elevation SVG renderings
### Bug Fixes ### Bug Fixes

View File

@ -225,7 +225,8 @@ class RackViewSet(CustomFieldModelViewSet):
unit_width=data['unit_width'], unit_width=data['unit_width'],
unit_height=data['unit_height'], unit_height=data['unit_height'],
legend_width=data['legend_width'], legend_width=data['legend_width'],
include_images=data['include_images'] include_images=data['include_images'],
base_url=request.build_absolute_uri('/')
) )
return HttpResponse(drawing.tostring(), content_type='image/svg+xml') return HttpResponse(drawing.tostring(), content_type='image/svg+xml')

View File

@ -15,10 +15,15 @@ class RackElevationSVG:
:param rack: A NetBox Rack instance :param rack: A NetBox Rack instance
:param include_images: If true, the SVG document will embed front/rear device face images, where available :param include_images: If true, the SVG document will embed front/rear device face images, where available
:param base_url: Base URL for links within the SVG document. If none, links will be relative.
""" """
def __init__(self, rack, include_images=True): def __init__(self, rack, include_images=True, base_url=None):
self.rack = rack self.rack = rack
self.include_images = include_images self.include_images = include_images
if base_url is not None:
self.base_url = base_url.rstrip('/')
else:
self.base_url = ''
def _get_device_description(self, device): def _get_device_description(self, device):
return '{} ({}) — {} ({}U) {} {}'.format( return '{} ({}) — {} ({}U) {} {}'.format(
@ -69,7 +74,7 @@ class RackElevationSVG:
color = device.device_role.color color = device.device_role.color
link = drawing.add( link = drawing.add(
drawing.a( drawing.a(
href=reverse('dcim:device', kwargs={'pk': device.pk}), href='{}{}'.format(self.base_url, reverse('dcim:device', kwargs={'pk': device.pk})),
target='_top', target='_top',
fill='black' fill='black'
) )
@ -81,7 +86,7 @@ class RackElevationSVG:
# Embed front device type image if one exists # Embed front device type image if one exists
if self.include_images and device.device_type.front_image: if self.include_images and device.device_type.front_image:
url = device.device_type.front_image.url url = '{}{}'.format(self.base_url, device.device_type.front_image.url)
image = drawing.image(href=url, insert=start, size=end, class_='device-image') image = drawing.image(href=url, insert=start, size=end, class_='device-image')
image.fit(scale='slice') image.fit(scale='slice')
link.add(image) link.add(image)

View File

@ -682,7 +682,8 @@ class Rack(ChangeLoggedModel, CustomFieldModel):
unit_width=RACK_ELEVATION_UNIT_WIDTH_DEFAULT, unit_width=RACK_ELEVATION_UNIT_WIDTH_DEFAULT,
unit_height=RACK_ELEVATION_UNIT_HEIGHT_DEFAULT, unit_height=RACK_ELEVATION_UNIT_HEIGHT_DEFAULT,
legend_width=RACK_ELEVATION_LEGEND_WIDTH_DEFAULT, legend_width=RACK_ELEVATION_LEGEND_WIDTH_DEFAULT,
include_images=True include_images=True,
base_url=None
): ):
""" """
Return an SVG of the rack elevation Return an SVG of the rack elevation
@ -693,8 +694,9 @@ class Rack(ChangeLoggedModel, CustomFieldModel):
height of the elevation height of the elevation
:param legend_width: Width of the unit legend, in pixels :param legend_width: Width of the unit legend, in pixels
:param include_images: Embed front/rear device images where available :param include_images: Embed front/rear device images where available
:param base_url: Base URL for links and images. If none, URLs will be relative.
""" """
elevation = RackElevationSVG(self, include_images=include_images) elevation = RackElevationSVG(self, include_images=include_images, base_url=base_url)
return elevation.render(face, unit_width, unit_height, legend_width) return elevation.render(face, unit_width, unit_height, legend_width)