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(),
|
queryset=Tag.objects.all(),
|
||||||
required=False
|
required=False
|
||||||
)
|
)
|
||||||
|
replication_fields = ('name', 'label')
|
||||||
|
|
||||||
|
|
||||||
class ConsolePortBulkCreateForm(
|
class ConsolePortBulkCreateForm(
|
||||||
@ -102,7 +103,7 @@ class RearPortBulkCreateForm(
|
|||||||
class ModuleBayBulkCreateForm(DeviceBulkAddComponentForm):
|
class ModuleBayBulkCreateForm(DeviceBulkAddComponentForm):
|
||||||
model = ModuleBay
|
model = ModuleBay
|
||||||
field_order = ('name', 'label', 'position_pattern', 'description', 'tags')
|
field_order = ('name', 'label', 'position_pattern', 'description', 'tags')
|
||||||
|
replication_fields = ('name', 'label', 'position')
|
||||||
position_pattern = ExpandableNameField(
|
position_pattern = ExpandableNameField(
|
||||||
label='Position',
|
label='Position',
|
||||||
required=False,
|
required=False,
|
||||||
|
@ -2670,7 +2670,6 @@ class DeviceBulkAddModuleBayView(generic.BulkComponentCreateView):
|
|||||||
filterset = filtersets.DeviceFilterSet
|
filterset = filtersets.DeviceFilterSet
|
||||||
table = tables.DeviceTable
|
table = tables.DeviceTable
|
||||||
default_return_url = 'dcim:device_list'
|
default_return_url = 'dcim:device_list'
|
||||||
patterned_fields = ('name', 'label', 'position')
|
|
||||||
|
|
||||||
|
|
||||||
class DeviceBulkAddDeviceBayView(generic.BulkComponentCreateView):
|
class DeviceBulkAddDeviceBayView(generic.BulkComponentCreateView):
|
||||||
|
@ -774,7 +774,6 @@ class BulkComponentCreateView(GetReturnURLMixin, BaseMultiObjectView):
|
|||||||
model_form = None
|
model_form = None
|
||||||
filterset = None
|
filterset = None
|
||||||
table = None
|
table = None
|
||||||
patterned_fields = ('name', 'label')
|
|
||||||
|
|
||||||
def get_required_permission(self):
|
def get_required_permission(self):
|
||||||
return f'dcim.add_{self.queryset.model._meta.model_name}'
|
return f'dcim.add_{self.queryset.model._meta.model_name}'
|
||||||
@ -804,23 +803,25 @@ class BulkComponentCreateView(GetReturnURLMixin, BaseMultiObjectView):
|
|||||||
|
|
||||||
new_components = []
|
new_components = []
|
||||||
data = deepcopy(form.cleaned_data)
|
data = deepcopy(form.cleaned_data)
|
||||||
|
replication_data = {
|
||||||
|
field: data.pop(field) for field in form.replication_fields
|
||||||
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
|
|
||||||
for obj in data['pk']:
|
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):
|
for i in range(pattern_count):
|
||||||
component_data = {
|
component_data = {
|
||||||
self.parent_field: obj.pk
|
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)
|
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)
|
component_form = self.model_form(component_data)
|
||||||
if component_form.is_valid():
|
if component_form.is_valid():
|
||||||
instance = component_form.save()
|
instance = component_form.save()
|
||||||
@ -829,7 +830,7 @@ class BulkComponentCreateView(GetReturnURLMixin, BaseMultiObjectView):
|
|||||||
else:
|
else:
|
||||||
for field, errors in component_form.errors.as_data().items():
|
for field, errors in component_form.errors.as_data().items():
|
||||||
for e in errors:
|
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
|
# Enforce object-level permissions
|
||||||
if self.queryset.filter(pk__in=[obj.pk for obj in new_components]).count() != len(new_components):
|
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(),
|
queryset=VirtualMachine.objects.all(),
|
||||||
widget=forms.MultipleHiddenInput()
|
widget=forms.MultipleHiddenInput()
|
||||||
)
|
)
|
||||||
name_pattern = ExpandableNameField(
|
name = ExpandableNameField(
|
||||||
label='Name'
|
label='Name'
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -27,4 +27,4 @@ class VMInterfaceBulkCreateForm(
|
|||||||
form_from_model(VMInterface, ['enabled', 'mtu', 'description', 'tags']),
|
form_from_model(VMInterface, ['enabled', 'mtu', 'description', 'tags']),
|
||||||
VirtualMachineBulkAddComponentForm
|
VirtualMachineBulkAddComponentForm
|
||||||
):
|
):
|
||||||
pass
|
replication_fields = ('name',)
|
||||||
|
Loading…
Reference in New Issue
Block a user