racks.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. import decimal
  2. from functools import cached_property
  3. from django.conf import settings
  4. from django.contrib.contenttypes.fields import GenericRelation
  5. from django.contrib.postgres.fields import ArrayField
  6. from django.core.exceptions import ValidationError
  7. from django.core.validators import MaxValueValidator, MinValueValidator
  8. from django.db import models
  9. from django.db.models import Count
  10. from django.utils.translation import gettext_lazy as _
  11. from dcim.choices import *
  12. from dcim.constants import *
  13. from dcim.svg import RackElevationSVG
  14. from netbox.choices import ColorChoices
  15. from netbox.models import OrganizationalModel, PrimaryModel
  16. from netbox.models.mixins import WeightMixin
  17. from netbox.models.features import ContactsMixin, ImageAttachmentsMixin
  18. from utilities.conversion import to_grams
  19. from utilities.data import array_to_string, drange
  20. from utilities.fields import ColorField, CounterCacheField
  21. from utilities.tracking import TrackingModelMixin
  22. from .device_components import PowerPort
  23. from .devices import Device
  24. from .modules import Module
  25. from .power import PowerFeed
  26. __all__ = (
  27. 'Rack',
  28. 'RackReservation',
  29. 'RackRole',
  30. 'RackType',
  31. )
  32. #
  33. # Rack Types
  34. #
  35. class RackBase(WeightMixin, PrimaryModel):
  36. """
  37. Base class for RackType & Rack. Holds
  38. """
  39. width = models.PositiveSmallIntegerField(
  40. choices=RackWidthChoices,
  41. default=RackWidthChoices.WIDTH_19IN,
  42. verbose_name=_('width'),
  43. help_text=_('Rail-to-rail width')
  44. )
  45. # Numbering
  46. u_height = models.PositiveSmallIntegerField(
  47. default=RACK_U_HEIGHT_DEFAULT,
  48. verbose_name=_('height (U)'),
  49. validators=[MinValueValidator(1), MaxValueValidator(RACK_U_HEIGHT_MAX)],
  50. help_text=_('Height in rack units')
  51. )
  52. starting_unit = models.PositiveSmallIntegerField(
  53. default=RACK_STARTING_UNIT_DEFAULT,
  54. verbose_name=_('starting unit'),
  55. validators=[MinValueValidator(1)],
  56. help_text=_('Starting unit for rack')
  57. )
  58. desc_units = models.BooleanField(
  59. default=False,
  60. verbose_name=_('descending units'),
  61. help_text=_('Units are numbered top-to-bottom')
  62. )
  63. # Dimensions
  64. outer_width = models.PositiveSmallIntegerField(
  65. verbose_name=_('outer width'),
  66. blank=True,
  67. null=True,
  68. help_text=_('Outer dimension of rack (width)')
  69. )
  70. outer_height = models.PositiveSmallIntegerField(
  71. verbose_name=_('outer height'),
  72. blank=True,
  73. null=True,
  74. help_text=_('Outer dimension of rack (height)')
  75. )
  76. outer_depth = models.PositiveSmallIntegerField(
  77. verbose_name=_('outer depth'),
  78. blank=True,
  79. null=True,
  80. help_text=_('Outer dimension of rack (depth)')
  81. )
  82. outer_unit = models.CharField(
  83. verbose_name=_('outer unit'),
  84. max_length=50,
  85. choices=RackDimensionUnitChoices,
  86. blank=True,
  87. null=True
  88. )
  89. mounting_depth = models.PositiveSmallIntegerField(
  90. verbose_name=_('mounting depth'),
  91. blank=True,
  92. null=True,
  93. help_text=(_(
  94. 'Maximum depth of a mounted device, in millimeters. For four-post racks, this is the distance between the '
  95. 'front and rear rails.'
  96. ))
  97. )
  98. # Weight
  99. # WeightMixin provides weight, weight_unit, and _abs_weight
  100. max_weight = models.PositiveIntegerField(
  101. verbose_name=_('max weight'),
  102. blank=True,
  103. null=True,
  104. help_text=_('Maximum load capacity for the rack')
  105. )
  106. # Stores the normalized max weight (in grams) for database ordering
  107. _abs_max_weight = models.PositiveBigIntegerField(
  108. blank=True,
  109. null=True
  110. )
  111. class Meta:
  112. abstract = True
  113. class RackType(RackBase):
  114. """
  115. Devices are housed within Racks. Each rack has a defined height measured in rack units, and a front and rear face.
  116. Each Rack is assigned to a Site and (optionally) a Location.
  117. """
  118. form_factor = models.CharField(
  119. choices=RackFormFactorChoices,
  120. max_length=50,
  121. verbose_name=_('form factor')
  122. )
  123. manufacturer = models.ForeignKey(
  124. to='dcim.Manufacturer',
  125. on_delete=models.PROTECT,
  126. related_name='rack_types'
  127. )
  128. model = models.CharField(
  129. verbose_name=_('model'),
  130. max_length=100
  131. )
  132. slug = models.SlugField(
  133. verbose_name=_('slug'),
  134. max_length=100,
  135. unique=True
  136. )
  137. rack_count = CounterCacheField(
  138. to_model='dcim.Rack',
  139. to_field='rack_type'
  140. )
  141. clone_fields = (
  142. 'manufacturer', 'form_factor', 'width', 'u_height', 'desc_units', 'outer_width', 'outer_height', 'outer_depth',
  143. 'outer_unit', 'mounting_depth', 'weight', 'max_weight', 'weight_unit',
  144. )
  145. prerequisite_models = (
  146. 'dcim.Manufacturer',
  147. )
  148. class Meta:
  149. ordering = ('manufacturer', 'model')
  150. constraints = (
  151. models.UniqueConstraint(
  152. fields=('manufacturer', 'model'),
  153. name='%(app_label)s_%(class)s_unique_manufacturer_model'
  154. ),
  155. models.UniqueConstraint(
  156. fields=('manufacturer', 'slug'),
  157. name='%(app_label)s_%(class)s_unique_manufacturer_slug'
  158. ),
  159. )
  160. verbose_name = _('rack type')
  161. verbose_name_plural = _('rack types')
  162. def __str__(self):
  163. return self.model
  164. @property
  165. def full_name(self):
  166. return f"{self.manufacturer} {self.model}"
  167. def clean(self):
  168. super().clean()
  169. # Validate outer dimensions and unit
  170. if any([self.outer_width, self.outer_depth, self.outer_height]) and not self.outer_unit:
  171. raise ValidationError(_("Must specify a unit when setting an outer dimension"))
  172. # Validate max_weight and weight_unit
  173. if self.max_weight and not self.weight_unit:
  174. raise ValidationError(_("Must specify a unit when setting a maximum weight"))
  175. def save(self, *args, **kwargs):
  176. # Store the given max weight (if any) in grams for use in database ordering
  177. if self.max_weight and self.weight_unit:
  178. self._abs_max_weight = to_grams(self.max_weight, self.weight_unit)
  179. else:
  180. self._abs_max_weight = None
  181. # Clear unit if outer width & depth are not set
  182. if not any([self.outer_width, self.outer_depth, self.outer_height]):
  183. self.outer_unit = None
  184. super().save(*args, **kwargs)
  185. # Update all Racks associated with this RackType
  186. for rack in self.racks.all():
  187. rack.snapshot()
  188. rack.copy_racktype_attrs()
  189. rack.save()
  190. @property
  191. def units(self):
  192. """
  193. Return a list of unit numbers, top to bottom.
  194. """
  195. if self.desc_units:
  196. return drange(decimal.Decimal(self.starting_unit), self.u_height + self.starting_unit, 0.5)
  197. return drange(self.u_height + decimal.Decimal(0.5) + self.starting_unit - 1, 0.5 + self.starting_unit - 1, -0.5)
  198. #
  199. # Racks
  200. #
  201. class RackRole(OrganizationalModel):
  202. """
  203. Racks can be organized by functional role, similar to Devices.
  204. """
  205. color = ColorField(
  206. verbose_name=_('color'),
  207. default=ColorChoices.COLOR_GREY
  208. )
  209. class Meta:
  210. ordering = ('name',)
  211. verbose_name = _('rack role')
  212. verbose_name_plural = _('rack roles')
  213. class Rack(ContactsMixin, ImageAttachmentsMixin, TrackingModelMixin, RackBase):
  214. """
  215. Devices are housed within Racks. Each rack has a defined height measured in rack units, and a front and rear face.
  216. Each Rack is assigned to a Site and (optionally) a Location.
  217. """
  218. # Fields which cannot be set locally if a RackType is assigned
  219. RACKTYPE_FIELDS = (
  220. 'form_factor', 'width', 'u_height', 'starting_unit', 'desc_units', 'outer_width', 'outer_height',
  221. 'outer_depth', 'outer_unit', 'mounting_depth', 'weight', 'weight_unit', 'max_weight',
  222. )
  223. form_factor = models.CharField(
  224. choices=RackFormFactorChoices,
  225. max_length=50,
  226. blank=True,
  227. null=True,
  228. verbose_name=_('form factor')
  229. )
  230. rack_type = models.ForeignKey(
  231. to='dcim.RackType',
  232. on_delete=models.PROTECT,
  233. related_name='racks',
  234. blank=True,
  235. null=True,
  236. )
  237. name = models.CharField(
  238. verbose_name=_('name'),
  239. max_length=100,
  240. db_collation="natural_sort"
  241. )
  242. facility_id = models.CharField(
  243. max_length=50,
  244. blank=True,
  245. null=True,
  246. verbose_name=_('facility ID'),
  247. help_text=_("Locally-assigned identifier")
  248. )
  249. site = models.ForeignKey(
  250. to='dcim.Site',
  251. on_delete=models.PROTECT,
  252. related_name='racks'
  253. )
  254. location = models.ForeignKey(
  255. to='dcim.Location',
  256. on_delete=models.SET_NULL,
  257. related_name='racks',
  258. blank=True,
  259. null=True
  260. )
  261. tenant = models.ForeignKey(
  262. to='tenancy.Tenant',
  263. on_delete=models.PROTECT,
  264. related_name='racks',
  265. blank=True,
  266. null=True
  267. )
  268. status = models.CharField(
  269. verbose_name=_('status'),
  270. max_length=50,
  271. choices=RackStatusChoices,
  272. default=RackStatusChoices.STATUS_ACTIVE
  273. )
  274. role = models.ForeignKey(
  275. to='dcim.RackRole',
  276. on_delete=models.PROTECT,
  277. related_name='racks',
  278. blank=True,
  279. null=True,
  280. help_text=_('Functional role')
  281. )
  282. serial = models.CharField(
  283. max_length=50,
  284. blank=True,
  285. verbose_name=_('serial number')
  286. )
  287. asset_tag = models.CharField(
  288. max_length=50,
  289. blank=True,
  290. null=True,
  291. unique=True,
  292. verbose_name=_('asset tag'),
  293. help_text=_('A unique tag used to identify this rack')
  294. )
  295. airflow = models.CharField(
  296. verbose_name=_('airflow'),
  297. max_length=50,
  298. choices=RackAirflowChoices,
  299. blank=True,
  300. null=True
  301. )
  302. # Generic relations
  303. vlan_groups = GenericRelation(
  304. to='ipam.VLANGroup',
  305. content_type_field='scope_type',
  306. object_id_field='scope_id',
  307. related_query_name='rack'
  308. )
  309. clone_fields = (
  310. 'site', 'location', 'tenant', 'status', 'role', 'form_factor', 'width', 'airflow', 'u_height', 'desc_units',
  311. 'outer_width', 'outer_height', 'outer_depth', 'outer_unit', 'mounting_depth', 'weight', 'max_weight',
  312. 'weight_unit',
  313. )
  314. prerequisite_models = (
  315. 'dcim.Site',
  316. )
  317. class Meta:
  318. ordering = ('site', 'location', 'name', 'pk') # (site, location, name) may be non-unique
  319. constraints = (
  320. # Name and facility_id must be unique *only* within a Location
  321. models.UniqueConstraint(
  322. fields=('location', 'name'),
  323. name='%(app_label)s_%(class)s_unique_location_name'
  324. ),
  325. models.UniqueConstraint(
  326. fields=('location', 'facility_id'),
  327. name='%(app_label)s_%(class)s_unique_location_facility_id'
  328. ),
  329. )
  330. verbose_name = _('rack')
  331. verbose_name_plural = _('racks')
  332. def __str__(self):
  333. if self.facility_id:
  334. return f'{self.name} ({self.facility_id})'
  335. return self.name
  336. def clean(self):
  337. super().clean()
  338. # Validate location/site assignment
  339. if self.site and self.location and self.location.site != self.site:
  340. raise ValidationError(_("Assigned location must belong to parent site ({site}).").format(site=self.site))
  341. # Validate outer dimensions and unit
  342. if any([self.outer_width, self.outer_depth, self.outer_height]) and not self.outer_unit:
  343. raise ValidationError(_("Must specify a unit when setting an outer dimension"))
  344. # Validate max_weight and weight_unit
  345. if self.max_weight and not self.weight_unit:
  346. raise ValidationError(_("Must specify a unit when setting a maximum weight"))
  347. if not self._state.adding:
  348. mounted_devices = Device.objects.filter(rack=self).exclude(position__isnull=True).order_by('position')
  349. effective_u_height = self.rack_type.u_height if self.rack_type else self.u_height
  350. effective_starting_unit = self.rack_type.starting_unit if self.rack_type else self.starting_unit
  351. # Validate that Rack is tall enough to house the highest mounted Device
  352. if top_device := mounted_devices.last():
  353. min_height = top_device.position + top_device.device_type.u_height - effective_starting_unit
  354. if effective_u_height < min_height:
  355. field = 'rack_type' if self.rack_type else 'u_height'
  356. raise ValidationError({
  357. field: _(
  358. "Rack must be at least {min_height}U tall to house currently installed devices."
  359. ).format(min_height=min_height)
  360. })
  361. # Validate that the Rack's starting unit is less than or equal to the position of the lowest mounted Device
  362. if last_device := mounted_devices.first():
  363. if effective_starting_unit > last_device.position:
  364. field = 'rack_type' if self.rack_type else 'starting_unit'
  365. raise ValidationError({
  366. field: _("Rack unit numbering must begin at {position} or less to house "
  367. "currently installed devices.").format(position=last_device.position)
  368. })
  369. # Validate that Rack was assigned a Location of its same site, if applicable
  370. if self.location:
  371. if self.location.site != self.site:
  372. raise ValidationError({
  373. 'location': _("Location must be from the same site, {site}.").format(site=self.site)
  374. })
  375. def save(self, *args, **kwargs):
  376. self.copy_racktype_attrs()
  377. # Store the given max weight (if any) in grams for use in database ordering
  378. if self.max_weight and self.weight_unit:
  379. self._abs_max_weight = to_grams(self.max_weight, self.weight_unit)
  380. else:
  381. self._abs_max_weight = None
  382. # Clear unit if outer width & depth are not set
  383. if not any([self.outer_width, self.outer_depth, self.outer_height]):
  384. self.outer_unit = None
  385. super().save(*args, **kwargs)
  386. def copy_racktype_attrs(self):
  387. """
  388. Copy physical attributes from the assigned RackType (if any).
  389. """
  390. if self.rack_type:
  391. for field_name in self.RACKTYPE_FIELDS:
  392. setattr(self, field_name, getattr(self.rack_type, field_name))
  393. @property
  394. def units(self):
  395. """
  396. Return a list of unit numbers, top to bottom.
  397. """
  398. if self.desc_units:
  399. return drange(decimal.Decimal(self.starting_unit), self.u_height + self.starting_unit, 0.5)
  400. return drange(self.u_height + decimal.Decimal(0.5) + self.starting_unit - 1, 0.5 + self.starting_unit - 1, -0.5)
  401. def get_status_color(self):
  402. return RackStatusChoices.colors.get(self.status)
  403. def get_rack_units(self, user=None, face=DeviceFaceChoices.FACE_FRONT, exclude=None, expand_devices=True):
  404. """
  405. Return a list of rack units as dictionaries. Example: {'device': None, 'face': 0, 'id': 48, 'name': 'U48'}
  406. Each key 'device' is either a Device or None. By default, multi-U devices are repeated for each U they occupy.
  407. :param face: Rack face (front or rear)
  408. :param user: User instance to be used for evaluating device view permissions. If None, all devices
  409. will be included.
  410. :param exclude: PK of a Device to exclude (optional); helpful when relocating a Device within a Rack
  411. :param expand_devices: When True, all units that a device occupies will be listed with each containing a
  412. reference to the device. When False, only the bottom most unit for a device is included and that unit
  413. contains a height attribute for the device
  414. """
  415. elevation = {}
  416. for u in self.units:
  417. u_name = f'U{u}'.split('.')[0] if not u % 1 else f'U{u}'
  418. elevation[u] = {
  419. 'id': u,
  420. 'name': u_name,
  421. 'face': face,
  422. 'device': None,
  423. 'occupied': False
  424. }
  425. # Add devices to rack units list
  426. if not self._state.adding:
  427. # Retrieve all devices installed within the rack
  428. devices = Device.objects.prefetch_related(
  429. 'device_type',
  430. 'device_type__manufacturer',
  431. 'role'
  432. ).annotate(
  433. devicebay_count=Count('devicebays')
  434. ).exclude(
  435. pk=exclude
  436. ).filter(
  437. rack=self,
  438. position__gt=0,
  439. device_type__u_height__gt=0
  440. ).filter(
  441. Q(face=face) | Q(device_type__is_full_depth=True)
  442. )
  443. # Determine which devices the user has permission to view
  444. permitted_device_ids = []
  445. if user is not None:
  446. permitted_device_ids = self.devices.restrict(user, 'view').values_list('pk', flat=True)
  447. for device in devices:
  448. if expand_devices:
  449. for u in drange(device.position, device.position + device.device_type.u_height, 0.5):
  450. if user is None or device.pk in permitted_device_ids:
  451. elevation[u]['device'] = device
  452. elevation[u]['occupied'] = True
  453. else:
  454. if user is None or device.pk in permitted_device_ids:
  455. elevation[device.position]['device'] = device
  456. elevation[device.position]['occupied'] = True
  457. elevation[device.position]['height'] = device.device_type.u_height
  458. return [u for u in elevation.values()]
  459. def get_available_units(self, u_height=1.0, rack_face=None, exclude=None, ignore_excluded_devices=False):
  460. """
  461. Return a list of units within the rack available to accommodate a device of a given U height (default 1).
  462. Optionally exclude one or more devices when calculating empty units (needed when moving a device from one
  463. position to another within a rack).
  464. :param u_height: Minimum number of contiguous free units required
  465. :param rack_face: The face of the rack (front or rear) required; 'None' if device is full depth
  466. :param exclude: List of devices IDs to exclude (useful when moving a device within a rack)
  467. :param ignore_excluded_devices: Ignore devices that are marked to exclude from utilization calculations
  468. """
  469. # Gather all devices which consume U space within the rack
  470. devices = self.devices.prefetch_related('device_type').filter(position__gte=1)
  471. if ignore_excluded_devices:
  472. devices = devices.exclude(device_type__exclude_from_utilization=True)
  473. if exclude is not None:
  474. devices = devices.exclude(pk__in=exclude)
  475. # Initialize the rack unit skeleton
  476. units = list(self.units)
  477. # Remove units consumed by installed devices
  478. for d in devices:
  479. if rack_face is None or d.face == rack_face or d.device_type.is_full_depth:
  480. for u in drange(d.position, d.position + d.device_type.u_height, 0.5):
  481. try:
  482. units.remove(u)
  483. except ValueError:
  484. # Found overlapping devices in the rack!
  485. pass
  486. # Remove units without enough space above them to accommodate a device of the specified height
  487. available_units = []
  488. for u in units:
  489. if set(drange(u, u + decimal.Decimal(u_height), 0.5)).issubset(units):
  490. available_units.append(u)
  491. return list(reversed(available_units))
  492. def get_reserved_units(self):
  493. """
  494. Return a dictionary mapping all reserved units within the rack to their reservation.
  495. """
  496. reserved_units = {}
  497. for reservation in self.reservations.all():
  498. for u in reservation.units:
  499. reserved_units[u] = reservation
  500. return reserved_units
  501. def get_elevation_svg(
  502. self,
  503. face=DeviceFaceChoices.FACE_FRONT,
  504. user=None,
  505. unit_width=None,
  506. unit_height=None,
  507. legend_width=RACK_ELEVATION_DEFAULT_LEGEND_WIDTH,
  508. margin_width=RACK_ELEVATION_DEFAULT_MARGIN_WIDTH,
  509. include_images=True,
  510. base_url=None,
  511. highlight_params=None
  512. ):
  513. """
  514. Return an SVG of the rack elevation
  515. :param face: Enum of [front, rear] representing the desired side of the rack elevation to render
  516. :param user: User instance to be used for evaluating device view permissions. If None, all devices
  517. will be included.
  518. :param unit_width: Width in pixels for the rendered drawing
  519. :param unit_height: Height of each rack unit for the rendered drawing. Note this is not the total
  520. height of the elevation
  521. :param legend_width: Width of the unit legend, in pixels
  522. :param margin_width: Width of the right-hand margin, in pixels
  523. :param include_images: Embed front/rear device images where available
  524. :param base_url: Base URL for links and images. If none, URLs will be relative.
  525. :param highlight_params: Dictionary of parameters to be passed to the RackElevationSVG.render_highlight() method
  526. """
  527. elevation = RackElevationSVG(
  528. self,
  529. unit_width=unit_width,
  530. unit_height=unit_height,
  531. legend_width=legend_width,
  532. margin_width=margin_width,
  533. user=user,
  534. include_images=include_images,
  535. base_url=base_url,
  536. highlight_params=highlight_params
  537. )
  538. return elevation.render(face)
  539. def get_0u_devices(self):
  540. return self.devices.filter(position=0)
  541. def get_utilization(self):
  542. """
  543. Determine the utilization rate of the rack and return it as a percentage. Occupied and reserved units both count
  544. as utilized.
  545. """
  546. # Determine unoccupied units
  547. total_units = len(list(self.units))
  548. available_units = self.get_available_units(u_height=0.5, ignore_excluded_devices=True)
  549. # Remove reserved units
  550. for ru in self.get_reserved_units():
  551. for u in drange(ru, ru + 1, 0.5):
  552. if u in available_units:
  553. available_units.remove(u)
  554. occupied_unit_count = total_units - len(available_units)
  555. percentage = float(occupied_unit_count) / total_units * 100
  556. return percentage
  557. def get_power_utilization(self):
  558. """
  559. Determine the utilization rate of power in the rack and return it as a percentage.
  560. """
  561. powerfeeds = PowerFeed.objects.filter(rack=self)
  562. available_power_total = sum(pf.available_power for pf in powerfeeds)
  563. if not available_power_total:
  564. return 0
  565. powerports = []
  566. for powerfeed in powerfeeds:
  567. powerports.extend([
  568. peer for peer in powerfeed.link_peers if isinstance(peer, PowerPort)
  569. ])
  570. allocated_draw = sum([
  571. powerport.get_power_draw()['allocated'] for powerport in powerports
  572. ])
  573. return round(allocated_draw / available_power_total * 100, 1)
  574. @cached_property
  575. def total_weight(self):
  576. total_weight = sum(
  577. device.device_type._abs_weight
  578. for device in self.devices.exclude(device_type___abs_weight__isnull=True).prefetch_related('device_type')
  579. )
  580. total_weight += sum(
  581. module.module_type._abs_weight
  582. for module in Module.objects.filter(device__rack=self)
  583. .exclude(module_type___abs_weight__isnull=True)
  584. .prefetch_related('module_type')
  585. )
  586. if self._abs_weight:
  587. total_weight += self._abs_weight
  588. return round(total_weight / 1000, 2)
  589. class RackReservation(PrimaryModel):
  590. """
  591. One or more reserved units within a Rack.
  592. """
  593. rack = models.ForeignKey(
  594. to='dcim.Rack',
  595. on_delete=models.CASCADE,
  596. related_name='reservations'
  597. )
  598. units = ArrayField(
  599. verbose_name=_('units'),
  600. base_field=models.PositiveSmallIntegerField()
  601. )
  602. status = models.CharField(
  603. verbose_name=_('status'),
  604. max_length=50,
  605. choices=RackReservationStatusChoices,
  606. default=RackReservationStatusChoices.STATUS_ACTIVE
  607. )
  608. tenant = models.ForeignKey(
  609. to='tenancy.Tenant',
  610. on_delete=models.PROTECT,
  611. related_name='rackreservations',
  612. blank=True,
  613. null=True
  614. )
  615. user = models.ForeignKey(
  616. to=settings.AUTH_USER_MODEL,
  617. on_delete=models.PROTECT
  618. )
  619. description = models.CharField(
  620. verbose_name=_('description'),
  621. max_length=200
  622. )
  623. clone_fields = ('rack', 'user', 'tenant')
  624. prerequisite_models = (
  625. 'dcim.Rack',
  626. )
  627. class Meta:
  628. ordering = ['created', 'pk']
  629. verbose_name = _('rack reservation')
  630. verbose_name_plural = _('rack reservations')
  631. def __str__(self):
  632. return "Reservation for rack {}".format(self.rack)
  633. def clean(self):
  634. super().clean()
  635. if hasattr(self, 'rack') and self.units:
  636. # Validate that all specified units exist in the Rack.
  637. invalid_units = [u for u in self.units if u not in self.rack.units]
  638. if invalid_units:
  639. raise ValidationError({
  640. 'units': _("Invalid unit(s) for {height}U rack: {unit_list}").format(
  641. height=self.rack.u_height,
  642. unit_list=', '.join([str(u) for u in invalid_units])
  643. ),
  644. })
  645. # Check that none of the units has already been reserved for this Rack.
  646. reserved_units = []
  647. for resv in self.rack.reservations.exclude(pk=self.pk):
  648. reserved_units += resv.units
  649. conflicting_units = [u for u in self.units if u in reserved_units]
  650. if conflicting_units:
  651. raise ValidationError({
  652. 'units': _('The following units have already been reserved: {unit_list}').format(
  653. unit_list=', '.join([str(u) for u in conflicting_units])
  654. )
  655. })
  656. @property
  657. def unit_list(self):
  658. return array_to_string(self.units)
  659. def get_status_color(self):
  660. return RackReservationStatusChoices.colors.get(self.status)
  661. def to_objectchange(self, action):
  662. objectchange = super().to_objectchange(action)
  663. objectchange.related_object = self.rack
  664. return objectchange