mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-24 00:15:17 -06:00
Update component bulk update forms & views to support new replication fields
This commit is contained in:
parent
2cf7b6dc39
commit
f122dce4e6
@ -37,6 +37,7 @@ class DeviceBulkAddComponentForm(BootstrapMixin, CustomFieldsMixin, ComponentCre
|
||||
queryset=Tag.objects.all(),
|
||||
required=False
|
||||
)
|
||||
replication_fields = ('name', 'label')
|
||||
|
||||
|
||||
class ConsolePortBulkCreateForm(
|
||||
@ -102,7 +103,7 @@ class RearPortBulkCreateForm(
|
||||
class ModuleBayBulkCreateForm(DeviceBulkAddComponentForm):
|
||||
model = ModuleBay
|
||||
field_order = ('name', 'label', 'position_pattern', 'description', 'tags')
|
||||
|
||||
replication_fields = ('name', 'label', 'position')
|
||||
position_pattern = ExpandableNameField(
|
||||
label='Position',
|
||||
required=False,
|
||||
|
@ -2670,7 +2670,6 @@ class DeviceBulkAddModuleBayView(generic.BulkComponentCreateView):
|
||||
filterset = filtersets.DeviceFilterSet
|
||||
table = tables.DeviceTable
|
||||
default_return_url = 'dcim:device_list'
|
||||
patterned_fields = ('name', 'label', 'position')
|
||||
|
||||
|
||||
class DeviceBulkAddDeviceBayView(generic.BulkComponentCreateView):
|
||||
|
@ -774,7 +774,6 @@ class BulkComponentCreateView(GetReturnURLMixin, BaseMultiObjectView):
|
||||
model_form = None
|
||||
filterset = None
|
||||
table = None
|
||||
patterned_fields = ('name', 'label')
|
||||
|
||||
def get_required_permission(self):
|
||||
return f'dcim.add_{self.queryset.model._meta.model_name}'
|
||||
@ -804,23 +803,25 @@ class BulkComponentCreateView(GetReturnURLMixin, BaseMultiObjectView):
|
||||
|
||||
new_components = []
|
||||
data = deepcopy(form.cleaned_data)
|
||||
replication_data = {
|
||||
field: data.pop(field) for field in form.replication_fields
|
||||
}
|
||||
|
||||
try:
|
||||
with transaction.atomic():
|
||||
|
||||
for obj in data['pk']:
|
||||
|
||||
pattern_count = len(data[f'{self.patterned_fields[0]}_pattern'])
|
||||
pattern_count = len(replication_data[form.replication_fields[0]])
|
||||
for i in range(pattern_count):
|
||||
component_data = {
|
||||
self.parent_field: obj.pk
|
||||
}
|
||||
|
||||
for field_name in self.patterned_fields:
|
||||
if data.get(f'{field_name}_pattern'):
|
||||
component_data[field_name] = data[f'{field_name}_pattern'][i]
|
||||
|
||||
component_data.update(data)
|
||||
for field, values in replication_data.items():
|
||||
if values:
|
||||
component_data[field] = values[i]
|
||||
|
||||
component_form = self.model_form(component_data)
|
||||
if component_form.is_valid():
|
||||
instance = component_form.save()
|
||||
@ -829,7 +830,7 @@ class BulkComponentCreateView(GetReturnURLMixin, BaseMultiObjectView):
|
||||
else:
|
||||
for field, errors in component_form.errors.as_data().items():
|
||||
for e in errors:
|
||||
form.add_error(field, '{} {}: {}'.format(obj, name, ', '.join(e)))
|
||||
form.add_error(field, '{}: {}'.format(obj, ', '.join(e)))
|
||||
|
||||
# Enforce object-level permissions
|
||||
if self.queryset.filter(pk__in=[obj.pk for obj in new_components]).count() != len(new_components):
|
||||
|
@ -13,7 +13,7 @@ class VirtualMachineBulkAddComponentForm(BootstrapMixin, forms.Form):
|
||||
queryset=VirtualMachine.objects.all(),
|
||||
widget=forms.MultipleHiddenInput()
|
||||
)
|
||||
name_pattern = ExpandableNameField(
|
||||
name = ExpandableNameField(
|
||||
label='Name'
|
||||
)
|
||||
|
||||
@ -27,4 +27,4 @@ class VMInterfaceBulkCreateForm(
|
||||
form_from_model(VMInterface, ['enabled', 'mtu', 'description', 'tags']),
|
||||
VirtualMachineBulkAddComponentForm
|
||||
):
|
||||
pass
|
||||
replication_fields = ('name',)
|
||||
|
Loading…
Reference in New Issue
Block a user