1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #8261 -- ModelAdmin hook for customising the "show on site" button

``ModelAdmin.view_on_site`` defines wether to show a link to the object on the
admin detail page. If ``True``, cleverness (i.e. ``Model.get_absolute_url``) is
used to get the url. If it's a callable, the callable is called with the object
as the only parameter. If ``False``, not link is displayed.

With the aim of maitaining backwards compatibility, ``True`` is the default.
This commit is contained in:
Unai Zalakain
2013-10-24 17:28:09 +02:00
committed by Simon Charette
parent 497930b7f6
commit fd219fa24c
12 changed files with 266 additions and 10 deletions

View File

@@ -9,6 +9,7 @@ import unittest
from django.conf import settings, global_settings
from django.core import mail
from django.core.files import temp as tempfile
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse, NoReverseMatch
# Register auth models with the admin.
from django.contrib.auth import get_permission_codename
@@ -48,8 +49,8 @@ from .models import (Article, BarAccount, CustomArticle, EmptyModel, FooAccount,
AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedCallable,
Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject,
Simple, UndeletableObject, UnchangeableObject, Choice, ShortMessage,
Telegram, Pizza, Topping, FilteredManager)
from .admin import site, site2
Telegram, Pizza, Topping, FilteredManager, City, Restaurant, Worker)
from .admin import site, site2, CityAdmin
ERROR_MESSAGE = "Please enter the correct username and password \
@@ -4597,3 +4598,87 @@ class TestLabelVisibility(TestCase):
def assert_fieldline_hidden(self, response):
self.assertContains(response, '<div class="form-row hidden')
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
class AdminViewOnSiteTest(TestCase):
urls = "admin_views.urls"
fixtures = ['admin-views-users.xml', 'admin-views-restaurants.xml']
def setUp(self):
self.client.login(username='super', password='secret')
def tearDown(self):
self.client.logout()
def test_validate(self):
"Ensure that the view_on_site value is either a boolean or a callable"
CityAdmin.view_on_site = True
CityAdmin.validate(City)
CityAdmin.view_on_site = False
CityAdmin.validate(City)
CityAdmin.view_on_site = lambda obj: obj.get_absolute_url()
CityAdmin.validate(City)
CityAdmin.view_on_site = []
with self.assertRaisesMessage(ImproperlyConfigured, 'CityAdmin.view_on_site is not a callable or a boolean value.'):
CityAdmin.validate(City)
def test_false(self):
"Ensure that the 'View on site' button is not displayed if view_on_site is False"
response = self.client.get('/test_admin/admin/admin_views/restaurant/1/')
content_type_pk = ContentType.objects.get_for_model(Restaurant).pk
self.assertNotContains(response,
'"/test_admin/admin/r/%s/1/"' % content_type_pk,
)
def test_true(self):
"Ensure that the default behaviour is followed if view_on_site is True"
response = self.client.get('/test_admin/admin/admin_views/city/1/')
content_type_pk = ContentType.objects.get_for_model(City).pk
self.assertContains(response,
'"/test_admin/admin/r/%s/1/"' % content_type_pk,
)
def test_callable(self):
"Ensure that the right link is displayed if view_on_site is a callable"
response = self.client.get('/test_admin/admin/admin_views/worker/1/')
worker = Worker.objects.get(pk=1)
self.assertContains(response,
'"/worker/%s/%s/"' % (worker.surname, worker.name),
)
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
class InlineAdminViewOnSiteTest(TestCase):
urls = "admin_views.urls"
fixtures = ['admin-views-users.xml', 'admin-views-restaurants.xml']
def setUp(self):
self.client.login(username='super', password='secret')
def tearDown(self):
self.client.logout()
def test_false(self):
"Ensure that the 'View on site' button is not displayed if view_on_site is False"
response = self.client.get('/test_admin/admin/admin_views/state/1/')
content_type_pk = ContentType.objects.get_for_model(City).pk
self.assertNotContains(response,
'/test_admin/admin/r/%s/1/' % content_type_pk,
)
def test_true(self):
"Ensure that the 'View on site' button is displayed if view_on_site is True"
response = self.client.get('/test_admin/admin/admin_views/city/1/')
content_type_pk = ContentType.objects.get_for_model(Restaurant).pk
self.assertContains(response,
'/test_admin/admin/r/%s/1/' % content_type_pk,
)
def test_callable(self):
"Ensure that the right link is displayed if view_on_site is a callable"
response = self.client.get('/test_admin/admin/admin_views/restaurant/1/')
worker = Worker.objects.get(pk=1)
self.assertContains(response,
'"/worker_inline/%s/%s/"' % (worker.surname, worker.name),
)