Skip to content

Commit

Permalink
[1.5.x] Added further flexibility to ModelAdmin for controlling post-…
Browse files Browse the repository at this point in the history
…save redirections.

Refs #19505.
Backport of cee40c7
  • Loading branch information
jphalip committed Dec 31, 2012
1 parent e871e02 commit fa71536
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
26 changes: 20 additions & 6 deletions django/contrib/admin/options.py
Expand Up @@ -838,7 +838,7 @@ def response_add(self, request, obj, post_url_continue=None):
else:
msg = _('The %(name)s "%(obj)s" was added successfully.') % msg_dict
self.message_user(request, msg)
return self.response_post_save(request, obj)
return self.response_post_save_add(request, obj)

def response_change(self, request, obj):
"""
Expand Down Expand Up @@ -872,13 +872,27 @@ def response_change(self, request, obj):
else:
msg = _('The %(name)s "%(obj)s" was changed successfully.') % msg_dict
self.message_user(request, msg)
return self.response_post_save(request, obj)
return self.response_post_save_change(request, obj)

def response_post_save(self, request, obj):
def response_post_save_add(self, request, obj):
"""
Figure out where to redirect after the 'Save' button has been pressed.
If the user has change permission, redirect to the change-list page for
this object. Otherwise, redirect to the admin index.
Figure out where to redirect after the 'Save' button has been pressed
when adding a new object.
"""
opts = self.model._meta
if self.has_change_permission(request, None):
post_url = reverse('admin:%s_%s_changelist' %
(opts.app_label, opts.module_name),
current_app=self.admin_site.name)
else:
post_url = reverse('admin:index',
current_app=self.admin_site.name)
return HttpResponseRedirect(post_url)

def response_post_save_change(self, request, obj):
"""
Figure out where to redirect after the 'Save' button has been pressed
when editing an existing object.
"""
opts = self.model._meta
if self.has_change_permission(request, None):
Expand Down
8 changes: 6 additions & 2 deletions tests/regressiontests/admin_custom_urls/models.py
Expand Up @@ -56,10 +56,14 @@ class Person(models.Model):

class PersonAdmin(admin.ModelAdmin):

def response_post_save(self, request, obj):
def response_post_save_add(self, request, obj):
return HttpResponseRedirect(
reverse('admin:admin_custom_urls_person_history', args=[obj.pk]))

def response_post_save_change(self, request, obj):
return HttpResponseRedirect(
reverse('admin:admin_custom_urls_person_delete', args=[obj.pk]))


class Car(models.Model):
name = models.CharField(max_length=20)
Expand All @@ -85,4 +89,4 @@ def response_add(self, request, obj, post_url_continue=None):
admin.site.register(Action, ActionAdmin)
admin.site.register(Person, PersonAdmin)
admin.site.register(Car, CarAdmin)
admin.site.register(CarDeprecated, CarDeprecatedAdmin)
admin.site.register(CarDeprecated, CarDeprecatedAdmin)
25 changes: 21 additions & 4 deletions tests/regressiontests/admin_custom_urls/tests.py
Expand Up @@ -94,10 +94,11 @@ def setUp(self):
def tearDown(self):
self.client.logout()

def test_post_save_redirect(self):
def test_post_save_add_redirect(self):
"""
Ensures that ModelAdmin.response_post_save() controls the redirection
after the 'Save' button has been pressed.
Ensures that ModelAdmin.response_post_save_add() controls the
redirection after the 'Save' button has been pressed when adding a
new object.
Refs 8001, 18310, 19505.
"""
post_data = { 'name': 'John Doe', }
Expand All @@ -109,6 +110,22 @@ def test_post_save_redirect(self):
self.assertRedirects(
response, reverse('admin:admin_custom_urls_person_history', args=[persons[0].pk]))

def test_post_save_change_redirect(self):
"""
Ensures that ModelAdmin.response_post_save_change() controls the
redirection after the 'Save' button has been pressed when editing an
existing object.
Refs 8001, 18310, 19505.
"""
Person.objects.create(name='John Doe')
self.assertEqual(Person.objects.count(), 1)
person = Person.objects.all()[0]
post_data = { 'name': 'Jack Doe', }
response = self.client.post(
reverse('admin:admin_custom_urls_person_change', args=[person.pk]), post_data)
self.assertRedirects(
response, reverse('admin:admin_custom_urls_person_delete', args=[person.pk]))

def test_post_url_continue(self):
"""
Ensures that the ModelAdmin.response_add()'s parameter `post_url_continue`
Expand Down Expand Up @@ -139,4 +156,4 @@ def test_post_url_continue_string_formats(self):
self.assertRedirects(
response, reverse('admin:admin_custom_urls_cardeprecated_history', args=[cars[0].pk]))
self.assertEqual(len(w), 1)
self.assertTrue(isinstance(w[0].message, DeprecationWarning))
self.assertTrue(isinstance(w[0].message, DeprecationWarning))

0 comments on commit fa71536

Please sign in to comment.