mirror of
https://github.com/netbox-community/netbox.git
synced 2026-04-25 02:08:58 +02:00
#20162 allow background job when adding components to devices in bulk
This commit is contained in:
@@ -6,6 +6,7 @@ from extras.models import Tag
|
|||||||
from netbox.forms.mixins import CustomFieldsMixin
|
from netbox.forms.mixins import CustomFieldsMixin
|
||||||
from utilities.forms import form_from_model
|
from utilities.forms import form_from_model
|
||||||
from utilities.forms.fields import DynamicModelMultipleChoiceField, ExpandableNameField
|
from utilities.forms.fields import DynamicModelMultipleChoiceField, ExpandableNameField
|
||||||
|
from utilities.forms.mixins import BackgroundJobMixin
|
||||||
|
|
||||||
from .object_create import ComponentCreateForm
|
from .object_create import ComponentCreateForm
|
||||||
|
|
||||||
@@ -27,7 +28,7 @@ __all__ = (
|
|||||||
# Device components
|
# Device components
|
||||||
#
|
#
|
||||||
|
|
||||||
class DeviceBulkAddComponentForm(CustomFieldsMixin, ComponentCreateForm):
|
class DeviceBulkAddComponentForm(BackgroundJobMixin, CustomFieldsMixin, ComponentCreateForm):
|
||||||
pk = forms.ModelMultipleChoiceField(
|
pk = forms.ModelMultipleChoiceField(
|
||||||
queryset=Device.objects.all(),
|
queryset=Device.objects.all(),
|
||||||
widget=forms.MultipleHiddenInput()
|
widget=forms.MultipleHiddenInput()
|
||||||
|
|||||||
@@ -1137,8 +1137,18 @@ class BulkComponentCreateView(GetReturnURLMixin, BaseMultiObjectView):
|
|||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
logger.debug("Form validation was successful")
|
logger.debug("Form validation was successful")
|
||||||
|
|
||||||
|
# If indicated, defer this request to a background job & redirect the user
|
||||||
|
if form.cleaned_data.get('background_job'):
|
||||||
|
job_name = _('Bulk add {count} {object_type}').format(
|
||||||
|
count=len(form.cleaned_data['pk']),
|
||||||
|
object_type=model_name,
|
||||||
|
)
|
||||||
|
if process_request_as_job(self.__class__, request, name=job_name):
|
||||||
|
return redirect(self.get_return_url(request))
|
||||||
|
|
||||||
new_components = []
|
new_components = []
|
||||||
data = deepcopy(form.cleaned_data)
|
data = deepcopy(form.cleaned_data)
|
||||||
|
data.pop('background_job', None)
|
||||||
replication_data = {
|
replication_data = {
|
||||||
field: data.pop(field) for field in form.replication_fields
|
field: data.pop(field) for field in form.replication_fields
|
||||||
}
|
}
|
||||||
@@ -1189,6 +1199,12 @@ class BulkComponentCreateView(GetReturnURLMixin, BaseMultiObjectView):
|
|||||||
parent_model_name
|
parent_model_name
|
||||||
)
|
)
|
||||||
logger.info(msg)
|
logger.info(msg)
|
||||||
|
|
||||||
|
# Handle background job
|
||||||
|
if is_background_request(request):
|
||||||
|
request.job.logger.info(msg)
|
||||||
|
return None
|
||||||
|
|
||||||
messages.success(request, msg)
|
messages.success(request, msg)
|
||||||
|
|
||||||
return redirect(self.get_return_url(request))
|
return redirect(self.get_return_url(request))
|
||||||
|
|||||||
@@ -58,10 +58,19 @@ Context:
|
|||||||
<h2 class="card-header">{{ model_name|title }} {% trans "to Add" %}</h2>
|
<h2 class="card-header">{{ model_name|title }} {% trans "to Add" %}</h2>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
{% for field in form.visible_fields %}
|
{% for field in form.visible_fields %}
|
||||||
{% render_field field %}
|
{% if form.meta_fields and field.name in form.meta_fields %}
|
||||||
|
{% else %}
|
||||||
|
{% render_field field %}
|
||||||
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{# Meta fields #}
|
||||||
|
{% if form.background_job %}
|
||||||
|
<div class="bg-primary-subtle border border-primary rounded-1 pt-3 px-3 mb-3">
|
||||||
|
{% render_field form.background_job %}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
<div class="form-group text-end">
|
<div class="form-group text-end">
|
||||||
<div class="col col-md-12">
|
<div class="col col-md-12">
|
||||||
<a href="{{ return_url }}" class="btn btn-outline-secondary">{% trans "Cancel" %}</a>
|
<a href="{{ return_url }}" class="btn btn-outline-secondary">{% trans "Cancel" %}</a>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from django.utils.translation import gettext_lazy as _
|
|||||||
|
|
||||||
from utilities.forms import form_from_model
|
from utilities.forms import form_from_model
|
||||||
from utilities.forms.fields import ExpandableNameField
|
from utilities.forms.fields import ExpandableNameField
|
||||||
|
from utilities.forms.mixins import BackgroundJobMixin
|
||||||
from virtualization.models import VirtualDisk, VirtualMachine, VMInterface
|
from virtualization.models import VirtualDisk, VirtualMachine, VMInterface
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@@ -11,7 +12,7 @@ __all__ = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class VirtualMachineBulkAddComponentForm(forms.Form):
|
class VirtualMachineBulkAddComponentForm(BackgroundJobMixin, forms.Form):
|
||||||
pk = forms.ModelMultipleChoiceField(
|
pk = forms.ModelMultipleChoiceField(
|
||||||
queryset=VirtualMachine.objects.all(),
|
queryset=VirtualMachine.objects.all(),
|
||||||
widget=forms.MultipleHiddenInput()
|
widget=forms.MultipleHiddenInput()
|
||||||
|
|||||||
Reference in New Issue
Block a user