Show clickable floor plan previews on location pages

Render a read-only SVG preview of each floor plan server-side and add
it as a panel to the location detail page; clicking a preview opens
the floor plan. Bump version to 0.1.2.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-11-04 12:53:42 +01:00
1 parent 6b47b0fd5b
commit 805aedba52
10 files changed
+162 -6

No files matched your search

+1
View File
@@ -106,6 +106,7 @@ sudo systemctl restart netbox netbox-rq
- **Shape library:** wall, door (with swing arc), window, area/zone, column, cooling unit, power/UPS and free text labels, each with an editable colour. - **Shape library:** wall, door (with swing arc), window, area/zone, column, cooling unit, power/UPS and free text labels, each with an editable colour.
- **NetBox integration:** - **NetBox integration:**
- The palette lists the site's racks (filtered by location) and its non-racked devices, such as access points and cameras. Objects already placed are marked and can only be placed once. - The palette lists the site's racks (filtered by location) and its non-racked devices, such as access points and cameras. Objects already placed are marked and can only be placed once.
- Clickable floor plan previews on Location pages. Clicking a preview opens the floor plan.
- A **Floor Plans** tab on Site and Location pages, with a pre-filled "Add floor plan" button. - A **Floor Plans** tab on Site and Location pages, with a pre-filled "Add floor plan" button.
- A **Show on floor plan** button on Rack and Device pages that opens the plan, zooms to the object and highlights it. - A **Show on floor plan** button on Rack and Device pages that opens the plan, zooms to the object and highlights it.
- **Interactive view mode:** pan, zoom, tooltips (status, role, tenant, height, device count), click to open the rack or device (Ctrl+click opens a new tab), colouring by type, status or role, and a search box that highlights matches. - **Interactive view mode:** pan, zoom, tooltips (status, role, tenant, height, device count), click to open the rack or device (Ctrl+click opens a new tab), colouring by type, status or role, and a search box that highlights matches.
+1 -1
View File
@@ -1,7 +1,7 @@
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from netbox.plugins import PluginConfig from netbox.plugins import PluginConfig
__version__ = '0.1.1' __version__ = '0.1.2'
class FloorPlannerConfig(PluginConfig): class FloorPlannerConfig(PluginConfig):
Binary file not shown.
@@ -239,6 +239,9 @@ msgstr "Beschreibung"
msgid "Add floor plan" msgid "Add floor plan"
msgstr "Grundriss hinzufügen" msgstr "Grundriss hinzufügen"
msgid "Open floor plan"
msgstr "Grundriss öffnen"
msgid "Show on floor plan" msgid "Show on floor plan"
msgstr "Im Grundriss anzeigen" msgstr "Im Grundriss anzeigen"
+93
View File
@@ -0,0 +1,93 @@
"""
Server-side SVG preview of a floor plan (read-only, no JavaScript). Mirrors the
shape styling of the designer in floorplanner.js in a simplified form.
"""
from django.utils.html import escape
from django.utils.safestring import mark_safe
from .utils import format_meters
__all__ = ('render_preview_svg',)
TYPE_COLORS = {
'area': '#22c55e', 'wall': '#475569', 'door': '#b45309', 'window': '#0ea5e9', 'column': '#64748b',
'rack': '#3b82f6', 'device': '#8b5cf6', 'cooling': '#06b6d4', 'power': '#f59e0b', 'label': '#334155',
}
FILLED_TYPES = ('rack', 'device', 'cooling', 'power')
def _shade(hex_color):
try:
n = int(hex_color.lstrip('#'), 16)
except ValueError:
return hex_color
return '#{:02x}{:02x}{:02x}'.format(*(round(((n >> s) & 255) * 0.7) for s in (16, 8, 0)))
def _item_svg(item):
x, y, w, h = item.x, item.y, item.width, item.height
cx, cy = x + w / 2, y + h / 2
color = item.color or TYPE_COLORS.get(item.item_type, '#868e96')
parts = []
t = item.item_type
if t == 'area':
parts.append(f'<rect x="{x}" y="{y}" width="{w}" height="{h}" rx="4" fill="{color}" fill-opacity="0.12" '
f'stroke="{color}" stroke-width="2" stroke-dasharray="8 4" vector-effect="non-scaling-stroke"/>')
elif t in ('wall', 'door'):
parts.append(f'<rect x="{x}" y="{y}" width="{w}" height="{h}" fill="{color}"/>')
if t == 'door':
parts.append(f'<path d="M {x + w} {y} A {w} {w} 0 0 0 {x} {y - w} L {x} {y}" fill="{color}" '
f'fill-opacity="0.08" stroke="{color}" stroke-dasharray="4 3" vector-effect="non-scaling-stroke"/>')
elif t == 'window':
parts.append(f'<rect x="{x}" y="{y}" width="{w}" height="{h}" style="fill:var(--fp-floor,#fff)" '
f'stroke="{color}" stroke-width="2" vector-effect="non-scaling-stroke"/>')
elif t == 'column':
parts.append(f'<rect x="{x}" y="{y}" width="{w}" height="{h}" fill="{color}" fill-opacity="0.35" '
f'stroke="{color}" stroke-width="2" vector-effect="non-scaling-stroke"/>'
f'<path d="M {x} {y} L {x + w} {y + h} M {x + w} {y} L {x} {y + h}" stroke="{color}" '
f'vector-effect="non-scaling-stroke"/>')
elif t in FILLED_TYPES:
parts.append(f'<rect x="{x}" y="{y}" width="{w}" height="{h}" rx="2" fill="{color}" fill-opacity="0.85" '
f'stroke="{_shade(color)}" stroke-width="2" vector-effect="non-scaling-stroke"/>')
if t == 'rack':
fh = min(h * 0.12, 10)
parts.append(f'<rect x="{x}" y="{y + h - fh}" width="{w}" height="{fh}" fill="{_shade(color)}"/>')
text = item.label or (item.rack.name if item.rack_id else item.device.name if item.device_id else '')
if text:
if t == 'label':
fs = max(8, h * 0.6)
else:
fs = max(6, min(h * 0.3, (w * 1.6) / max(len(text), 3), 60))
fill = '#ffffff' if t in FILLED_TYPES else (item.color or 'currentColor') if t == 'label' else _shade(color)
anchor, tx, ty = ('start', x + fs * 0.5, y + fs * 1.2) if t == 'area' else ('middle', cx, cy)
parts.append(f'<text x="{tx}" y="{ty}" font-size="{fs:.1f}" font-weight="600" text-anchor="{anchor}" '
f'dominant-baseline="middle" fill="{fill}">{escape(text)}</text>')
transform = f' transform="rotate({item.rotation} {cx} {cy})"' if item.rotation else ''
return f'<g{transform}>{"".join(parts)}</g>'
def render_preview_svg(floorplan):
width, depth = floorplan.width, floorplan.depth
fs = max(10, min(max(width, depth) * 0.025, 200))
m = fs * 2
items = floorplan.items.select_related('rack', 'device').order_by('z_index', 'pk')
background = ''
if floorplan.background_image:
background = (f'<image href="{escape(floorplan.background_image.url)}" x="0" y="0" width="{width}" '
f'height="{depth}" preserveAspectRatio="none" opacity="{floorplan.background_opacity / 100}"/>')
return mark_safe(
f'<svg class="fp-preview" xmlns="http://www.w3.org/2000/svg" viewBox="{-m} {-m} {width + 2 * m} {depth + 2 * m}" '
f'role="img" aria-label="{escape(floorplan.name)}" font-family="Inter, system-ui, sans-serif">'
f'<rect x="0" y="0" width="{width}" height="{depth}" style="fill:var(--fp-floor,#fff)"/>'
f'{background}'
f'{"".join(_item_svg(i) for i in items)}'
f'<rect x="0" y="0" width="{width}" height="{depth}" fill="none" style="stroke:var(--fp-wall,#334155)" '
f'stroke-width="4" vector-effect="non-scaling-stroke"/>'
f'<text x="{width / 2}" y="{-fs * 0.6}" font-size="{fs:.1f}" text-anchor="middle" '
f'style="fill:var(--fp-muted,#6c757d)">{format_meters(width)} m</text>'
f'<text transform="translate({-fs * 0.6} {depth / 2}) rotate(-90)" font-size="{fs:.1f}" text-anchor="middle" '
f'style="fill:var(--fp-muted,#6c757d)">{format_meters(depth)} m</text>'
f'</svg>'
)
@@ -158,3 +158,33 @@
.fp-canvas { height: 60vh; } .fp-canvas { height: 60vh; }
.fp-palette, .fp-panel { width: auto; max-height: 30vh; border: none; border-top: 1px solid var(--fp-border); } .fp-palette, .fp-panel { width: auto; max-height: 30vh; border: none; border-top: 1px solid var(--fp-border); }
} }
/* Location page previews */
.fp-preview-list {
--fp-floor: #ffffff;
--fp-wall: #334155;
--fp-muted: #6c757d;
display: flex;
flex-wrap: wrap;
gap: 16px;
}
[data-bs-theme="dark"] .fp-preview-list {
--fp-floor: #1f2937;
--fp-wall: #94a3b8;
--fp-muted: #9aa5b1;
}
.fp-preview-card {
display: flex;
flex-direction: column;
align-items: center;
gap: 6px;
padding: 8px;
border: 1px solid var(--tblr-border-color, #dee2e6);
border-radius: 6px;
color: inherit;
text-decoration: none;
transition: border-color .15s, box-shadow .15s;
}
.fp-preview-card:hover { border-color: #206bc4; box-shadow: 0 2px 10px rgba(32, 107, 196, .2); }
.fp-preview { display: block; width: auto; height: 320px; max-width: 100%; }
.fp-preview-name { font-weight: 600; }
+17 -2
View File
@@ -1,6 +1,7 @@
from netbox.plugins import PluginTemplateExtension from netbox.plugins import PluginTemplateExtension
from .models import FloorPlanItem from .models import FloorPlan, FloorPlanItem
from .preview import render_preview_svg
class RackFloorPlanButton(PluginTemplateExtension): class RackFloorPlanButton(PluginTemplateExtension):
@@ -19,4 +20,18 @@ class RackFloorPlanButton(PluginTemplateExtension):
}) })
template_extensions = [RackFloorPlanButton] class LocationFloorPlanPreview(PluginTemplateExtension):
"""Shows clickable previews of the location's floor plans on the location page."""
models = ['dcim.location']
def full_width_page(self):
request = self.context['request']
plans = FloorPlan.objects.restrict(request.user, 'view').filter(location=self.context['object'])
if not plans:
return ''
return self.render('netbox_floorplanner/inc/location_floorplans.html', extra_context={
'previews': [(plan, render_preview_svg(plan)) for plan in plans],
})
template_extensions = [RackFloorPlanButton, LocationFloorPlanPreview]
@@ -1,5 +1,5 @@
{% load static %} {% load static %}
<link rel="stylesheet" href="{% static 'netbox_floorplanner/floorplanner.css' %}?v=0.1.1"> <link rel="stylesheet" href="{% static 'netbox_floorplanner/floorplanner.css' %}?v=0.1.2">
<div class="fp-root" data-fp-config="fp-config">{% csrf_token %}</div> <div class="fp-root" data-fp-config="fp-config">{% csrf_token %}</div>
{{ designer_config|json_script:"fp-config" }} {{ designer_config|json_script:"fp-config" }}
<script src="{% static 'netbox_floorplanner/floorplanner.js' %}?v=0.1.1"></script> <script src="{% static 'netbox_floorplanner/floorplanner.js' %}?v=0.1.2"></script>
@@ -0,0 +1,14 @@
{% load static %}
{% load i18n %}
<link rel="stylesheet" href="{% static 'netbox_floorplanner/floorplanner.css' %}?v=0.1.2">
<div class="card">
<h2 class="card-header">{% trans "Floor Plans" %}</h2>
<div class="card-body fp-preview-list">
{% for plan, svg in previews %}
<a href="{{ plan.get_absolute_url }}" class="fp-preview-card" title="{% trans 'Open floor plan' %}">
{{ svg }}
<span class="fp-preview-name"><i class="mdi mdi-floor-plan"></i> {{ plan.name }}</span>
</a>
{% endfor %}
</div>
</div>
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project] [project]
name = "netbox-floorplanner" name = "netbox-floorplanner"
version = "0.1.1" version = "0.1.2"
description = "Modern drag & drop floor plan designer for NetBox 4.7+" description = "Modern drag & drop floor plan designer for NetBox 4.7+"
readme = "README.md" readme = "README.md"
license = "Apache-2.0" license = "Apache-2.0"