From 04a6e16975dbd038f15697293e843332880af2f2 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 4 Nov 2026 13:04:15 +0100 Subject: [PATCH] Add tenant to floor plans with tenant filters Floor plans get an optional tenant (inherited from location or site on creation; existing plans are pre-filled by the migration). The list view supports tenant and tenant group filtering and columns, and the tenant is exposed in forms, CSV import, REST API and the detail page. Co-Authored-By: Claude Opus 5.5 --- README.md | 1 + netbox_floorplanner/__init__.py | 2 +- netbox_floorplanner/api/serializers.py | 4 ++- netbox_floorplanner/api/views.py | 2 +- netbox_floorplanner/filtersets.py | 4 ++- netbox_floorplanner/forms.py | 15 +++++--- .../locale/de/LC_MESSAGES/django.mo | Bin 7212 -> 7286 bytes .../locale/de/LC_MESSAGES/django.po | 6 ++++ .../migrations/0002_floorplan_tenant.py | 32 ++++++++++++++++++ netbox_floorplanner/models.py | 8 +++++ netbox_floorplanner/tables.py | 7 ++-- .../netbox_floorplanner/floorplan.html | 7 ++++ .../netbox_floorplanner/inc/designer.html | 4 +-- .../inc/location_floorplans.html | 2 +- netbox_floorplanner/views.py | 7 +++- pyproject.toml | 2 +- 16 files changed, 87 insertions(+), 16 deletions(-) create mode 100644 netbox_floorplanner/migrations/0002_floorplan_tenant.py diff --git a/README.md b/README.md index b4af10b..62c02a5 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ sudo systemctl restart netbox netbox-rq - **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. - **Background drawings:** upload a CAD or architectural image per plan, stretched to the room size with adjustable opacity. - **Export:** SVG (vector) and high-resolution PNG, with the background embedded. +- **Tenancy:** an optional tenant per floor plan, with tenant and tenant group filters and columns in the floor plan list. New plans inherit the tenant of their location or site, and existing plans are pre-filled from them during the migration. - **NetBox native:** change logging, journaling, tags, custom fields, global search, CSV/YAML import, bulk delete, object permissions, REST API, dark mode and fullscreen. ### Keyboard shortcuts (designer) diff --git a/netbox_floorplanner/__init__.py b/netbox_floorplanner/__init__.py index ad808a8..cc05da4 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.2' +__version__ = '0.1.3' class FloorPlannerConfig(PluginConfig): diff --git a/netbox_floorplanner/api/serializers.py b/netbox_floorplanner/api/serializers.py index 2cc0926..49f23ee 100644 --- a/netbox_floorplanner/api/serializers.py +++ b/netbox_floorplanner/api/serializers.py @@ -3,6 +3,7 @@ from dcim.choices import DeviceStatusChoices, RackStatusChoices from dcim.models import Device, Rack from django.utils.translation import gettext as _ from netbox.api.serializers import NetBoxModelSerializer +from tenancy.api.serializers import TenantSerializer from rest_framework import serializers from ..models import FloorPlan, FloorPlanItem @@ -14,12 +15,13 @@ class FloorPlanSerializer(NetBoxModelSerializer): url = serializers.HyperlinkedIdentityField(view_name='plugins-api:netbox_floorplanner-api:floorplan-detail') site = SiteSerializer(nested=True) location = LocationSerializer(nested=True, required=False, allow_null=True, default=None) + tenant = TenantSerializer(nested=True, required=False, allow_null=True, default=None) item_count = serializers.IntegerField(read_only=True, required=False) class Meta: model = FloorPlan fields = ( - 'id', 'url', 'display', 'name', 'site', 'location', 'width', 'depth', 'grid_size', + 'id', 'url', 'display', 'name', 'site', 'location', 'tenant', 'width', 'depth', 'grid_size', 'background_image', 'background_opacity', 'description', 'comments', 'item_count', 'tags', 'custom_fields', 'created', 'last_updated', ) diff --git a/netbox_floorplanner/api/views.py b/netbox_floorplanner/api/views.py index 1a34291..1d970a7 100644 --- a/netbox_floorplanner/api/views.py +++ b/netbox_floorplanner/api/views.py @@ -10,7 +10,7 @@ from .serializers import FloorPlanItemSerializer, FloorPlanSerializer, LayoutSer class FloorPlanViewSet(NetBoxModelViewSet): - queryset = FloorPlan.objects.select_related('site', 'location').prefetch_related('tags').annotate( + queryset = FloorPlan.objects.select_related('site', 'location', 'tenant').prefetch_related('tags').annotate( item_count=Count('items') ) serializer_class = FloorPlanSerializer diff --git a/netbox_floorplanner/filtersets.py b/netbox_floorplanner/filtersets.py index fd40de0..30eb74f 100644 --- a/netbox_floorplanner/filtersets.py +++ b/netbox_floorplanner/filtersets.py @@ -3,13 +3,14 @@ from dcim.models import Location, Site from django.db.models import Q from django.utils.translation import gettext_lazy as _ from netbox.filtersets import NetBoxModelFilterSet +from tenancy.filtersets import TenancyFilterSet from .models import FloorPlan __all__ = ('FloorPlanFilterSet',) -class FloorPlanFilterSet(NetBoxModelFilterSet): +class FloorPlanFilterSet(TenancyFilterSet, NetBoxModelFilterSet): site_id = django_filters.ModelMultipleChoiceFilter( queryset=Site.objects.all(), label=_('Site (ID)'), @@ -42,4 +43,5 @@ class FloorPlanFilterSet(NetBoxModelFilterSet): | Q(description__icontains=value) | Q(site__name__icontains=value) | Q(location__name__icontains=value) + | Q(tenant__name__icontains=value) ) diff --git a/netbox_floorplanner/forms.py b/netbox_floorplanner/forms.py index 9dce80e..7dd247f 100644 --- a/netbox_floorplanner/forms.py +++ b/netbox_floorplanner/forms.py @@ -3,6 +3,8 @@ from django import forms from django.utils.translation import gettext_lazy as _ from netbox.forms import NetBoxModelFilterSetForm, NetBoxModelForm, NetBoxModelImportForm from netbox.plugins import get_plugin_config +from tenancy.forms import TenancyFilterForm, TenancyForm +from tenancy.models import Tenant from utilities.forms.fields import ( CSVModelChoiceField, CommentField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, TagFilterField, ) @@ -14,7 +16,7 @@ from .utils import format_room_size, parse_room_size __all__ = ('FloorPlanForm', 'FloorPlanFilterForm', 'FloorPlanImportForm') -class FloorPlanForm(NetBoxModelForm): +class FloorPlanForm(TenancyForm, NetBoxModelForm): site = DynamicModelChoiceField(label=_('Site'), queryset=Site.objects.all()) location = DynamicModelChoiceField( label=_('Location'), @@ -31,6 +33,7 @@ class FloorPlanForm(NetBoxModelForm): fieldsets = ( FieldSet('name', 'site', 'location', 'description', 'tags', name=_('Floor plan')), + FieldSet('tenant_group', 'tenant', name=_('Tenancy')), FieldSet('room_size', 'width', 'depth', 'grid_size', name=_('Dimensions')), FieldSet('background_image', 'background_opacity', name=_('Background')), ) @@ -38,7 +41,7 @@ class FloorPlanForm(NetBoxModelForm): class Meta: model = FloorPlan fields = ( - 'name', 'site', 'location', 'description', 'width', 'depth', 'grid_size', + 'name', 'site', 'location', 'tenant_group', 'tenant', 'description', 'width', 'depth', 'grid_size', 'background_image', 'background_opacity', 'comments', 'tags', ) widgets = { @@ -67,11 +70,12 @@ class FloorPlanForm(NetBoxModelForm): return self.cleaned_data -class FloorPlanFilterForm(NetBoxModelFilterSetForm): +class FloorPlanFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): model = FloorPlan fieldsets = ( FieldSet('q', 'filter_id', 'tag'), FieldSet('site_id', 'location_id', name=_('Location')), + FieldSet('tenant_group_id', 'tenant_id', name=_('Tenant')), ) site_id = DynamicModelMultipleChoiceField(queryset=Site.objects.all(), required=False, label=_('Site')) location_id = DynamicModelMultipleChoiceField( @@ -88,7 +92,10 @@ class FloorPlanImportForm(NetBoxModelImportForm): location = CSVModelChoiceField( label=_('Location'), queryset=Location.objects.all(), to_field_name='name', required=False, ) + tenant = CSVModelChoiceField( + label=_('Tenant'), queryset=Tenant.objects.all(), to_field_name='name', required=False, + ) class Meta: model = FloorPlan - fields = ('name', 'site', 'location', 'description', 'width', 'depth', 'grid_size', 'comments', 'tags') + fields = ('name', 'site', 'location', 'tenant', 'description', 'width', 'depth', 'grid_size', 'comments', 'tags') diff --git a/netbox_floorplanner/locale/de/LC_MESSAGES/django.mo b/netbox_floorplanner/locale/de/LC_MESSAGES/django.mo index 58eed25f050c8e7652f7186cca1cc962f824f7e3..53b86ff4ab654c928765d7a4b8038cfd4e35da68 100644 GIT binary patch delta 2243 zcmYM$acEUl9Ki9@b8pSPe67v4>5^&{TV|~`+=d}#vLdKyO=^}{5BF61$^Z7GZ;usPDHOXx!b;J?EbDJHPY0_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SAGES/django.po index c5ee3b9..49f6387 100644 --- a/netbox_floorplanner/locale/de/LC_MESSAGES/django.po +++ b/netbox_floorplanner/locale/de/LC_MESSAGES/django.po @@ -122,6 +122,12 @@ msgstr "Farbe" msgid "z-index" msgstr "Ebene" +msgid "tenant" +msgstr "Mandant" + +msgid "Tenancy" +msgstr "Mandantenzuordnung" + msgid "rack" msgstr "Rack" diff --git a/netbox_floorplanner/migrations/0002_floorplan_tenant.py b/netbox_floorplanner/migrations/0002_floorplan_tenant.py new file mode 100644 index 0000000..4d8fb9f --- /dev/null +++ b/netbox_floorplanner/migrations/0002_floorplan_tenant.py @@ -0,0 +1,32 @@ +import django.db.models.deletion +from django.db import migrations, models + + +def inherit_tenant(apps, schema_editor): + """Pre-fill the tenant of existing floor plans from their location or site.""" + FloorPlan = apps.get_model('netbox_floorplanner', 'FloorPlan') + for plan in FloorPlan.objects.select_related('site', 'location').filter(tenant__isnull=True): + tenant_id = (plan.location.tenant_id if plan.location_id else None) or plan.site.tenant_id + if tenant_id: + plan.tenant_id = tenant_id + plan.save(update_fields=['tenant']) + + +class Migration(migrations.Migration): + + dependencies = [ + ('tenancy', '__first__'), + ('netbox_floorplanner', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='floorplan', + name='tenant', + field=models.ForeignKey( + blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, + related_name='floorplans', to='tenancy.tenant', + ), + ), + migrations.RunPython(inherit_tenant, migrations.RunPython.noop), + ] diff --git a/netbox_floorplanner/models.py b/netbox_floorplanner/models.py index 4dff9d5..fb46a03 100644 --- a/netbox_floorplanner/models.py +++ b/netbox_floorplanner/models.py @@ -26,6 +26,14 @@ class FloorPlan(NetBoxModel): null=True, verbose_name=_('location'), ) + tenant = models.ForeignKey( + to='tenancy.Tenant', + on_delete=models.PROTECT, + related_name='floorplans', + blank=True, + null=True, + verbose_name=_('tenant'), + ) width = models.PositiveIntegerField( verbose_name=_('width'), default=2000, diff --git a/netbox_floorplanner/tables.py b/netbox_floorplanner/tables.py index 5f4426e..7af22a1 100644 --- a/netbox_floorplanner/tables.py +++ b/netbox_floorplanner/tables.py @@ -1,6 +1,7 @@ import django_tables2 as tables from django.utils.translation import gettext_lazy as _ from netbox.tables import NetBoxTable, columns +from tenancy.tables import TenancyColumnsMixin from .models import FloorPlan from .utils import format_room_size @@ -8,7 +9,7 @@ from .utils import format_room_size __all__ = ('FloorPlanTable',) -class FloorPlanTable(NetBoxTable): +class FloorPlanTable(TenancyColumnsMixin, NetBoxTable): name = tables.Column(verbose_name=_('Name'), linkify=True) site = tables.Column(verbose_name=_('Site'), linkify=True) location = tables.Column(verbose_name=_('Location'), linkify=True) @@ -22,10 +23,10 @@ class FloorPlanTable(NetBoxTable): class Meta(NetBoxTable.Meta): model = FloorPlan fields = ( - 'pk', 'id', 'name', 'site', 'location', 'room_size', 'width', 'depth', 'grid_size', 'item_count', + 'pk', 'id', 'name', 'site', 'location', 'tenant', 'tenant_group', 'room_size', 'width', 'depth', 'grid_size', 'item_count', 'rack_count', 'description', 'tags', 'created', 'last_updated', ) - default_columns = ('name', 'site', 'location', 'room_size', 'rack_count', 'description') + default_columns = ('name', 'site', 'location', 'tenant', 'room_size', 'rack_count', 'description') def render_room_size(self, record): return f'{format_room_size(record.width, record.depth)} m' diff --git a/netbox_floorplanner/templates/netbox_floorplanner/floorplan.html b/netbox_floorplanner/templates/netbox_floorplanner/floorplan.html index 66f72a7..3d5ba5a 100644 --- a/netbox_floorplanner/templates/netbox_floorplanner/floorplan.html +++ b/netbox_floorplanner/templates/netbox_floorplanner/floorplan.html @@ -33,6 +33,13 @@ {% trans "Location" %} {{ object.location|linkify|placeholder }} + + {% trans "Tenant" %} + + {% if object.tenant.group %}{{ object.tenant.group|linkify }} / {% endif %} + {{ object.tenant|linkify|placeholder }} + + {% trans "Dimensions" %} {{ room_size }} m ({{ object.width }} × {{ object.depth }} cm · {{ floor_area }} m²) diff --git a/netbox_floorplanner/templates/netbox_floorplanner/inc/designer.html b/netbox_floorplanner/templates/netbox_floorplanner/inc/designer.html index c01d3bc..f72ec4f 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 index fb6161f..715c52f 100644 --- a/netbox_floorplanner/templates/netbox_floorplanner/inc/location_floorplans.html +++ b/netbox_floorplanner/templates/netbox_floorplanner/inc/location_floorplans.html @@ -1,6 +1,6 @@ {% load static %} {% load i18n %} - +

