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 58eed25..53b86ff 100644 Binary files a/netbox_floorplanner/locale/de/LC_MESSAGES/django.mo and b/netbox_floorplanner/locale/de/LC_MESSAGES/django.mo differ diff --git a/netbox_floorplanner/locale/de/LC_MESSAGES/django.po b/netbox_floorplanner/locale/de/LC_MESSAGES/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 @@