1
0
mirror of https://github.com/django/django.git synced 2025-10-26 07:06:08 +00:00

Fixed #19505 -- A more flexible implementation for customizable admin redirect urls.

Work by Julien Phalip.

Refs #8001, #18310, #19505. See also 0b908b92a2.
This commit is contained in:
Julien Phalip
2012-12-22 20:00:08 +01:00
committed by Ramiro Morales
parent 4a71b84266
commit 35d1cd0b28
5 changed files with 144 additions and 174 deletions

View File

@@ -1,5 +1,4 @@
from __future__ import absolute_import, unicode_literals
import warnings
from django.contrib.admin.util import quote
@@ -8,7 +7,7 @@ from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.utils import override_settings
from .models import Action, Person, City
from .models import Action, Person, Car, CarDeprecated
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
@@ -86,8 +85,8 @@ class AdminCustomUrlsTest(TestCase):
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
class CustomUrlsWorkflowTests(TestCase):
fixtures = ['users.json']
class CustomRedirects(TestCase):
fixtures = ['users.json', 'actions.json']
def setUp(self):
self.client.login(username='super', password='secret')
@@ -95,33 +94,49 @@ class CustomUrlsWorkflowTests(TestCase):
def tearDown(self):
self.client.logout()
def test_old_argument_deprecation(self):
"""Test reporting of post_url_continue deprecation."""
post_data = {
'nick': 'johndoe',
}
cnt = Person.objects.count()
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
response = self.client.post(reverse('admin:admin_custom_urls_person_add'), post_data)
self.assertEqual(response.status_code, 302)
self.assertEqual(Person.objects.count(), cnt + 1)
# We should get a DeprecationWarning
self.assertEqual(len(w), 1)
self.assertTrue(isinstance(w[0].message, DeprecationWarning))
def test_post_save_redirect(self):
"""
Ensures that ModelAdmin.response_post_save() controls the redirection
after the 'Save' button has been pressed.
Refs 8001, 18310, 19505.
"""
post_data = { 'name': 'John Doe', }
self.assertEqual(Person.objects.count(), 0)
response = self.client.post(
reverse('admin:admin_custom_urls_person_add'), post_data)
persons = Person.objects.all()
self.assertEqual(len(persons), 1)
self.assertRedirects(
response, reverse('admin:admin_custom_urls_person_history', args=[persons[0].pk]))
def test_custom_add_another_redirect(self):
"""Test customizability of post-object-creation redirect URL."""
post_data = {
'name': 'Rome',
'_addanother': '1',
}
cnt = City.objects.count()
def test_post_url_continue(self):
"""
Ensures that the ModelAdmin.response_add()'s parameter `post_url_continue`
controls the redirection after an object has been created.
"""
post_data = { 'name': 'SuperFast', '_continue': '1' }
self.assertEqual(Car.objects.count(), 0)
response = self.client.post(
reverse('admin:admin_custom_urls_car_add'), post_data)
cars = Car.objects.all()
self.assertEqual(len(cars), 1)
self.assertRedirects(
response, reverse('admin:admin_custom_urls_car_history', args=[cars[0].pk]))
def test_post_url_continue_string_formats(self):
"""
Ensures that string formats are accepted for post_url_continue. This
is a deprecated functionality that will be removed in Django 1.6 along
with this test.
"""
with warnings.catch_warnings(record=True) as w:
# POST to the view whose post-object-creation redir URL argument we
# are customizing (object creation)
response = self.client.post(reverse('admin:admin_custom_urls_city_add'), post_data)
self.assertEqual(City.objects.count(), cnt + 1)
# Check that it redirected to the URL we set
self.assertRedirects(response, reverse('admin:admin_custom_urls_city_changelist'))
self.assertEqual(len(w), 0) # We should get no DeprecationWarning
post_data = { 'name': 'SuperFast', '_continue': '1' }
self.assertEqual(Car.objects.count(), 0)
response = self.client.post(
reverse('admin:admin_custom_urls_cardeprecated_add'), post_data)
cars = CarDeprecated.objects.all()
self.assertEqual(len(cars), 1)
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))