2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-10-14 02:51:33 +08:00
|
|
|
|
2010-09-27 23:11:55 +08:00
|
|
|
from django.contrib import admin
|
2012-12-18 08:04:10 +08:00
|
|
|
from django.contrib.admin.decorators import register
|
|
|
|
from django.contrib.admin.sites import site
|
2011-10-14 02:51:33 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-04-18 05:38:20 +08:00
|
|
|
from django.test import SimpleTestCase
|
2011-10-14 02:51:33 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import Location, Person, Place, Traveler
|
2010-09-27 23:11:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
class NameAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ['name']
|
|
|
|
save_on_top = True
|
|
|
|
|
2012-12-18 08:04:10 +08:00
|
|
|
|
|
|
|
class CustomSite(admin.AdminSite):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class TestRegistration(SimpleTestCase):
|
2010-09-27 23:11:55 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.site = admin.AdminSite()
|
|
|
|
|
|
|
|
def test_bare_registration(self):
|
|
|
|
self.site.register(Person)
|
|
|
|
self.assertTrue(
|
|
|
|
isinstance(self.site._registry[Person], admin.options.ModelAdmin)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_registration_with_model_admin(self):
|
|
|
|
self.site.register(Person, NameAdmin)
|
|
|
|
self.assertTrue(
|
|
|
|
isinstance(self.site._registry[Person], NameAdmin)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_prevent_double_registration(self):
|
|
|
|
self.site.register(Person)
|
|
|
|
self.assertRaises(admin.sites.AlreadyRegistered,
|
|
|
|
self.site.register,
|
|
|
|
Person)
|
|
|
|
|
|
|
|
def test_registration_with_star_star_options(self):
|
|
|
|
self.site.register(Person, search_fields=['name'])
|
|
|
|
self.assertEqual(self.site._registry[Person].search_fields, ['name'])
|
|
|
|
|
|
|
|
def test_star_star_overrides(self):
|
|
|
|
self.site.register(Person, NameAdmin,
|
|
|
|
search_fields=["name"], list_display=['__str__'])
|
|
|
|
self.assertEqual(self.site._registry[Person].search_fields, ['name'])
|
|
|
|
self.assertEqual(self.site._registry[Person].list_display,
|
2011-10-26 17:37:07 +08:00
|
|
|
['__str__'])
|
2010-09-27 23:11:55 +08:00
|
|
|
self.assertTrue(self.site._registry[Person].save_on_top)
|
|
|
|
|
|
|
|
def test_iterable_registration(self):
|
|
|
|
self.site.register([Person, Place], search_fields=['name'])
|
|
|
|
self.assertTrue(
|
|
|
|
isinstance(self.site._registry[Person], admin.options.ModelAdmin)
|
|
|
|
)
|
|
|
|
self.assertEqual(self.site._registry[Person].search_fields, ['name'])
|
|
|
|
self.assertTrue(
|
|
|
|
isinstance(self.site._registry[Place], admin.options.ModelAdmin)
|
|
|
|
)
|
|
|
|
self.assertEqual(self.site._registry[Place].search_fields, ['name'])
|
2011-02-23 21:43:21 +08:00
|
|
|
|
|
|
|
def test_abstract_model(self):
|
|
|
|
"""
|
|
|
|
Exception is raised when trying to register an abstract model.
|
|
|
|
Refs #12004.
|
|
|
|
"""
|
|
|
|
self.assertRaises(ImproperlyConfigured, self.site.register, Location)
|
2012-12-18 08:04:10 +08:00
|
|
|
|
2014-07-25 20:07:04 +08:00
|
|
|
def test_is_registered_model(self):
|
|
|
|
"Checks for registered models should return true."
|
|
|
|
self.site.register(Person)
|
|
|
|
self.assertTrue(self.site.is_registered(Person))
|
|
|
|
|
|
|
|
def test_is_registered_not_registered_model(self):
|
|
|
|
"Checks for unregistered models should return false."
|
|
|
|
self.assertFalse(self.site.is_registered(Person))
|
|
|
|
|
2012-12-18 08:04:10 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class TestRegistrationDecorator(SimpleTestCase):
|
2012-12-18 08:04:10 +08:00
|
|
|
"""
|
|
|
|
Tests the register decorator in admin.decorators
|
|
|
|
|
|
|
|
For clarity:
|
|
|
|
|
|
|
|
@register(Person)
|
|
|
|
class AuthorAdmin(ModelAdmin):
|
|
|
|
pass
|
|
|
|
|
|
|
|
is functionally equal to (the way it is written in these tests):
|
|
|
|
|
|
|
|
AuthorAdmin = register(Person)(AuthorAdmin)
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
self.default_site = site
|
|
|
|
self.custom_site = CustomSite()
|
|
|
|
|
|
|
|
def test_basic_registration(self):
|
|
|
|
register(Person)(NameAdmin)
|
|
|
|
self.assertTrue(
|
|
|
|
isinstance(self.default_site._registry[Person],
|
|
|
|
admin.options.ModelAdmin)
|
|
|
|
)
|
2015-05-14 20:14:34 +08:00
|
|
|
self.default_site.unregister(Person)
|
2012-12-18 08:04:10 +08:00
|
|
|
|
|
|
|
def test_custom_site_registration(self):
|
|
|
|
register(Person, site=self.custom_site)(NameAdmin)
|
|
|
|
self.assertTrue(
|
|
|
|
isinstance(self.custom_site._registry[Person],
|
|
|
|
admin.options.ModelAdmin)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_multiple_registration(self):
|
|
|
|
register(Traveler, Place)(NameAdmin)
|
|
|
|
self.assertTrue(
|
|
|
|
isinstance(self.default_site._registry[Traveler],
|
|
|
|
admin.options.ModelAdmin)
|
|
|
|
)
|
2015-05-14 20:14:34 +08:00
|
|
|
self.default_site.unregister(Traveler)
|
2012-12-18 08:04:10 +08:00
|
|
|
self.assertTrue(
|
|
|
|
isinstance(self.default_site._registry[Place],
|
|
|
|
admin.options.ModelAdmin)
|
|
|
|
)
|
2015-05-14 20:14:34 +08:00
|
|
|
self.default_site.unregister(Place)
|
2012-12-18 08:04:10 +08:00
|
|
|
|
|
|
|
def test_wrapped_class_not_a_model_admin(self):
|
2013-09-19 18:24:12 +08:00
|
|
|
self.assertRaisesMessage(ValueError, 'Wrapped class must subclass ModelAdmin.',
|
2012-12-18 08:04:10 +08:00
|
|
|
register(Person), CustomSite)
|
|
|
|
|
|
|
|
def test_custom_site_not_an_admin_site(self):
|
|
|
|
self.assertRaisesMessage(ValueError, 'site must subclass AdminSite',
|
|
|
|
register(Person, site=Traveler), NameAdmin)
|