|
@@ -656,12 +656,14 @@ class Rack(ChangeLoggedModel, CustomFieldModel):
|
|
|
def get_status_class(self):
|
|
def get_status_class(self):
|
|
|
return self.STATUS_CLASS_MAP.get(self.status)
|
|
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, user=None, 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'}
|
|
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.
|
|
Each key 'device' is either a Device or None. By default, multi-U devices are repeated for each U they occupy.
|
|
|
|
|
|
|
|
:param face: Rack face (front or rear)
|
|
:param face: Rack face (front or rear)
|
|
|
|
|
+ :param user: User instance to be used for evaluating device view permissions. If None, all devices
|
|
|
|
|
+ will be included.
|
|
|
:param exclude: PK of a Device to exclude (optional); helpful when relocating a Device within a Rack
|
|
:param exclude: PK of a Device to exclude (optional); helpful when relocating a Device within a Rack
|
|
|
:param expand_devices: When True, all units that a device occupies will be listed with each containing a
|
|
: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
|
|
reference to the device. When False, only the bottom most unit for a device is included and that unit
|
|
@@ -670,10 +672,18 @@ class Rack(ChangeLoggedModel, CustomFieldModel):
|
|
|
|
|
|
|
|
elevation = OrderedDict()
|
|
elevation = OrderedDict()
|
|
|
for u in self.units:
|
|
for u in self.units:
|
|
|
- elevation[u] = {'id': u, 'name': 'U{}'.format(u), 'face': face, 'device': None}
|
|
|
|
|
|
|
+ elevation[u] = {
|
|
|
|
|
+ 'id': u,
|
|
|
|
|
+ 'name': f'U{u}',
|
|
|
|
|
+ 'face': face,
|
|
|
|
|
+ 'device': None,
|
|
|
|
|
+ 'occupied': False
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
# Add devices to rack units list
|
|
# Add devices to rack units list
|
|
|
if self.pk:
|
|
if self.pk:
|
|
|
|
|
+
|
|
|
|
|
+ # Retrieve all devices installed within the rack
|
|
|
queryset = Device.objects.prefetch_related(
|
|
queryset = Device.objects.prefetch_related(
|
|
|
'device_type',
|
|
'device_type',
|
|
|
'device_type__manufacturer',
|
|
'device_type__manufacturer',
|
|
@@ -689,12 +699,22 @@ class Rack(ChangeLoggedModel, CustomFieldModel):
|
|
|
).filter(
|
|
).filter(
|
|
|
Q(face=face) | Q(device_type__is_full_depth=True)
|
|
Q(face=face) | Q(device_type__is_full_depth=True)
|
|
|
)
|
|
)
|
|
|
|
|
+
|
|
|
|
|
+ # Determine which devices the user has permission to view
|
|
|
|
|
+ permitted_device_ids = []
|
|
|
|
|
+ if user is not None:
|
|
|
|
|
+ permitted_device_ids = self.devices.restrict(user, 'view').values_list('pk', flat=True)
|
|
|
|
|
+
|
|
|
for device in queryset:
|
|
for device in queryset:
|
|
|
if expand_devices:
|
|
if expand_devices:
|
|
|
for u in range(device.position, device.position + device.device_type.u_height):
|
|
for u in range(device.position, device.position + device.device_type.u_height):
|
|
|
- elevation[u]['device'] = device
|
|
|
|
|
|
|
+ if user is None or device.pk in permitted_device_ids:
|
|
|
|
|
+ elevation[u]['device'] = device
|
|
|
|
|
+ elevation[u]['occupied'] = True
|
|
|
else:
|
|
else:
|
|
|
- elevation[device.position]['device'] = device
|
|
|
|
|
|
|
+ if user is None or device.pk in permitted_device_ids:
|
|
|
|
|
+ elevation[device.position]['device'] = device
|
|
|
|
|
+ elevation[device.position]['occupied'] = True
|
|
|
elevation[device.position]['height'] = device.device_type.u_height
|
|
elevation[device.position]['height'] = device.device_type.u_height
|
|
|
for u in range(device.position + 1, device.position + device.device_type.u_height):
|
|
for u in range(device.position + 1, device.position + device.device_type.u_height):
|
|
|
elevation.pop(u, None)
|
|
elevation.pop(u, None)
|