Skip to content

Commit

Permalink
allow changing device_type and module_type when reassigning assets to…
Browse files Browse the repository at this point in the history
… device or module
  • Loading branch information
matejv committed Dec 11, 2024
1 parent 6833222 commit 0833c21
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
26 changes: 4 additions & 22 deletions netbox_inventory/forms/reassign.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def _clean_asset(self, asset, instance):
try:
# update hardware assignment and validate data
setattr(asset, asset.kind, instance)
# signal to assset.clean() methods to not validate _type match beetween asset and hw
asset._in_reassign = True
asset.full_clean(exclude=(asset.kind,))
except ValidationError as e:
# ValidationError raised for device or module field
Expand Down Expand Up @@ -133,7 +135,6 @@ class AssetDeviceReassignForm(AssetReassignMixin, NetBoxModelForm):
query_params={
'kind': 'device',
'is_assigned': False,
'device_type_id': '$device_type',
'storage_site_id': '$storage_site',
'storage_location_id': '$storage_location',
},
Expand All @@ -143,16 +144,7 @@ class AssetDeviceReassignForm(AssetReassignMixin, NetBoxModelForm):

class Meta:
model = Device
# device_type is here only to allow setting assigned_asset query_params on device_type
fields = AssetReassignMixin.Meta.fields + ('device_type',)
widgets = {'device_type': forms.HiddenInput,}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# limit asset selection based on current device.device_type
self.initial['device_type'] = self.instance.device_type_id
assigned_asset_field = self.fields['assigned_asset']
assigned_asset_field.queryset = Asset.objects.filter(device_type=self.instance.device_type, device__isnull=True)
fields = AssetReassignMixin.Meta.fields


class AssetModuleReassignForm(AssetReassignMixin, NetBoxModelForm):
Expand All @@ -163,7 +155,6 @@ class AssetModuleReassignForm(AssetReassignMixin, NetBoxModelForm):
query_params={
'kind': 'module',
'is_assigned': False,
'module_type_id': '$module_type',
'storage_site_id': '$storage_site',
'storage_location_id': '$storage_location',
},
Expand All @@ -173,16 +164,7 @@ class AssetModuleReassignForm(AssetReassignMixin, NetBoxModelForm):

class Meta:
model = Module
# module_type is here only to allow setting assigned_asset query_params on module_type
fields = AssetReassignMixin.Meta.fields + ('module_type',)
widgets = {'module_type': forms.HiddenInput,}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# limit asset selection based on current module.module_type
self.initial['module_type'] = self.instance.module_type_id
assigned_asset_field = self.fields['assigned_asset']
assigned_asset_field.queryset = Asset.objects.filter(module_type=self.instance.module_type, module__isnull=True)
fields = AssetReassignMixin.Meta.fields


class AssetInventoryItemReassignForm(AssetReassignMixin, NetBoxModelForm):
Expand Down
7 changes: 5 additions & 2 deletions netbox_inventory/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,11 @@ def validate_hardware(self):

# e.g.: self.device_type and self.device.device_type must match
# InventoryItem does not have FK to InventoryItemType
if kind != 'inventoryitem' and hw and _type != getattr(hw, kind+'_type'):
raise ValidationError({kind: f'{kind} type of {kind} does not match {kind} type of asset'})
if kind != 'inventoryitem':
if not getattr(self, '_in_reassign', False):
# but don't check if we are reassigning asset to another device
if hw and _type != getattr(hw, kind+'_type'):
raise ValidationError({kind: f'{kind} type of {kind} does not match {kind} type of asset'})
# ensure only one hardware is set and that it is correct kind
# e.g. if self.device_type is set, we cannot have self.module or self.inventoryitem set
for hw_other in hw_others:
Expand Down
7 changes: 7 additions & 0 deletions netbox_inventory/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ def asset_set_new_hw(asset, hw):
if hw.asset_tag != new_asset_tag:
hw.asset_tag = new_asset_tag
hw_save = True
# handle changing of model (<kind>_type)
if asset.kind in ['device', 'module']:
asset_type = getattr(asset, asset.kind+'_type')
hw_type = getattr(hw, asset.kind+'_type')
if asset_type != hw_type:
setattr(hw, asset.kind+'_type', asset_type)
hw_save = True
# for inventory items also set manufacturer and part_number
if asset.inventoryitem_type:
if hw.manufacturer != asset.inventoryitem_type.manufacturer:
Expand Down

0 comments on commit 0833c21

Please sign in to comment.