Clean up nullable fields declaration for bulk edit forms

This commit is contained in:
jeremystretch 2022-01-28 16:47:54 -05:00
parent f4776731ec
commit e4eee1cdfc
9 changed files with 120 additions and 121 deletions

View File

@ -48,9 +48,9 @@ class ProviderBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'asn', 'account', 'portal_url', 'noc_contact', 'admin_contact', 'comments',
]
)
class ProviderNetworkBulkEditForm(NetBoxModelBulkEditForm):
@ -76,9 +76,9 @@ class ProviderNetworkBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'service_id', 'description', 'comments',
]
)
class CircuitTypeBulkEditForm(NetBoxModelBulkEditForm):
@ -92,7 +92,7 @@ class CircuitTypeBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['description']
nullable_fields = ('description',)
class CircuitBulkEditForm(NetBoxModelBulkEditForm):
@ -132,6 +132,6 @@ class CircuitBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'tenant', 'commit_rate', 'description', 'comments',
]
)

View File

@ -72,7 +72,7 @@ class RegionBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['parent', 'description']
nullable_fields = ('parent', 'description')
class SiteGroupBulkEditForm(NetBoxModelBulkEditForm):
@ -90,7 +90,7 @@ class SiteGroupBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['parent', 'description']
nullable_fields = ('parent', 'description')
class SiteBulkEditForm(NetBoxModelBulkEditForm):
@ -132,9 +132,9 @@ class SiteBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'region', 'group', 'tenant', 'asns', 'description', 'time_zone',
]
)
class LocationBulkEditForm(NetBoxModelBulkEditForm):
@ -163,7 +163,7 @@ class LocationBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['parent', 'tenant', 'description']
nullable_fields = ('parent', 'tenant', 'description')
class RackRoleBulkEditForm(NetBoxModelBulkEditForm):
@ -180,7 +180,7 @@ class RackRoleBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['color', 'description']
nullable_fields = ('color', 'description')
class RackBulkEditForm(NetBoxModelBulkEditForm):
@ -278,9 +278,9 @@ class RackBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'location', 'tenant', 'role', 'serial', 'asset_tag', 'outer_width', 'outer_depth', 'outer_unit', 'comments',
]
)
class RackReservationBulkEditForm(NetBoxModelBulkEditForm):
@ -304,9 +304,6 @@ class RackReservationBulkEditForm(NetBoxModelBulkEditForm):
required=False
)
class Meta:
nullable_fields = []
class ManufacturerBulkEditForm(NetBoxModelBulkEditForm):
pk = forms.ModelMultipleChoiceField(
@ -319,7 +316,7 @@ class ManufacturerBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['description']
nullable_fields = ('description',)
class DeviceTypeBulkEditForm(NetBoxModelBulkEditForm):
@ -350,7 +347,7 @@ class DeviceTypeBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['part_number', 'airflow']
nullable_fields = ('part_number', 'airflow')
class ModuleTypeBulkEditForm(NetBoxModelBulkEditForm):
@ -367,7 +364,7 @@ class ModuleTypeBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['part_number']
nullable_fields = ('part_number',)
class DeviceRoleBulkEditForm(NetBoxModelBulkEditForm):
@ -389,7 +386,7 @@ class DeviceRoleBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['color', 'description']
nullable_fields = ('color', 'description')
class PlatformBulkEditForm(NetBoxModelBulkEditForm):
@ -412,7 +409,7 @@ class PlatformBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['manufacturer', 'napalm_driver', 'description']
nullable_fields = ('manufacturer', 'napalm_driver', 'description')
class DeviceBulkEditForm(NetBoxModelBulkEditForm):
@ -471,9 +468,9 @@ class DeviceBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'tenant', 'platform', 'serial', 'airflow',
]
)
class ModuleBulkEditForm(NetBoxModelBulkEditForm):
@ -499,7 +496,7 @@ class ModuleBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['serial']
nullable_fields = ('serial',)
class CableBulkEditForm(NetBoxModelBulkEditForm):
@ -542,9 +539,9 @@ class CableBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'type', 'status', 'tenant', 'label', 'color', 'length',
]
)
def clean(self):
super().clean()
@ -569,7 +566,7 @@ class VirtualChassisBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['domain']
nullable_fields = ('domain',)
class PowerPanelBulkEditForm(NetBoxModelBulkEditForm):
@ -608,7 +605,7 @@ class PowerPanelBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['location']
nullable_fields = ('location',)
class PowerFeedBulkEditForm(NetBoxModelBulkEditForm):
@ -667,9 +664,9 @@ class PowerFeedBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'location', 'comments',
]
)
#
@ -930,7 +927,7 @@ class InventoryItemTemplateBulkEditForm(BulkEditForm):
)
class Meta:
nullable_fields = ['label', 'role', 'manufacturer', 'part_id', 'description']
nullable_fields = ('label', 'role', 'manufacturer', 'part_id', 'description')
#
@ -951,7 +948,7 @@ class ConsolePortBulkEditForm(
)
class Meta:
nullable_fields = ['label', 'description']
nullable_fields = ('label', 'description')
class ConsoleServerPortBulkEditForm(
@ -968,7 +965,7 @@ class ConsoleServerPortBulkEditForm(
)
class Meta:
nullable_fields = ['label', 'description']
nullable_fields = ('label', 'description')
class PowerPortBulkEditForm(
@ -985,7 +982,7 @@ class PowerPortBulkEditForm(
)
class Meta:
nullable_fields = ['label', 'description']
nullable_fields = ('label', 'description')
class PowerOutletBulkEditForm(
@ -1008,7 +1005,7 @@ class PowerOutletBulkEditForm(
)
class Meta:
nullable_fields = ['label', 'type', 'feed_leg', 'power_port', 'description']
nullable_fields = ('label', 'type', 'feed_leg', 'power_port', 'description')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@ -1088,10 +1085,11 @@ class InterfaceBulkEditForm(
)
class Meta:
nullable_fields = [
'label', 'parent', 'bridge', 'lag', 'speed', 'duplex', 'mac_address', 'wwn', 'mtu', 'description', 'mode', 'rf_channel',
'rf_channel_frequency', 'rf_channel_width', 'tx_power', 'untagged_vlan', 'tagged_vlans', 'vrf',
]
nullable_fields = (
'label', 'parent', 'bridge', 'lag', 'speed', 'duplex', 'mac_address', 'wwn', 'mtu', 'description', 'mode',
'rf_channel', 'rf_channel_frequency', 'rf_channel_width', 'tx_power', 'untagged_vlan', 'tagged_vlans',
'vrf',
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@ -1157,7 +1155,7 @@ class FrontPortBulkEditForm(
)
class Meta:
nullable_fields = ['label', 'description']
nullable_fields = ('label', 'description')
class RearPortBulkEditForm(
@ -1170,7 +1168,7 @@ class RearPortBulkEditForm(
)
class Meta:
nullable_fields = ['label', 'description']
nullable_fields = ('label', 'description')
class ModuleBayBulkEditForm(
@ -1183,7 +1181,7 @@ class ModuleBayBulkEditForm(
)
class Meta:
nullable_fields = ['label', 'position', 'description']
nullable_fields = ('label', 'position', 'description')
class DeviceBayBulkEditForm(
@ -1196,7 +1194,7 @@ class DeviceBayBulkEditForm(
)
class Meta:
nullable_fields = ['label', 'description']
nullable_fields = ('label', 'description')
class InventoryItemBulkEditForm(
@ -1217,7 +1215,7 @@ class InventoryItemBulkEditForm(
)
class Meta:
nullable_fields = ['label', 'role', 'manufacturer', 'part_id', 'description']
nullable_fields = ('label', 'role', 'manufacturer', 'part_id', 'description')
#
@ -1238,4 +1236,4 @@ class InventoryItemRoleBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['color', 'description']
nullable_fields = ('color', 'description')

View File

@ -34,7 +34,7 @@ class CustomFieldBulkEditForm(BulkEditForm):
)
class Meta:
nullable_fields = []
nullable_fields = ('description',)
class CustomLinkBulkEditForm(BulkEditForm):
@ -64,9 +64,6 @@ class CustomLinkBulkEditForm(BulkEditForm):
widget=StaticSelect()
)
class Meta:
nullable_fields = []
class ExportTemplateBulkEditForm(BulkEditForm):
pk = forms.ModelMultipleChoiceField(
@ -96,7 +93,7 @@ class ExportTemplateBulkEditForm(BulkEditForm):
)
class Meta:
nullable_fields = ['description', 'mime_type', 'file_extension']
nullable_fields = ('description', 'mime_type', 'file_extension')
class WebhookBulkEditForm(BulkEditForm):
@ -139,7 +136,7 @@ class WebhookBulkEditForm(BulkEditForm):
)
class Meta:
nullable_fields = ['secret', 'conditions', 'ca_file_path']
nullable_fields = ('secret', 'conditions', 'ca_file_path')
class TagBulkEditForm(BulkEditForm):
@ -156,7 +153,7 @@ class TagBulkEditForm(BulkEditForm):
)
class Meta:
nullable_fields = ['description']
nullable_fields = ('description',)
class ConfigContextBulkEditForm(BulkEditForm):
@ -178,9 +175,7 @@ class ConfigContextBulkEditForm(BulkEditForm):
)
class Meta:
nullable_fields = [
'description',
]
nullable_fields = ('description',)
class JournalEntryBulkEditForm(BulkEditForm):
@ -196,6 +191,3 @@ class JournalEntryBulkEditForm(BulkEditForm):
required=False,
widget=forms.Textarea()
)
class Meta:
nullable_fields = []

View File

@ -50,9 +50,9 @@ class VRFBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'tenant', 'description',
]
)
class RouteTargetBulkEditForm(NetBoxModelBulkEditForm):
@ -70,9 +70,9 @@ class RouteTargetBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'tenant', 'description',
]
)
class RIRBulkEditForm(NetBoxModelBulkEditForm):
@ -90,7 +90,7 @@ class RIRBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['is_private', 'description']
nullable_fields = ('is_private', 'description')
class ASNBulkEditForm(NetBoxModelBulkEditForm):
@ -117,9 +117,9 @@ class ASNBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'date_added', 'description',
]
)
widgets = {
'date_added': DatePicker(),
}
@ -148,9 +148,9 @@ class AggregateBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'date_added', 'description',
]
)
widgets = {
'date_added': DatePicker(),
}
@ -170,7 +170,7 @@ class RoleBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['description']
nullable_fields = ('description',)
class PrefixBulkEditForm(NetBoxModelBulkEditForm):
@ -233,9 +233,9 @@ class PrefixBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'site', 'vrf', 'tenant', 'role', 'description',
]
)
class IPRangeBulkEditForm(NetBoxModelBulkEditForm):
@ -267,9 +267,9 @@ class IPRangeBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'vrf', 'tenant', 'role', 'description',
]
)
class IPAddressBulkEditForm(NetBoxModelBulkEditForm):
@ -312,9 +312,9 @@ class IPAddressBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'vrf', 'role', 'tenant', 'dns_name', 'description',
]
)
class FHRPGroupBulkEditForm(NetBoxModelBulkEditForm):
@ -349,7 +349,7 @@ class FHRPGroupBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['auth_type', 'auth_key', 'description']
nullable_fields = ('auth_type', 'auth_key', 'description')
class VLANGroupBulkEditForm(NetBoxModelBulkEditForm):
@ -379,7 +379,7 @@ class VLANGroupBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['site', 'description']
nullable_fields = ('site', 'description')
class VLANBulkEditForm(NetBoxModelBulkEditForm):
@ -429,9 +429,9 @@ class VLANBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'site', 'group', 'tenant', 'role', 'description',
]
)
class ServiceTemplateBulkEditForm(NetBoxModelBulkEditForm):
@ -457,9 +457,7 @@ class ServiceTemplateBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
'description',
]
nullable_fields = ('description',)
class ServiceBulkEditForm(ServiceTemplateBulkEditForm):

