diff --git a/netbox/templates/dcim/inc/ipaddress.html b/netbox/templates/dcim/inc/ipaddress.html index 7bdc8bc1e37..72920986e3f 100644 --- a/netbox/templates/dcim/inc/ipaddress.html +++ b/netbox/templates/dcim/inc/ipaddress.html @@ -13,7 +13,7 @@ {% if perms.ipam.delete_ipaddress %} - + {% endif %} diff --git a/netbox/utilities/forms.py b/netbox/utilities/forms.py index 9351c60448e..f957f8e887f 100644 --- a/netbox/utilities/forms.py +++ b/netbox/utilities/forms.py @@ -386,7 +386,12 @@ def __init__(self, *args, **kwargs): class ConfirmationForm(BootstrapMixin, forms.Form): + """ + A generic confirmation form. The form is not valid unless the confirm field is checked. An optional return_url can + be specified to direct the user to a specific URL after the action has been taken. + """ confirm = forms.BooleanField(required=True) + return_url = forms.CharField(required=False, widget=forms.HiddenInput()) class BulkEditForm(forms.Form): diff --git a/netbox/utilities/views.py b/netbox/utilities/views.py index 125bb658484..bae8728bfc4 100644 --- a/netbox/utilities/views.py +++ b/netbox/utilities/views.py @@ -239,13 +239,16 @@ def get_cancel_url(self, obj): def get(self, request, **kwargs): obj = self.get_object(kwargs) - form = ConfirmationForm() + initial_data = { + 'return_url': request.GET.get('return_url'), + } + form = ConfirmationForm(initial=initial_data) return render(request, self.template_name, { 'obj': obj, 'form': form, 'obj_type': self.model._meta.verbose_name, - 'cancel_url': self.get_cancel_url(obj), + 'cancel_url': request.GET.get('return_url') or self.get_cancel_url(obj), }) def post(self, request, **kwargs): @@ -261,7 +264,10 @@ def post(self, request, **kwargs): msg = u'Deleted {} {}'.format(self.model._meta.verbose_name, obj) messages.success(request, msg) UserAction.objects.log_delete(request.user, obj, msg) - if self.redirect_url: + return_url = form.cleaned_data['return_url'] + if return_url and is_safe_url(url=return_url, host=request.get_host()): + return redirect(return_url) + elif self.redirect_url: return redirect(self.redirect_url) elif hasattr(obj, 'get_parent_url'): return redirect(obj.get_parent_url()) @@ -272,7 +278,7 @@ def post(self, request, **kwargs): 'obj': obj, 'form': form, 'obj_type': self.model._meta.verbose_name, - 'cancel_url': self.get_cancel_url(obj), + 'cancel_url': request.GET.get('return_url') or self.get_cancel_url(obj), })