Skip to content

Commit

Permalink
Closes #760: Redirect user back to device view after deleting an assi…
Browse files Browse the repository at this point in the history
…gned IP address
  • Loading branch information
jeremystretch committed Jan 18, 2017
1 parent fc7f88d commit 9ff59ab
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion netbox/templates/dcim/inc/ipaddress.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</td>
<td class="text-right">
{% if perms.ipam.delete_ipaddress %}
<a href="{% url 'ipam:ipaddress_delete' pk=ip.pk %}" class="btn btn-danger btn-xs">
<a href="{% url 'ipam:ipaddress_delete' pk=ip.pk %}?return_url={{ device.get_absolute_url }}" class="btn btn-danger btn-xs">
<i class="glyphicon glyphicon-trash" aria-hidden="true" title="Delete IP address"></i>
</a>
{% endif %}
Expand Down
5 changes: 5 additions & 0 deletions netbox/utilities/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 10 additions & 4 deletions netbox/utilities/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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())
Expand All @@ -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),
})


Expand Down

0 comments on commit 9ff59ab

Please sign in to comment.