View File

@ -5,7 +5,7 @@ from django.db.models import Q
from extras.choices import CustomFieldFilterLogicChoices, CustomFieldTypeChoices
from extras.forms.customfields import CustomFieldsMixin
from extras.models import CustomField, Tag
from utilities.forms import BootstrapMixin, BulkEditBaseForm, CSVModelForm
from utilities.forms import BootstrapMixin, BulkEditMixin, CSVModelForm
from utilities.forms.fields import DynamicModelMultipleChoiceField
__all__ = (
@ -55,7 +55,7 @@ class NetBoxModelCSVForm(CSVModelForm, NetBoxModelForm):
return customfield.to_form_field(for_csv_import=True)
class NetBoxModelBulkEditForm(BootstrapMixin, CustomFieldsMixin, BulkEditBaseForm):
class NetBoxModelBulkEditForm(BootstrapMixin, CustomFieldsMixin, BulkEditMixin, forms.Form):
"""
Base form for modifying multiple NetBox objects (of the same type) in bulk via the UI. Adds support for custom
fields and adding/removing tags.
@ -76,16 +76,21 @@ class NetBoxModelBulkEditForm(BootstrapMixin, CustomFieldsMixin, BulkEditBaseFor
"""
Append form fields for all CustomFields assigned to this object type.
"""
nullable_custom_fields = []
for customfield in self._get_custom_fields(self._get_content_type()):
# Annotate non-required custom fields as nullable
# Record non-required custom fields as nullable
if not customfield.required:
self.nullable_fields.append(customfield.name)
nullable_custom_fields.append(customfield.name)
self.fields[customfield.name] = self._get_form_field(customfield)
# Annotate the field in the list of CustomField form fields
self.custom_fields[customfield.name] = customfield
# Annotate nullable custom fields (if any) on the form instance
if nullable_custom_fields:
self.custom_fields = (*self.custom_fields, *nullable_custom_fields)
class NetBoxModelFilterSetForm(BootstrapMixin, CustomFieldsMixin, forms.Form):
"""

View File

@ -32,7 +32,7 @@ class TenantGroupBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['parent', 'description']
nullable_fields = ('parent', 'description')
class TenantBulkEditForm(NetBoxModelBulkEditForm):
@ -46,9 +46,7 @@ class TenantBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
'group',
]
nullable_fields = ('group',)
#
@ -70,7 +68,7 @@ class ContactGroupBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['parent', 'description']
nullable_fields = ('parent', 'description')
class ContactRoleBulkEditForm(NetBoxModelBulkEditForm):
@ -84,7 +82,7 @@ class ContactRoleBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['description']
nullable_fields = ('description',)
class ContactBulkEditForm(NetBoxModelBulkEditForm):
@ -113,4 +111,4 @@ class ContactBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['group', 'title', 'phone', 'email', 'address', 'comments']
nullable_fields = ('group', 'title', 'phone', 'email', 'address', 'comments')

