Replaced some more hardcoded admin URLs

This commit is contained in:
Claude Paroz 2015-02-09 17:28:03 +01:00
parent c5d1a5ef5c
commit 50aa1a790c
4 changed files with 54 additions and 36 deletions

View File

@ -8,6 +8,7 @@ from django.contrib.auth.forms import (
) )
from django.contrib.auth.models import Group, User from django.contrib.auth.models import Group, User
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.db import transaction from django.db import transaction
from django.http import Http404, HttpResponseRedirect from django.http import Http404, HttpResponseRedirect
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
@ -133,7 +134,9 @@ class UserAdmin(admin.ModelAdmin):
msg = ugettext('Password changed successfully.') msg = ugettext('Password changed successfully.')
messages.success(request, msg) messages.success(request, msg)
update_session_auth_hash(request, form.user) update_session_auth_hash(request, form.user)
return HttpResponseRedirect('..') return HttpResponseRedirect(
reverse('%s:auth_user_change' % self.admin_site.name, args=(user.pk,))
)
else: else:
form = self.change_password_form(user) form = self.change_password_form(user)

View File

@ -861,32 +861,39 @@ class ChangelistTests(AuthViewsTestCase):
def test_changelist_disallows_password_lookups(self): def test_changelist_disallows_password_lookups(self):
# A lookup that tries to filter on password isn't OK # A lookup that tries to filter on password isn't OK
with patch_logger('django.security.DisallowedModelAdminLookup', 'error') as logger_calls: with patch_logger('django.security.DisallowedModelAdminLookup', 'error') as logger_calls:
response = self.client.get('/admin/auth/user/?password__startswith=sha1$') response = self.client.get(reverse('auth_test_admin:auth_user_changelist') + '?password__startswith=sha1$')
self.assertEqual(response.status_code, 400) self.assertEqual(response.status_code, 400)
self.assertEqual(len(logger_calls), 1) self.assertEqual(len(logger_calls), 1)
def test_user_change_email(self): def test_user_change_email(self):
data = self.get_user_data(self.admin) data = self.get_user_data(self.admin)
data['email'] = 'new_' + data['email'] data['email'] = 'new_' + data['email']
response = self.client.post('/admin/auth/user/%s/' % self.admin.pk, data) response = self.client.post(
self.assertRedirects(response, '/admin/auth/user/') reverse('auth_test_admin:auth_user_change', args=(self.admin.pk,)),
data
)
self.assertRedirects(response, reverse('auth_test_admin:auth_user_changelist'))
row = LogEntry.objects.latest('id') row = LogEntry.objects.latest('id')
self.assertEqual(row.change_message, 'Changed email.') self.assertEqual(row.change_message, 'Changed email.')
def test_user_not_change(self): def test_user_not_change(self):
response = self.client.post('/admin/auth/user/%s/' % self.admin.pk, response = self.client.post(
reverse('auth_test_admin:auth_user_change', args=(self.admin.pk,)),
self.get_user_data(self.admin) self.get_user_data(self.admin)
) )
self.assertRedirects(response, '/admin/auth/user/') self.assertRedirects(response, reverse('auth_test_admin:auth_user_changelist'))
row = LogEntry.objects.latest('id') row = LogEntry.objects.latest('id')
self.assertEqual(row.change_message, 'No fields changed.') self.assertEqual(row.change_message, 'No fields changed.')
def test_user_change_password(self): def test_user_change_password(self):
response = self.client.post('/admin/auth/user/%s/password/' % self.admin.pk, { response = self.client.post(
'password1': 'password1', reverse('auth_test_admin:auth_user_password_change', args=(self.admin.pk,)),
'password2': 'password1', {
}) 'password1': 'password1',
self.assertRedirects(response, '/admin/auth/user/%s/' % self.admin.pk) 'password2': 'password1',
}
)
self.assertRedirects(response, reverse('auth_test_admin:auth_user_change', args=(self.admin.pk,)))
row = LogEntry.objects.latest('id') row = LogEntry.objects.latest('id')
self.assertEqual(row.change_message, 'Changed password.') self.assertEqual(row.change_message, 'Changed password.')
self.logout() self.logout()
@ -894,11 +901,14 @@ class ChangelistTests(AuthViewsTestCase):
def test_user_change_different_user_password(self): def test_user_change_different_user_password(self):
u = User.objects.get(email='staffmember@example.com') u = User.objects.get(email='staffmember@example.com')
response = self.client.post('/admin/auth/user/%s/password/' % u.pk, { response = self.client.post(
'password1': 'password1', reverse('auth_test_admin:auth_user_password_change', args=(u.pk,)),
'password2': 'password1', {
}) 'password1': 'password1',
self.assertRedirects(response, '/admin/auth/user/%s/' % u.pk) 'password2': 'password1',
}
)
self.assertRedirects(response, reverse('auth_test_admin:auth_user_change', args=(u.pk,)))
row = LogEntry.objects.latest('id') row = LogEntry.objects.latest('id')
self.assertEqual(row.user_id, self.admin.pk) self.assertEqual(row.user_id, self.admin.pk)
self.assertEqual(row.object_id, str(u.pk)) self.assertEqual(row.object_id, str(u.pk))

