Add room size input in meters alongside centimeters
The floor plan form and the designer accept a room size like "4 x 3" (meters), show dimensions in meters on the plan edges, in the table, detail page and tooltips, and display the floor area. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
53ffda1773
commit
ab8ab7ed61
10 files changed
+152
-14
No files matched your search
@@ -9,6 +9,7 @@ from utilities.forms.fields import (
|
||||
from utilities.forms.rendering import FieldSet
|
||||
|
||||
from .models import FloorPlan
|
||||
from .utils import format_room_size, parse_room_size
|
||||
|
||||
__all__ = ('FloorPlanForm', 'FloorPlanFilterForm', 'FloorPlanImportForm')
|
||||
|
||||
@@ -21,11 +22,16 @@ class FloorPlanForm(NetBoxModelForm):
|
||||
required=False,
|
||||
query_params={'site_id': '$site'},
|
||||
)
|
||||
room_size = forms.CharField(
|
||||
label=_('Room size (m)'),
|
||||
required=False,
|
||||
help_text=_('Width x depth in meters, e.g. "4 x 3" or "12,5 x 8". Takes precedence over the centimeter values.'),
|
||||
)
|
||||
comments = CommentField()
|
||||
|
||||
fieldsets = (
|
||||
FieldSet('name', 'site', 'location', 'description', 'tags', name=_('Floor plan')),
|
||||
FieldSet('width', 'depth', 'grid_size', name=_('Dimensions (cm)')),
|
||||
FieldSet('room_size', 'width', 'depth', 'grid_size', name=_('Dimensions')),
|
||||
FieldSet('background_image', 'background_opacity', name=_('Background')),
|
||||
)
|
||||
|
||||
@@ -45,6 +51,20 @@ class FloorPlanForm(NetBoxModelForm):
|
||||
for field, setting in (('width', 'default_width'), ('depth', 'default_depth'),
|
||||
('grid_size', 'default_grid_size')):
|
||||
self.fields[field].initial = get_plugin_config('netbox_floorplanner', setting)
|
||||
width = self.instance.width if self.instance.pk else self.fields['width'].initial
|
||||
depth = self.instance.depth if self.instance.pk else self.fields['depth'].initial
|
||||
self.fields['room_size'].initial = format_room_size(width, depth)
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
# Only apply the meter value if the user actually edited it, so the
|
||||
# centimeter fields can still be used on their own.
|
||||
if 'room_size' in self.changed_data and self.cleaned_data.get('room_size'):
|
||||
size = parse_room_size(self.cleaned_data['room_size'])
|
||||
if size is None:
|
||||
raise forms.ValidationError({'room_size': _('Enter the size as "width x depth" in meters, e.g. "4 x 3".')})
|
||||
self.cleaned_data['width'], self.cleaned_data['depth'] = size
|
||||
return self.cleaned_data
|
||||
|
||||
|
||||
class FloorPlanFilterForm(NetBoxModelFilterSetForm):
|
||||
|
||||
Reference in new issue
Block a user