Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split reassign form in fieldsets #180

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions netbox_inventory/forms/reassign.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

from dcim.models import Device, InventoryItem, Module, Site, Location, Manufacturer
from netbox.forms import NetBoxModelForm
from utilities.forms.fields import DynamicModelChoiceField
from utilities.forms.rendering import FieldSet
from ..choices import AssetStatusChoices
from ..models import Asset, InventoryItemType, InventoryItemGroup
from ..utils import get_status_for
Expand Down Expand Up @@ -31,10 +33,16 @@ class AssetReassignMixin(forms.Form):
asset_status = forms.ChoiceField(
choices=AssetStatusChoices,
initial=get_status_for('stored'),
label='Old Asset Status',
label='Status',
help_text='Status to set to existing asset that is being unassigned',
)

fieldsets = (
FieldSet('storage_site', 'storage_location', 'assigned_asset',
name=_('New Asset')),
FieldSet('asset_status', name=_('Old Asset')),
)

class Meta:
fields = ('storage_site', 'storage_location', 'assigned_asset', 'asset_status')

Expand Down Expand Up @@ -113,7 +121,8 @@ def __init__(self, *args, **kwargs):
self.instance.assigned_asset
except Asset.DoesNotExist:
# no asset currently assigned, hide status field for old asset
self.fields['asset_status'].widget = forms.HiddenInput()
self.fields.pop('asset_status')
self.fieldsets = (self.fieldsets[0],)


class AssetDeviceReassignForm(AssetReassignMixin, NetBoxModelForm):
Expand Down Expand Up @@ -215,6 +224,13 @@ class AssetInventoryItemReassignForm(AssetReassignMixin, NetBoxModelForm):
help_text='New asset to assign to inventory item. Set to blank to remove assignment.',
)

fieldsets = (
FieldSet('manufacturer', 'inventoryitem_group', 'inventoryitem_type',
'storage_site', 'storage_location', 'assigned_asset',
name=_('New Asset')),
FieldSet('asset_status', name=_('Old Asset')),
)

class Meta:
model = InventoryItem
fields = ('manufacturer', 'inventoryitem_group', 'inventoryitem_type') + AssetReassignMixin.Meta.fields