diff options
| author | Julien Phalip <jphalip@gmail.com> | 2012-12-22 20:00:08 +0100 |
|---|---|---|
| committer | Ramiro Morales <cramm0@gmail.com> | 2012-12-24 18:12:13 -0300 |
| commit | 3ad34c231bb6e13c29dd268f9557a9cb241280fa (patch) | |
| tree | 80a972d4113398c62f048e483b1b9e9bf03d76b4 /tests/regressiontests | |
| parent | 5489fc47df76c3f25a959510b885fa9a28b495b4 (diff) | |
[1.5.x] Fixed #19505 -- A more flexible implementation for customizable admin redirect urls.
Work by Julien Phalip.
Refs #8001, #18310, #19505. See also 0b908b92a2ca4fb74a103e96bb75c53c05d0a428.
35d1cd0b28d1d9cd7bffbfbc6cc2e02b58404415 from master.
Diffstat (limited to 'tests/regressiontests')
| -rw-r--r-- | tests/regressiontests/admin_custom_urls/models.py | 51 | ||||
| -rw-r--r-- | tests/regressiontests/admin_custom_urls/tests.py | 79 |
2 files changed, 72 insertions, 58 deletions
diff --git a/tests/regressiontests/admin_custom_urls/models.py b/tests/regressiontests/admin_custom_urls/models.py index b9b3285463..cc8e730c26 100644 --- a/tests/regressiontests/admin_custom_urls/models.py +++ b/tests/regressiontests/admin_custom_urls/models.py @@ -1,7 +1,9 @@ from functools import update_wrapper from django.contrib import admin +from django.core.urlresolvers import reverse from django.db import models +from django.http import HttpResponseRedirect from django.utils.encoding import python_2_unicode_compatible @@ -49,41 +51,38 @@ class ActionAdmin(admin.ModelAdmin): ) + self.remove_url(view_name) -admin.site.register(Action, ActionAdmin) +class Person(models.Model): + name = models.CharField(max_length=20) +class PersonAdmin(admin.ModelAdmin): -class Person(models.Model): - nick = models.CharField(max_length=20) + def response_post_save(self, request, obj): + return HttpResponseRedirect( + reverse('admin:admin_custom_urls_person_history', args=[obj.pk])) -class PersonAdmin(admin.ModelAdmin): - """A custom ModelAdmin that customizes the deprecated post_url_continue - argument to response_add()""" - def response_add(self, request, obj, post_url_continue='../%s/continue/', - continue_url=None, add_url=None, hasperm_url=None, - noperm_url=None): - return super(PersonAdmin, self).response_add(request, obj, - post_url_continue, - continue_url, add_url, - hasperm_url, noperm_url) +class Car(models.Model): + name = models.CharField(max_length=20) +class CarAdmin(admin.ModelAdmin): -admin.site.register(Person, PersonAdmin) + def response_add(self, request, obj, post_url_continue=None): + return super(CarAdmin, self).response_add( + request, obj, post_url_continue=reverse('admin:admin_custom_urls_car_history', args=[obj.pk])) -class City(models.Model): +class CarDeprecated(models.Model): + """ This class must be removed in Django 1.6 """ name = models.CharField(max_length=20) - -class CityAdmin(admin.ModelAdmin): - """A custom ModelAdmin that redirects to the changelist when the user - presses the 'Save and add another' button when adding a model instance.""" - def response_add(self, request, obj, - add_another_url='admin:admin_custom_urls_city_changelist', - **kwargs): - return super(CityAdmin, self).response_add(request, obj, - add_another_url=add_another_url, - **kwargs) +class CarDeprecatedAdmin(admin.ModelAdmin): + """ This class must be removed in Django 1.6 """ + def response_add(self, request, obj, post_url_continue=None): + return super(CarDeprecatedAdmin, self).response_add( + request, obj, post_url_continue='../%s/history/') -admin.site.register(City, CityAdmin) +admin.site.register(Action, ActionAdmin) +admin.site.register(Person, PersonAdmin) +admin.site.register(Car, CarAdmin) +admin.site.register(CarDeprecated, CarDeprecatedAdmin)
\ No newline at end of file diff --git a/tests/regressiontests/admin_custom_urls/tests.py b/tests/regressiontests/admin_custom_urls/tests.py index 87c72e2e71..d691a97557 100644 --- a/tests/regressiontests/admin_custom_urls/tests.py +++ b/tests/regressiontests/admin_custom_urls/tests.py @@ -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))
\ No newline at end of file |
