#12255 allow inventory item template to change devices

This commit is contained in:
Arthur 2023-04-19 10:58:50 -07:00
parent f3413e1851
commit 700ee1237f

View File

@ -59,6 +59,7 @@ class ComponentTemplateModel(WebhooksMixin, ChangeLoggedModel):
max_length=200, max_length=200,
blank=True blank=True
) )
_can_switch_device = False
class Meta: class Meta:
abstract = True abstract = True
@ -86,6 +87,14 @@ class ComponentTemplateModel(WebhooksMixin, ChangeLoggedModel):
objectchange.related_object = self.device_type objectchange.related_object = self.device_type
return objectchange return objectchange
def clean(self):
super().clean()
if (not self._can_switch_device) and (self.pk is not None) and (self._original_device != self.device_id):
raise ValidationError({
"device_type": "Component templates cannot be moved to a different device type."
})
class ModularComponentTemplateModel(ComponentTemplateModel): class ModularComponentTemplateModel(ComponentTemplateModel):
""" """
@ -137,11 +146,6 @@ class ModularComponentTemplateModel(ComponentTemplateModel):
def clean(self): def clean(self):
super().clean() super().clean()
if self.pk is not None and self._original_device_type != self.device_type_id:
raise ValidationError({
"device_type": "Component templates cannot be moved to a different device type."
})
# A component template must belong to a DeviceType *or* to a ModuleType # A component template must belong to a DeviceType *or* to a ModuleType
if self.device_type and self.module_type: if self.device_type and self.module_type:
raise ValidationError( raise ValidationError(
@ -638,6 +642,7 @@ class InventoryItemTemplate(MPTTModel, ComponentTemplateModel):
objects = TreeManager() objects = TreeManager()
component_model = InventoryItem component_model = InventoryItem
_can_switch_device = True
class Meta: class Meta:
ordering = ('device_type__id', 'parent__id', '_name') ordering = ('device_type__id', 'parent__id', '_name')