v0.2.1: rework PNG export to draw directly on canvas, like netbox-topology-views

Investigated how MrBlake/mrb-netbox-topology-views does its PNG export:
it renders its topology natively onto a canvas (vis-network draws
there directly) and just calls canvas.toDataURL() - no SVG round-trip
at all. Applied the same principle here.

Previous approach loaded the standalone SVG export via <img src="blob:...">
and rasterized that onto a canvas - fragile, because the whole export
failed if that SVG document wasn't strictly valid XML (as our leaked
{# #} comment bug demonstrated) or if a single device type image
reference inside it couldn't be resolved.

New approach: a dedicated export/data/ endpoint returns the elevation's
plain pixel geometry as JSON (positions, sizes, colors, labels, image
URLs - the same numbers the SVG is built from), and export.js draws
that directly onto a <canvas> with native 2D primitives (fillRect,
strokeRect, fillText/strokeText, drawImage). Device type images are
fetched individually as same-origin blobs before drawing; one image
failing to load only drops that picture; the rest of the export still
succeeds. Added a combined front+rear PNG option.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-16 12:53:42 +02:00
co-authored by Claude Sonnet 5
parent 3420d84803
commit 67d999e234
7 changed files with 274 additions and 61 deletions
+55
View File
@@ -89,6 +89,61 @@ class RackConceptExportSVGView(ObjectPermissionRequiredMixin, View):
return response
@register_model_view(RackConcept, 'export_data', path='export/data')
class RackConceptExportDataView(ObjectPermissionRequiredMixin, View):
"""
The elevation's full pixel geometry as JSON, for the PNG export: it draws straight
onto a <canvas> from this rather than rasterizing an SVG image, so a device type
image only ever reaches the canvas via a same-origin fetch()->blob()->Image, never
through an <img src="blob:svg..."> whose validity (and therefore whether it even
decodes) depends on the SVG being perfectly well-formed.
"""
queryset = RackConcept.objects.all()
def get_required_permission(self):
return 'netbox_rack_concept.view_rackconcept'
def get(self, request, pk):
concept = get_object_or_404(self.queryset.restrict(request.user, 'view'), pk=pk)
front, rear = get_elevations(concept)
def serialize(elevation):
return {
'width': elevation.width,
'height': elevation.height,
'rack_x': elevation.rack_x,
'rack_width': elevation.rack_width,
'face_label': elevation.face_label.title(),
'units': [
{'number': unit.number, 'y': unit.y, 'height': unit.height}
for unit in elevation.units
],
'blocks': [
{
'x': block.x,
'y': block.y,
'width': block.width,
'height': block.height,
'color': block.color,
'text_color': block.text_color,
'label': block.label,
'opposite': block.opposite,
'show_text': block.show_text,
'image_url': (
request.build_absolute_uri(block.image.url) if block.image else None
),
}
for block in elevation.blocks
],
}
return JsonResponse({
'name': concept.name,
'front': serialize(front),
'rear': serialize(rear),
})
@register_model_view(RackConcept, 'export_drawio', path='export/drawio')
class RackConceptExportDrawioView(ObjectPermissionRequiredMixin, View):
"""Download both faces of a concept as one draw.io (diagrams.net) diagram."""