View File

@ -6,6 +6,7 @@ from django.contrib.admin.sites import AdminSite
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.contenttypes.admin import GenericTabularInline from django.contrib.contenttypes.admin import GenericTabularInline
from django.contrib.contenttypes.forms import generic_inlineformset_factory from django.contrib.contenttypes.forms import generic_inlineformset_factory
from django.core.urlresolvers import reverse
from django.forms.formsets import DEFAULT_MAX_NUM from django.forms.formsets import DEFAULT_MAX_NUM
from django.forms.models import ModelForm from django.forms.models import ModelForm
from django.test import RequestFactory, TestCase, override_settings from django.test import RequestFactory, TestCase, override_settings
@ -42,14 +43,16 @@ class GenericAdminViewTest(TestCase):
""" """
A smoke test to ensure GET on the add_view works. A smoke test to ensure GET on the add_view works.
""" """
response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episode/add/') response = self.client.get(reverse('admin:generic_inline_admin_episode_add'))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_basic_edit_GET(self): def test_basic_edit_GET(self):
""" """
A smoke test to ensure GET on the change_view works. A smoke test to ensure GET on the change_view works.
""" """
response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episode/%d/' % self.episode_pk) response = self.client.get(
reverse('admin:generic_inline_admin_episode_change', args=(self.episode_pk,))
)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_basic_add_POST(self): def test_basic_add_POST(self):
@ -63,7 +66,7 @@ class GenericAdminViewTest(TestCase):
"generic_inline_admin-media-content_type-object_id-INITIAL_FORMS": "0", "generic_inline_admin-media-content_type-object_id-INITIAL_FORMS": "0",
"generic_inline_admin-media-content_type-object_id-MAX_NUM_FORMS": "0", "generic_inline_admin-media-content_type-object_id-MAX_NUM_FORMS": "0",
} }
response = self.client.post('/generic_inline_admin/admin/generic_inline_admin/episode/add/', post_data) response = self.client.post(reverse('admin:generic_inline_admin_episode_add'), post_data)
self.assertEqual(response.status_code, 302) # redirect somewhere self.assertEqual(response.status_code, 302) # redirect somewhere
def test_basic_edit_POST(self): def test_basic_edit_POST(self):
@ -83,7 +86,7 @@ class GenericAdminViewTest(TestCase):
"generic_inline_admin-media-content_type-object_id-2-id": "", "generic_inline_admin-media-content_type-object_id-2-id": "",
"generic_inline_admin-media-content_type-object_id-2-url": "", "generic_inline_admin-media-content_type-object_id-2-url": "",
} }
url = '/generic_inline_admin/admin/generic_inline_admin/episode/%d/' % self.episode_pk url = reverse('admin:generic_inline_admin_episode_change', args=(self.episode_pk,))
response = self.client.post(url, post_data) response = self.client.post(url, post_data)
self.assertEqual(response.status_code, 302) # redirect somewhere self.assertEqual(response.status_code, 302) # redirect somewhere
@ -147,7 +150,7 @@ class GenericInlineAdminParametersTest(TestCase):
With one initial form, extra (default) at 3, there should be 4 forms. With one initial form, extra (default) at 3, there should be 4 forms.
""" """
e = self._create_object(Episode) e = self._create_object(Episode)
response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk) response = self.client.get(reverse('admin:generic_inline_admin_episode_change', args=(e.pk,)))
formset = response.context['inline_admin_formsets'][0].formset formset = response.context['inline_admin_formsets'][0].formset
self.assertEqual(formset.total_form_count(), 4) self.assertEqual(formset.total_form_count(), 4)
self.assertEqual(formset.initial_form_count(), 1) self.assertEqual(formset.initial_form_count(), 1)
@ -164,7 +167,7 @@ class GenericInlineAdminParametersTest(TestCase):
modeladmin.inlines = [ExtraInline] modeladmin.inlines = [ExtraInline]
e = self._create_object(Episode) e = self._create_object(Episode)
request = self.factory.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk) request = self.factory.get(reverse('admin:generic_inline_admin_episode_change', args=(e.pk,)))
request.user = User(username='super', is_superuser=True) request.user = User(username='super', is_superuser=True)
response = modeladmin.changeform_view(request, object_id=str(e.pk)) response = modeladmin.changeform_view(request, object_id=str(e.pk))
formset = response.context_data['inline_admin_formsets'][0].formset formset = response.context_data['inline_admin_formsets'][0].formset
@ -184,7 +187,7 @@ class GenericInlineAdminParametersTest(TestCase):
modeladmin.inlines = [MaxNumInline] modeladmin.inlines = [MaxNumInline]
e = self._create_object(Episode) e = self._create_object(Episode)
request = self.factory.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk) request = self.factory.get(reverse('admin:generic_inline_admin_episode_change', args=(e.pk,)))
request.user = User(username='super', is_superuser=True) request.user = User(username='super', is_superuser=True)
response = modeladmin.changeform_view(request, object_id=str(e.pk)) response = modeladmin.changeform_view(request, object_id=str(e.pk))
formset = response.context_data['inline_admin_formsets'][0].formset formset = response.context_data['inline_admin_formsets'][0].formset
@ -204,7 +207,7 @@ class GenericInlineAdminParametersTest(TestCase):
modeladmin.inlines = [MinNumInline] modeladmin.inlines = [MinNumInline]
e = self._create_object(Episode) e = self._create_object(Episode)
request = self.factory.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk) request = self.factory.get(reverse('admin:generic_inline_admin_episode_change', args=(e.pk,)))
request.user = User(username='super', is_superuser=True) request.user = User(username='super', is_superuser=True)
response = modeladmin.changeform_view(request, object_id=str(e.pk)) response = modeladmin.changeform_view(request, object_id=str(e.pk))
formset = response.context_data['inline_admin_formsets'][0].formset formset = response.context_data['inline_admin_formsets'][0].formset
@ -223,7 +226,7 @@ class GenericInlineAdminParametersTest(TestCase):
modeladmin = admin.ModelAdmin(Episode, admin_site) modeladmin = admin.ModelAdmin(Episode, admin_site)
modeladmin.inlines = [GetExtraInline] modeladmin.inlines = [GetExtraInline]
e = self._create_object(Episode) e = self._create_object(Episode)
request = self.factory.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk) request = self.factory.get(reverse('admin:generic_inline_admin_episode_change', args=(e.pk,)))
request.user = User(username='super', is_superuser=True) request.user = User(username='super', is_superuser=True)
response = modeladmin.changeform_view(request, object_id=str(e.pk)) response = modeladmin.changeform_view(request, object_id=str(e.pk))
formset = response.context_data['inline_admin_formsets'][0].formset formset = response.context_data['inline_admin_formsets'][0].formset
@ -242,7 +245,7 @@ class GenericInlineAdminParametersTest(TestCase):
modeladmin = admin.ModelAdmin(Episode, admin_site) modeladmin = admin.ModelAdmin(Episode, admin_site)
modeladmin.inlines = [GetMinNumInline] modeladmin.inlines = [GetMinNumInline]
e = self._create_object(Episode) e = self._create_object(Episode)
request = self.factory.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk) request = self.factory.get(reverse('admin:generic_inline_admin_episode_change', args=(e.pk,)))
request.user = User(username='super', is_superuser=True) request.user = User(username='super', is_superuser=True)
response = modeladmin.changeform_view(request, object_id=str(e.pk)) response = modeladmin.changeform_view(request, object_id=str(e.pk))
formset = response.context_data['inline_admin_formsets'][0].formset formset = response.context_data['inline_admin_formsets'][0].formset
@ -261,7 +264,7 @@ class GenericInlineAdminParametersTest(TestCase):
modeladmin = admin.ModelAdmin(Episode, admin_site) modeladmin = admin.ModelAdmin(Episode, admin_site)
modeladmin.inlines = [GetMaxNumInline] modeladmin.inlines = [GetMaxNumInline]
e = self._create_object(Episode) e = self._create_object(Episode)
request = self.factory.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk) request = self.factory.get(reverse('admin:generic_inline_admin_episode_change', args=(e.pk,)))
request.user = User(username='super', is_superuser=True) request.user = User(username='super', is_superuser=True)
response = modeladmin.changeform_view(request, object_id=str(e.pk)) response = modeladmin.changeform_view(request, object_id=str(e.pk))
formset = response.context_data['inline_admin_formsets'][0].formset formset = response.context_data['inline_admin_formsets'][0].formset
@ -289,9 +292,9 @@ class GenericInlineAdminWithUniqueTogetherTest(TestCase):
"generic_inline_admin-phonenumber-content_type-object_id-0-phone_number": "555-555-5555", "generic_inline_admin-phonenumber-content_type-object_id-0-phone_number": "555-555-5555",
"generic_inline_admin-phonenumber-content_type-object_id-0-category": "%s" % category_id, "generic_inline_admin-phonenumber-content_type-object_id-0-category": "%s" % category_id,
} }
response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/contact/add/') response = self.client.get(reverse('admin:generic_inline_admin_contact_add'))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.post('/generic_inline_admin/admin/generic_inline_admin/contact/add/', post_data) response = self.client.post(reverse('admin:generic_inline_admin_contact_add'), post_data)
self.assertEqual(response.status_code, 302) # redirect somewhere self.assertEqual(response.status_code, 302) # redirect somewhere

