Move tagging ops into post_save_operations and use a TabbedGroup in the form

This commit is contained in:
Brian Tiemann 2024-11-06 13:02:35 -05:00
parent 00aae7688d
commit 56664ab0fe
3 changed files with 18 additions and 12 deletions

View File

@ -13,7 +13,7 @@ from tenancy.models import Tenant
from users.models import User from users.models import User
from utilities.forms import BulkEditForm, add_blank_choice, form_from_model from utilities.forms import BulkEditForm, add_blank_choice, form_from_model
from utilities.forms.fields import ColorField, CommentField, DynamicModelChoiceField, DynamicModelMultipleChoiceField from utilities.forms.fields import ColorField, CommentField, DynamicModelChoiceField, DynamicModelMultipleChoiceField
from utilities.forms.rendering import FieldSet, InlineFields from utilities.forms.rendering import FieldSet, InlineFields, TabbedGroups
from utilities.forms.widgets import BulkEditNullBooleanSelect, NumberWithOptions from utilities.forms.widgets import BulkEditNullBooleanSelect, NumberWithOptions
from wireless.models import WirelessLAN, WirelessLANGroup from wireless.models import WirelessLAN, WirelessLANGroup
from wireless.choices import WirelessRoleChoices from wireless.choices import WirelessRoleChoices
@ -1536,7 +1536,13 @@ class InterfaceBulkEditForm(
FieldSet('vdcs', 'mtu', 'tx_power', 'enabled', 'mgmt_only', 'mark_connected', name=_('Operation')), FieldSet('vdcs', 'mtu', 'tx_power', 'enabled', 'mgmt_only', 'mark_connected', name=_('Operation')),
FieldSet('poe_mode', 'poe_type', name=_('PoE')), FieldSet('poe_mode', 'poe_type', name=_('PoE')),
FieldSet('parent', 'bridge', 'lag', name=_('Related Interfaces')), FieldSet('parent', 'bridge', 'lag', name=_('Related Interfaces')),
FieldSet('mode', 'vlan_group', 'untagged_vlan', 'tagged_vlans', 'add_tagged_vlans', 'remove_tagged_vlans', name=_('802.1Q Switching')), FieldSet('mode', 'vlan_group', 'untagged_vlan', name=_('802.1Q Switching')),
FieldSet(
TabbedGroups(
FieldSet('tagged_vlans', name=_('Assignment')),
FieldSet('add_tagged_vlans', 'remove_tagged_vlans', name=_('Add/Remove')),
),
),
FieldSet( FieldSet(
'rf_role', 'rf_channel', 'rf_channel_frequency', 'rf_channel_width', 'wireless_lan_group', 'wireless_lans', 'rf_role', 'rf_channel', 'rf_channel_frequency', 'rf_channel_width', 'wireless_lan_group', 'wireless_lans',
name=_('Wireless') name=_('Wireless')

View File

@ -2616,7 +2616,9 @@ class InterfaceBulkEditView(generic.BulkEditView):
table = tables.InterfaceTable table = tables.InterfaceTable
form = forms.InterfaceBulkEditForm form = forms.InterfaceBulkEditForm
def extra_object_field_operations(self, form, obj): def post_save_operations(self, form, obj):
super().post_save_operations(form, obj)
# Add/remove tagged VLANs # Add/remove tagged VLANs
if obj.mode == InterfaceModeChoices.MODE_TAGGED: if obj.mode == InterfaceModeChoices.MODE_TAGGED:
if form.cleaned_data.get('add_tagged_vlans', None): if form.cleaned_data.get('add_tagged_vlans', None):

View File

@ -541,12 +541,16 @@ class BulkEditView(GetReturnURLMixin, BaseMultiObjectView):
def get_required_permission(self): def get_required_permission(self):
return get_permission_for_model(self.queryset.model, 'change') return get_permission_for_model(self.queryset.model, 'change')
def extra_object_field_operations(self, form, obj): def post_save_operations(self, form, obj):
""" """
This method is called for each object in _update_objects. Override to perform additional object-level This method is called for each object in _update_objects. Override to perform additional object-level
operations that are specific to a particular ModelForm. operations that are specific to a particular ModelForm.
""" """
pass # Add/remove tags
if form.cleaned_data.get('add_tags', None):
obj.tags.add(*form.cleaned_data['add_tags'])
if form.cleaned_data.get('remove_tags', None):
obj.tags.remove(*form.cleaned_data['remove_tags'])
def _update_objects(self, form, request): def _update_objects(self, form, request):
custom_fields = getattr(form, 'custom_fields', {}) custom_fields = getattr(form, 'custom_fields', {})
@ -619,13 +623,7 @@ class BulkEditView(GetReturnURLMixin, BaseMultiObjectView):
elif form.cleaned_data[name]: elif form.cleaned_data[name]:
getattr(obj, name).set(form.cleaned_data[name]) getattr(obj, name).set(form.cleaned_data[name])
# Add/remove tags self.post_save_operations(form, obj)
if form.cleaned_data.get('add_tags', None):
obj.tags.add(*form.cleaned_data['add_tags'])
if form.cleaned_data.get('remove_tags', None):
obj.tags.remove(*form.cleaned_data['remove_tags'])
self.extra_object_field_operations(form, obj)
# Rebuild the tree for MPTT models # Rebuild the tree for MPTT models
if issubclass(self.queryset.model, MPTTModel): if issubclass(self.queryset.model, MPTTModel):