From 805aedba526dcf39836337e858dedf38f9101114 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 4 Nov 2026 12:53:42 +0100 Subject: [PATCH] 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 --- README.md | 1 + netbox_floorplanner/__init__.py | 2 +- .../locale/de/LC_MESSAGES/django.mo | Bin 7162 -> 7212 bytes .../locale/de/LC_MESSAGES/django.po | 3 + netbox_floorplanner/preview.py | 93 ++++++++++++++++++ .../netbox_floorplanner/floorplanner.css | 30 ++++++ netbox_floorplanner/template_content.py | 19 +++- .../netbox_floorplanner/inc/designer.html | 4 +- .../inc/location_floorplans.html | 14 +++ pyproject.toml | 2 +- 10 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 netbox_floorplanner/preview.py create mode 100644 netbox_floorplanner/templates/netbox_floorplanner/inc/location_floorplans.html diff --git a/README.md b/README.md index f91f6d1..b4af10b 100644 --- a/README.md +++ b/README.md @@ -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. - **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. + - 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 **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. diff --git a/netbox_floorplanner/__init__.py b/netbox_floorplanner/__init__.py index fd15274..ad808a8 100644 --- a/netbox_floorplanner/__init__.py +++ b/netbox_floorplanner/__init__.py @@ -1,7 +1,7 @@ from django.utils.translation import gettext_lazy as _ from netbox.plugins import PluginConfig -__version__ = '0.1.1' +__version__ = '0.1.2' class FloorPlannerConfig(PluginConfig): diff --git a/netbox_floorplanner/locale/de/LC_MESSAGES/django.mo b/netbox_floorplanner/locale/de/LC_MESSAGES/django.mo index 1babd87fd7f8221bd1958c4daa716c4d4eca3ff6..58eed25f050c8e7652f7186cca1cc962f824f7e3 100644 GIT binary patch delta 2216 zcmYk;ZD>|y7{Kvc+MIiQT>7lm%4D{gZGl_O*rxeXl0_oZK}FO;o|=QjqtmU`()1K8 zA%|FosL&#ceaJT?>D8HLwFvgX7+F-5;i4fChJ9Ht)c?Qdh8kz*ckcV#FV}UQbEbbZ z9&Sp$&lKzp{PgjAJ-4;%3Xti+FS z6vvPwyvN|hcm`AKAO4=aU=wzm!Hv@B4QK#$oZR5{=-ts4bir1%GVNH0J@Nh^a)e`? zOmGNI@N;zDSuAA#@C_G~BAmAaFQb7>pc@x4+X`gR{@G}zH8>M%(EygC88_q2xEUS) z3R_3LCJA_u?%na)9#Rwe)6V9O}x)29kL@V<b$PeL-_#sx{7@o$0S@}Rd#j9z5fgIsyPWRyi zx=u5T81Mtgk%UJ%yHO9C*;ZVD{b(ldBS$#R$%&(AMn9mLjHBnd=)wt7 zys#Us)F2w@(RhCd&3pu%_aj=d@p9^4&&4G=uEQ$A^IyAV=*BD1{xxXEk7Fx#q5+Oz zJARFR70Ves`6$r86C2PCJ7fFF*zUzf`jdnUH+mOc@Dntnv&a$7bF##LqVK|#+4*)F zCo6|;P=U^$k5;N4{rr}o6>CQ4XVJ>!&~=k7T)5D)XokC@d(eUVVtWwH>>yg=x6n$R zKm$01E;x)7GmM}w*-tnJOZ?Bpoo#LY&TY?ic6QC(l*?{BzHZtb1*!kF52PPV72ng< Qv$3tKqr3a~cv+_KUt^5LCjbBd delta 2158 zcmYM#Z)jIl7{Kw<{?we?oPXM%+!UHoTjex!6J?5)qhyP=T+ybnK^xSFiWM!$evUA8h7_|&$;J3=Q+>4cN<&t zcVp(K1$o1XpI!V`@%w0=t^a>o3R23Y*@`9D7ItF;?N`w$qgaS1;{G`1)BYMc(zl%M z$4R^s|G@<*Wm4{(l#1vmLvPfef$DHBu0{iFjQd;gF4}E451&WJzl0|81~%f`crTvD z%Q%S~=~D);#WPsQ`f2+1MM}4@m>apQs`JqVmU7}&dN6DVH=+Ss(8@fE_1GKthmj*4 z<>Us(&<%c$&O3+ste+;hpcLtn9rz2Hz;$%t8MFd9M6rJ^y3HI&;^>&1X|DvbfED% zqum?#GcQNSKsbm7*n#e37n=DH-iJqUDSn0L@K-dEkLRa!H=ah0bb-@Wyn@DAP7ud6 zpz$_wVx3It;KH4C;ltRE?&J`1q@$dim_>JV7Tw83^c}by{Zr_?>*#`ipmA@Z^9vTv zR$>wQ_n{J3`2DwWv5bfN9y;)oMYEqt7U@cPY)dP!3?D)RKZYjq44TLb=%eUI6MYkT zPt&_d4rvdr#E;NcW@VFm5| zXoZfWpWzqi#wO7XOreQRms5Wy%*23F8V;yJ2h^gaTZKN`W;AdsQlzvUtw|87O3>B6PxjcRnqYj7Jjp*tACJ{(2AhGGUAcoq8h zVI8_)Q?wtCb~858--@oY8;y4$!-YFKf*k22CrkVd`jUJf?H}=W>CgqH(Exv;l}d|e zKf6M-VpZt;dbBcY(YQ~bah^gq*x3~q-RQvHXb+$}8$?UI6Rp(yXaalDfCrIkrV;ez Z%3?KMU7YPK=*Y{SEov{!)|Ai5{}03{u>1f3 diff --git a/netbox_floorplanner/locale/de/LC_MESSAGES/django.po b/netbox_floorplanner/locale/de/LC_MESSAGES/django.po index 0bd6119..c5ee3b9 100644 --- a/netbox_floorplanner/locale/de/LC_MESSAGES/django.po +++ b/netbox_floorplanner/locale/de/LC_MESSAGES/django.po @@ -239,6 +239,9 @@ msgstr "Beschreibung" msgid "Add floor plan" msgstr "Grundriss hinzufügen" +msgid "Open floor plan" +msgstr "Grundriss öffnen" + msgid "Show on floor plan" msgstr "Im Grundriss anzeigen" diff --git a/netbox_floorplanner/preview.py b/netbox_floorplanner/preview.py new file mode 100644 index 0000000..adb9bf5 --- /dev/null +++ b/netbox_floorplanner/preview.py @@ -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'') + elif t in ('wall', 'door'): + parts.append(f'') + if t == 'door': + parts.append(f'') + elif t == 'window': + parts.append(f'') + elif t == 'column': + parts.append(f'' + f'') + elif t in FILLED_TYPES: + parts.append(f'') + if t == 'rack': + fh = min(h * 0.12, 10) + parts.append(f'') + + 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'{escape(text)}') + + transform = f' transform="rotate({item.rotation} {cx} {cy})"' if item.rotation else '' + return f'{"".join(parts)}' + + +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'') + return mark_safe( + f'' + f'' + f'{background}' + f'{"".join(_item_svg(i) for i in items)}' + f'' + f'{format_meters(width)} m' + f'{format_meters(depth)} m' + f'' + ) diff --git a/netbox_floorplanner/static/netbox_floorplanner/floorplanner.css b/netbox_floorplanner/static/netbox_floorplanner/floorplanner.css index 85a6963..78f0207 100644 --- a/netbox_floorplanner/static/netbox_floorplanner/floorplanner.css +++ b/netbox_floorplanner/static/netbox_floorplanner/floorplanner.css @@ -158,3 +158,33 @@ .fp-canvas { height: 60vh; } .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; } diff --git a/netbox_floorplanner/template_content.py b/netbox_floorplanner/template_content.py index 05b09fe..ecfbf50 100644 --- a/netbox_floorplanner/template_content.py +++ b/netbox_floorplanner/template_content.py @@ -1,6 +1,7 @@ from netbox.plugins import PluginTemplateExtension -from .models import FloorPlanItem +from .models import FloorPlan, FloorPlanItem +from .preview import render_preview_svg 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] diff --git a/netbox_floorplanner/templates/netbox_floorplanner/inc/designer.html b/netbox_floorplanner/templates/netbox_floorplanner/inc/designer.html index 7f526ad..c01d3bc 100644 --- a/netbox_floorplanner/templates/netbox_floorplanner/inc/designer.html +++ b/netbox_floorplanner/templates/netbox_floorplanner/inc/designer.html @@ -1,5 +1,5 @@ {% load static %} - +
{% csrf_token %}
{{ designer_config|json_script:"fp-config" }} - + diff --git a/netbox_floorplanner/templates/netbox_floorplanner/inc/location_floorplans.html b/netbox_floorplanner/templates/netbox_floorplanner/inc/location_floorplans.html new file mode 100644 index 0000000..fb6161f --- /dev/null +++ b/netbox_floorplanner/templates/netbox_floorplanner/inc/location_floorplans.html @@ -0,0 +1,14 @@ +{% load static %} +{% load i18n %} + +
+

{% trans "Floor Plans" %}

+
+ {% for plan, svg in previews %} + + {{ svg }} + {{ plan.name }} + + {% endfor %} +
+
diff --git a/pyproject.toml b/pyproject.toml index 25ba52c..843c880 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "netbox-floorplanner" -version = "0.1.1" +version = "0.1.2" description = "Modern drag & drop floor plan designer for NetBox 4.7+" readme = "README.md" license = "Apache-2.0"