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 <[email protected]>
This commit is contained in:
1 parent
805aedba52
commit
04a6e16975
16 files changed
+87
-16
No files matched your search
@@ -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):
|
||||
|
||||
@@ -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',
|
||||
)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
)
|
||||
@@ -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')
|
||||
Binary file not shown.
@@ -122,6 +122,12 @@ msgstr "Farbe"
|
||||
msgid "z-index"
|
||||
msgstr "Ebene"
|
||||
|
||||
msgid "tenant"
|
||||
msgstr "Mandant"
|
||||
|
||||
msgid "Tenancy"
|
||||
msgstr "Mandantenzuordnung"
|
||||
|
||||
msgid "rack"
|
||||
msgstr "Rack"
|
||||
|
||||
|
||||
@@ -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),
|
||||
]
|
||||
@@ -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,
|
||||
|
||||
@@ -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'
|
||||
@@ -33,6 +33,13 @@
|
||||
<th scope="row">{% trans "Location" %}</th>
|
||||
<td>{{ object.location|linkify|placeholder }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">{% trans "Tenant" %}</th>
|
||||
<td>
|
||||
{% if object.tenant.group %}{{ object.tenant.group|linkify }} / {% endif %}
|
||||
{{ object.tenant|linkify|placeholder }}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">{% trans "Dimensions" %}</th>
|
||||
<td>{{ room_size }} m <span class="text-muted">({{ object.width }} × {{ object.depth }} cm · {{ floor_area }} m²)</span></td>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{% load static %}
|
||||
<link rel="stylesheet" href="{% static 'netbox_floorplanner/floorplanner.css' %}?v=0.1.2">
|
||||
<link rel="stylesheet" href="{% static 'netbox_floorplanner/floorplanner.css' %}?v=0.1.3">
|
||||
<div class="fp-root" data-fp-config="fp-config">{% csrf_token %}</div>
|
||||
{{ designer_config|json_script:"fp-config" }}
|
||||
<script src="{% static 'netbox_floorplanner/floorplanner.js' %}?v=0.1.2"></script>
|
||||
<script src="{% static 'netbox_floorplanner/floorplanner.js' %}?v=0.1.3"></script>
|
||||
@@ -1,6 +1,6 @@
|
||||
{% load static %}
|
||||
{% load i18n %}
|
||||
<link rel="stylesheet" href="{% static 'netbox_floorplanner/floorplanner.css' %}?v=0.1.2">
|
||||
<link rel="stylesheet" href="{% static 'netbox_floorplanner/floorplanner.css' %}?v=0.1.3">
|
||||
<div class="card">
|
||||
<h2 class="card-header">{% trans "Floor Plans" %}</h2>
|
||||
<div class="card-body fp-preview-list">
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user