|
@@ -1128,6 +1128,33 @@ class Device(
|
|
|
if hasattr(component, 'module_bay_types'):
|
|
if hasattr(component, 'module_bay_types'):
|
|
|
component.module_bay_types.set(src.module_bay_types.db_manager(using).all())
|
|
component.module_bay_types.set(src.module_bay_types.db_manager(using).all())
|
|
|
|
|
|
|
|
|
|
+ def _instantiate_inventory_items(self):
|
|
|
|
|
+ """
|
|
|
|
|
+ Instantiate inventory items for the device from its DeviceType's inventory item templates. Templates are
|
|
|
|
|
+ processed parents first, and each item is saved before its children are instantiated beneath it.
|
|
|
|
|
+ """
|
|
|
|
|
+ using = self._state.db
|
|
|
|
|
+ # An ltree path sorts after the paths of its ancestors
|
|
|
|
|
+ templates = list(self.device_type.inventoryitemtemplates.using(using).order_by('path'))
|
|
|
|
|
+
|
|
|
|
|
+ # Names need only be unique among siblings. The unique constraint does not cover root templates, as their
|
|
|
|
|
+ # parent is NULL, so check for duplicates before creating anything.
|
|
|
|
|
+ keys = [(t.parent_id, t.name) for t in templates]
|
|
|
|
|
+ if duplicates := {name for parent_id, name in keys if keys.count((parent_id, name)) > 1}:
|
|
|
|
|
+ raise AbortRequest(
|
|
|
|
|
+ _("Duplicate inventory item names under the same parent: {names}").format(
|
|
|
|
|
+ names=', '.join(sorted(duplicates))
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ items = {}
|
|
|
|
|
+ for template in templates:
|
|
|
|
|
+ item = template.instantiate(device=self, parent=items.get(template.parent_id))
|
|
|
|
|
+ if cf_defaults := CustomField.objects.get_defaults_for_model(InventoryItem):
|
|
|
|
|
+ item.custom_field_data = cf_defaults
|
|
|
|
|
+ item.save(using=using)
|
|
|
|
|
+ items[template.pk] = item
|
|
|
|
|
+
|
|
|
def save(self, *args, **kwargs):
|
|
def save(self, *args, **kwargs):
|
|
|
is_new = not bool(self.pk)
|
|
is_new = not bool(self.pk)
|
|
|
|
|
|
|
@@ -1165,8 +1192,7 @@ class Device(
|
|
|
# Disable bulk_create to accommodate MPTT
|
|
# Disable bulk_create to accommodate MPTT
|
|
|
self._instantiate_components(self.device_type.modulebaytemplates.all(), bulk_create=False)
|
|
self._instantiate_components(self.device_type.modulebaytemplates.all(), bulk_create=False)
|
|
|
self._instantiate_components(self.device_type.devicebaytemplates.all())
|
|
self._instantiate_components(self.device_type.devicebaytemplates.all())
|
|
|
- # Disable bulk_create to accommodate MPTT
|
|
|
|
|
- self._instantiate_components(self.device_type.inventoryitemtemplates.all(), bulk_create=False)
|
|
|
|
|
|
|
+ self._instantiate_inventory_items()
|
|
|
# Interface parents & bridges have to be set after interface instantiation. Parents are applied first so
|
|
# Interface parents & bridges have to be set after interface instantiation. Parents are applied first so
|
|
|
# that channel subinterfaces validate against a populated parent.
|
|
# that channel subinterfaces validate against a populated parent.
|
|
|
update_interface_parents(self, self.device_type.interfacetemplates.all())
|
|
update_interface_parents(self, self.device_type.interfacetemplates.all())
|