View File

@ -4,6 +4,7 @@ from django.apps import apps
from django.contrib import admin from django.contrib import admin
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core import checks, management from django.core import checks, management
from django.core.urlresolvers import reverse
from django.db import DEFAULT_DB_ALIAS, models from django.db import DEFAULT_DB_ALIAS, models
from django.db.models import signals from django.db.models import signals
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
@ -408,17 +409,18 @@ class ProxyModelAdminTests(TestCase):
user = TrackerUser.objects.get(name='Django Pony') user = TrackerUser.objects.get(name='Django Pony')
proxy = ProxyTrackerUser.objects.get(name='Django Pony') proxy = ProxyTrackerUser.objects.get(name='Django Pony')
user_str = ( user_str = 'Tracker user: <a href="%s">%s</a>' % (
'Tracker user: <a href="/admin/proxy_models/trackeruser/%s/">%s</a>' % (user.pk, user)) reverse('admin:proxy_models_trackeruser_change', args=(user.pk,)), user
proxy_str = ( )
'Proxy tracker user: <a href="/admin/proxy_models/proxytrackeruser/%s/">%s</a>' % proxy_str = 'Proxy tracker user: <a href="%s">%s</a>' % (
(proxy.pk, proxy)) reverse('admin:proxy_models_proxytrackeruser_change', args=(proxy.pk,)), proxy
)
self.client.login(username='super', password='secret') self.client.login(username='super', password='secret')
response = self.client.get('/admin/proxy_models/trackeruser/%s/delete/' % (user.pk,)) response = self.client.get(reverse('admin:proxy_models_trackeruser_delete', args=(user.pk,)))
delete_str = response.context['deleted_objects'][0] delete_str = response.context['deleted_objects'][0]
self.assertEqual(delete_str, user_str) self.assertEqual(delete_str, user_str)
response = self.client.get('/admin/proxy_models/proxytrackeruser/%s/delete/' % (proxy.pk,)) response = self.client.get(reverse('admin:proxy_models_proxytrackeruser_delete', args=(proxy.pk,)))
delete_str = response.context['deleted_objects'][0] delete_str = response.context['deleted_objects'][0]
self.assertEqual(delete_str, proxy_str) self.assertEqual(delete_str, proxy_str)
self.client.logout() self.client.logout()