From 255d12309a4cec652d660b102c1b5848a7513413 Mon Sep 17 00:00:00 2001 From: hellerve Date: Tue, 21 Jan 2020 15:50:38 +0100 Subject: [PATCH 1/3] dcim: fix #3965 by adding an option to get_rack_units --- netbox/dcim/models/__init__.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/netbox/dcim/models/__init__.py b/netbox/dcim/models/__init__.py index 4ff15141c..c1c47cf23 100644 --- a/netbox/dcim/models/__init__.py +++ b/netbox/dcim/models/__init__.py @@ -468,7 +468,7 @@ class RackElevationHelperMixin: :param unit_height: Height of each rack unit for the rendered drawing. Note this is not the total height of the elevation """ - elevation = self.get_rack_units(face=face, expand_devices=False) + elevation = self.get_rack_units(face=face, expand_devices=False, always_show_device=True) reserved_units = self.get_reserved_units().keys() return self._draw_elevations(elevation, reserved_units, face, unit_width, unit_height) @@ -694,7 +694,7 @@ class Rack(ChangeLoggedModel, CustomFieldModel, RackElevationHelperMixin): def get_status_class(self): return self.STATUS_CLASS_MAP.get(self.status) - def get_rack_units(self, face=DeviceFaceChoices.FACE_FRONT, exclude=None, expand_devices=True): + def get_rack_units(self, face=DeviceFaceChoices.FACE_FRONT, exclude=None, expand_devices=True, always_show_device=False): """ Return a list of rack units as dictionaries. Example: {'device': None, 'face': 0, 'id': 48, 'name': 'U48'} Each key 'device' is either a Device or None. By default, multi-U devices are repeated for each U they occupy. @@ -704,6 +704,8 @@ class Rack(ChangeLoggedModel, CustomFieldModel, RackElevationHelperMixin): :param expand_devices: When True, all units that a device occupies will be listed with each containing a reference to the device. When False, only the bottom most unit for a device is included and that unit contains a height attribute for the device + :param always_show_device: When True it will always show the device, no matter its orientation. + When False it will only show full-width devices or those with the right orientation in the rack. """ elevation = OrderedDict() @@ -723,9 +725,11 @@ class Rack(ChangeLoggedModel, CustomFieldModel, RackElevationHelperMixin): ).filter( rack=self, position__gt=0 - ).filter( - Q(face=face) | Q(device_type__is_full_depth=True) ) + if not always_show_device: + queryset = queryset.filter( + Q(face=face) | Q(device_type__is_full_depth=True) + ) for device in queryset: if expand_devices: for u in range(device.position, device.position + device.device_type.u_height): From e421c15bddb37e9fc34405107cc6d0b0cb365432 Mon Sep 17 00:00:00 2001 From: hellerve Date: Tue, 21 Jan 2020 16:56:06 +0100 Subject: [PATCH 2/3] dcim: merge elevations as necessary --- netbox/dcim/models/__init__.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/netbox/dcim/models/__init__.py b/netbox/dcim/models/__init__.py index c1c47cf23..e95c3e7b5 100644 --- a/netbox/dcim/models/__init__.py +++ b/netbox/dcim/models/__init__.py @@ -459,6 +459,21 @@ class RackElevationHelperMixin: return drawing + def merge_elevations(self, face): + elevation = self.get_rack_units(face=face, expand_devices=False) + other_face = DeviceFaceChoices.FACE_FRONT if face==DeviceFaceChoices.FACE_REAR else DeviceFaceChoices.FACE_REAR + other = self.get_rack_units(face=other_face) + + unit_cursor = 0 + for u in elevation: + o = other[unit_cursor] + if not u['device'] and o['device']: + u['device'] = o['device'] + u['height'] = 1 + unit_cursor += u.get('height', 1) + + return elevation + def get_elevation_svg(self, face=DeviceFaceChoices.FACE_FRONT, unit_width=230, unit_height=20): """ Return an SVG of the rack elevation @@ -468,7 +483,7 @@ class RackElevationHelperMixin: :param unit_height: Height of each rack unit for the rendered drawing. Note this is not the total height of the elevation """ - elevation = self.get_rack_units(face=face, expand_devices=False, always_show_device=True) + elevation = self.merge_elevations(face) reserved_units = self.get_reserved_units().keys() return self._draw_elevations(elevation, reserved_units, face, unit_width, unit_height) @@ -694,7 +709,7 @@ class Rack(ChangeLoggedModel, CustomFieldModel, RackElevationHelperMixin): def get_status_class(self): return self.STATUS_CLASS_MAP.get(self.status) - def get_rack_units(self, face=DeviceFaceChoices.FACE_FRONT, exclude=None, expand_devices=True, always_show_device=False): + def get_rack_units(self, face=DeviceFaceChoices.FACE_FRONT, exclude=None, expand_devices=True): """ Return a list of rack units as dictionaries. Example: {'device': None, 'face': 0, 'id': 48, 'name': 'U48'} Each key 'device' is either a Device or None. By default, multi-U devices are repeated for each U they occupy. @@ -704,8 +719,6 @@ class Rack(ChangeLoggedModel, CustomFieldModel, RackElevationHelperMixin): :param expand_devices: When True, all units that a device occupies will be listed with each containing a reference to the device. When False, only the bottom most unit for a device is included and that unit contains a height attribute for the device - :param always_show_device: When True it will always show the device, no matter its orientation. - When False it will only show full-width devices or those with the right orientation in the rack. """ elevation = OrderedDict() @@ -725,11 +738,9 @@ class Rack(ChangeLoggedModel, CustomFieldModel, RackElevationHelperMixin): ).filter( rack=self, position__gt=0 + ).filter( + Q(face=face) | Q(device_type__is_full_depth=True) ) - if not always_show_device: - queryset = queryset.filter( - Q(face=face) | Q(device_type__is_full_depth=True) - ) for device in queryset: if expand_devices: for u in range(device.position, device.position + device.device_type.u_height): From e184eb3521238f93ce0750558a4c1bb9ee3a06ab Mon Sep 17 00:00:00 2001 From: hellerve Date: Tue, 21 Jan 2020 17:01:48 +0100 Subject: [PATCH 3/3] dcim: make pep happy --- netbox/dcim/models/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox/dcim/models/__init__.py b/netbox/dcim/models/__init__.py index e95c3e7b5..5629edc37 100644 --- a/netbox/dcim/models/__init__.py +++ b/netbox/dcim/models/__init__.py @@ -461,7 +461,7 @@ class RackElevationHelperMixin: def merge_elevations(self, face): elevation = self.get_rack_units(face=face, expand_devices=False) - other_face = DeviceFaceChoices.FACE_FRONT if face==DeviceFaceChoices.FACE_REAR else DeviceFaceChoices.FACE_REAR + other_face = DeviceFaceChoices.FACE_FRONT if face == DeviceFaceChoices.FACE_REAR else DeviceFaceChoices.FACE_REAR other = self.get_rack_units(face=other_face) unit_cursor = 0