View File

@ -10,7 +10,7 @@ from .widgets import APISelect, APISelectMultiple, ClearableFileInput, StaticSel
__all__ = (
'BootstrapMixin',
'BulkEditForm',
'BulkEditBaseForm',
'BulkEditMixin',
'BulkRenameForm',
'ConfirmationForm',
'CSVModelForm',
@ -21,6 +21,10 @@ __all__ = (
)
#
# Mixins
#
class BootstrapMixin:
"""
Add the base Bootstrap CSS classes to form elements.
@ -61,6 +65,24 @@ class BootstrapMixin:
field.widget.attrs['class'] = ' '.join((css, 'form-select')).strip()
class BulkEditMixin:
"""
Base form for editing multiple objects in bulk
"""
def __init__(self, model, *args, **kwargs):
super().__init__(*args, **kwargs)
self.model = model
self.nullable_fields = ()
# Copy any nullable fields defined in Meta
if hasattr(self, 'Meta') and hasattr(self.Meta, 'nullable_fields'):
self.nullable_fields = self.Meta.nullable_fields
#
# Form classes
#
class ReturnURLForm(forms.Form):
"""
Provides a hidden return URL field to control where the user is directed after the form is submitted.
@ -75,21 +97,7 @@ class ConfirmationForm(BootstrapMixin, ReturnURLForm):
confirm = forms.BooleanField(required=True, widget=forms.HiddenInput(), initial=True)
class BulkEditBaseForm(forms.Form):
"""
Base form for editing multiple objects in bulk
"""
def __init__(self, model, *args, **kwargs):
super().__init__(*args, **kwargs)
self.model = model
self.nullable_fields = []
# Copy any nullable fields defined in Meta
if hasattr(self.Meta, 'nullable_fields'):
self.nullable_fields = self.Meta.nullable_fields
class BulkEditForm(BootstrapMixin, BulkEditBaseForm):
class BulkEditForm(BootstrapMixin, BulkEditMixin, forms.Form):
pass

View File

@ -34,7 +34,7 @@ class ClusterTypeBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['description']
nullable_fields = ('description',)
class ClusterGroupBulkEditForm(NetBoxModelBulkEditForm):
@ -48,7 +48,7 @@ class ClusterGroupBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['description']
nullable_fields = ('description',)
class ClusterBulkEditForm(NetBoxModelBulkEditForm):
@ -90,9 +90,9 @@ class ClusterBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'group', 'site', 'comments', 'tenant',
]
)
class VirtualMachineBulkEditForm(NetBoxModelBulkEditForm):
@ -145,9 +145,9 @@ class VirtualMachineBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'role', 'tenant', 'platform', 'vcpus', 'memory', 'disk', 'comments',
]
)
class VMInterfaceBulkEditForm(NetBoxModelBulkEditForm):
@ -198,9 +198,9 @@ class VMInterfaceBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = [
nullable_fields = (
'parent', 'bridge', 'mtu', 'description',
]
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

View File

@ -30,7 +30,7 @@ class WirelessLANGroupBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['parent', 'description']
nullable_fields = ('parent', 'description')
class WirelessLANBulkEditForm(NetBoxModelBulkEditForm):
@ -69,7 +69,7 @@ class WirelessLANBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['ssid', 'group', 'vlan', 'description', 'auth_type', 'auth_cipher', 'auth_psk']
nullable_fields = ('ssid', 'group', 'vlan', 'description', 'auth_type', 'auth_cipher', 'auth_psk')
class WirelessLinkBulkEditForm(NetBoxModelBulkEditForm):
@ -103,4 +103,4 @@ class WirelessLinkBulkEditForm(NetBoxModelBulkEditForm):
)
class Meta:
nullable_fields = ['ssid', 'description', 'auth_type', 'auth_cipher', 'auth_psk']
nullable_fields = ('ssid', 'description', 'auth_type', 'auth_cipher', 'auth_psk')