{% trans "Floor Plans" %}

diff --git a/netbox_floorplanner/views.py b/netbox_floorplanner/views.py index da0cacf..911cb28 100644 --- a/netbox_floorplanner/views.py +++ b/netbox_floorplanner/views.py @@ -15,7 +15,7 @@ from .utils import format_meters, format_room_size def floorplans_queryset(): - return FloorPlan.objects.select_related('site', 'location').annotate( + return FloorPlan.objects.select_related('site', 'location', 'tenant').annotate( item_count=Count('items', distinct=True), rack_count=Count('items', filter=Q(items__rack__isnull=False), distinct=True), ) @@ -104,6 +104,11 @@ class FloorPlanEditView(generic.ObjectEditView): obj.site_id = site_id if location_id := request.GET.get('location'): obj.location_id = location_id + # Inherit the tenant from the location or site + if obj.location_id and (location := Location.objects.filter(pk=obj.location_id).first()): + obj.tenant_id = location.tenant_id + if not obj.tenant_id and obj.site_id and (site := Site.objects.filter(pk=obj.site_id).first()): + obj.tenant_id = site.tenant_id return obj diff --git a/pyproject.toml b/pyproject.toml index 843c880..924fe25 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "netbox-floorplanner" -version = "0.1.2" +version = "0.1.3" description = "Modern drag & drop floor plan designer for NetBox 4.7+" readme = "README.md" license = "Apache-2.0"