1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fix tag assignment when bulk creating components

This commit is contained in:
Jeremy Stretch
2020-04-22 14:05:27 -04:00
parent 6a61f0911d
commit 7b50f2b0eb
3 changed files with 40 additions and 40 deletions

View File

@ -2299,6 +2299,11 @@ class DeviceBulkAddComponentForm(BootstrapMixin, forms.Form):
label='Name' label='Name'
) )
def clean_tags(self):
# Because we're feeding TagField data (on the bulk edit form) to another TagField (on the model form), we
# must first convert the list of tags to a string.
return ','.join(self.cleaned_data.get('tags'))
# #
# Console ports # Console ports
@ -2355,9 +2360,7 @@ class ConsolePortBulkCreateForm(
form_from_model(ConsolePort, ['type', 'description', 'tags']), form_from_model(ConsolePort, ['type', 'description', 'tags']),
DeviceBulkAddComponentForm DeviceBulkAddComponentForm
): ):
tags = TagField( pass
required=False
)
class ConsolePortBulkEditForm( class ConsolePortBulkEditForm(
@ -2447,9 +2450,7 @@ class ConsoleServerPortBulkCreateForm(
form_from_model(ConsoleServerPort, ['type', 'description', 'tags']), form_from_model(ConsoleServerPort, ['type', 'description', 'tags']),
DeviceBulkAddComponentForm DeviceBulkAddComponentForm
): ):
tags = TagField( pass
required=False
)
class ConsoleServerPortBulkEditForm( class ConsoleServerPortBulkEditForm(
@ -2563,9 +2564,7 @@ class PowerPortBulkCreateForm(
form_from_model(PowerPort, ['type', 'maximum_draw', 'allocated_draw', 'description', 'tags']), form_from_model(PowerPort, ['type', 'maximum_draw', 'allocated_draw', 'description', 'tags']),
DeviceBulkAddComponentForm DeviceBulkAddComponentForm
): ):
tags = TagField( pass
required=False
)
class PowerPortBulkEditForm( class PowerPortBulkEditForm(
@ -2685,9 +2684,7 @@ class PowerOutletBulkCreateForm(
form_from_model(PowerOutlet, ['type', 'feed_leg', 'description', 'tags']), form_from_model(PowerOutlet, ['type', 'feed_leg', 'description', 'tags']),
DeviceBulkAddComponentForm DeviceBulkAddComponentForm
): ):
tags = TagField( pass
required=False
)
class PowerOutletBulkEditForm( class PowerOutletBulkEditForm(
@ -2968,9 +2965,7 @@ class InterfaceBulkCreateForm(
form_from_model(Interface, ['type', 'enabled', 'mtu', 'mgmt_only', 'description', 'tags']), form_from_model(Interface, ['type', 'enabled', 'mtu', 'mgmt_only', 'description', 'tags']),
DeviceBulkAddComponentForm DeviceBulkAddComponentForm
): ):
tags = TagField( pass
required=False
)
class InterfaceBulkEditForm( class InterfaceBulkEditForm(
@ -3241,9 +3236,7 @@ class FrontPortCreateForm(BootstrapMixin, forms.Form):
# form_from_model(FrontPort, ['type', 'description', 'tags']), # form_from_model(FrontPort, ['type', 'description', 'tags']),
# DeviceBulkAddComponentForm # DeviceBulkAddComponentForm
# ): # ):
# tags = TagField( # pass
# required=False
# )
class FrontPortBulkEditForm( class FrontPortBulkEditForm(
@ -3381,9 +3374,7 @@ class RearPortBulkCreateForm(
form_from_model(RearPort, ['type', 'positions', 'description', 'tags']), form_from_model(RearPort, ['type', 'positions', 'description', 'tags']),
DeviceBulkAddComponentForm DeviceBulkAddComponentForm
): ):
tags = TagField( pass
required=False
)
class RearPortBulkEditForm( class RearPortBulkEditForm(

View File

@ -972,25 +972,32 @@ class BulkComponentCreateView(GetReturnURLMixin, View):
new_components = [] new_components = []
data = deepcopy(form.cleaned_data) data = deepcopy(form.cleaned_data)
for obj in data['pk']: try:
with transaction.atomic():
names = data['name_pattern'] for obj in data['pk']:
for name in names:
component_data = { names = data['name_pattern']
self.parent_field: obj.pk, for name in names:
'name': name, component_data = {
} self.parent_field: obj.pk,
component_data.update(data) 'name': name,
component_form = self.model_form(component_data) }
if component_form.is_valid(): component_data.update(data)
new_components.append(component_form.save(commit=False)) component_form = self.model_form(component_data)
else: if component_form.is_valid():
for field, errors in component_form.errors.as_data().items(): instance = component_form.save()
for e in errors: logger.debug(f"Created {instance} on {instance.parent}")
form.add_error(field, '{} {}: {}'.format(obj, name, ', '.join(e))) new_components.append(instance)
else:
for field, errors in component_form.errors.as_data().items():
for e in errors:
form.add_error(field, '{} {}: {}'.format(obj, name, ', '.join(e)))
except IntegrityError:
pass
if not form.errors: if not form.errors:
self.model.objects.bulk_create(new_components)
msg = "Added {} {} to {} {}.".format( msg = "Added {} {} to {} {}.".format(
len(new_components), len(new_components),
model_name, model_name,

View File

@ -828,6 +828,11 @@ class VirtualMachineBulkAddComponentForm(BootstrapMixin, forms.Form):
label='Name' label='Name'
) )
def clean_tags(self):
# Because we're feeding TagField data (on the bulk edit form) to another TagField (on the model form), we
# must first convert the list of tags to a string.
return ','.join(self.cleaned_data.get('tags'))
class InterfaceBulkCreateForm( class InterfaceBulkCreateForm(
form_from_model(Interface, ['enabled', 'mtu', 'description', 'tags']), form_from_model(Interface, ['enabled', 'mtu', 'description', 'tags']),
@ -838,6 +843,3 @@ class InterfaceBulkCreateForm(
initial=VMInterfaceTypeChoices.TYPE_VIRTUAL, initial=VMInterfaceTypeChoices.TYPE_VIRTUAL,
widget=forms.HiddenInput() widget=forms.HiddenInput()
) )
tags = TagField(
required=False
)