2010-09-27 15:11:55 +00:00
|
|
|
from django.contrib import admin
|
2012-12-18 00:04:10 +00:00
|
|
|
from django.contrib.admin.decorators import register
|
2023-07-07 11:22:06 +00:00
|
|
|
from django.contrib.admin.exceptions import AlreadyRegistered, NotRegistered
|
2012-12-18 00:04:10 +00:00
|
|
|
from django.contrib.admin.sites import site
|
2011-10-13 18:51:33 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-04-17 21:38:20 +00:00
|
|
|
from django.test import SimpleTestCase
|
2011-10-13 18:51:33 +00:00
|
|
|
|
2024-04-07 02:32:16 +00:00
|
|
|
from .models import Guest, Location, Person, Place, Traveler
|
2010-09-27 15:11:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NameAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ["name"]
|
|
|
|
save_on_top = True
|
|
|
|
|
2012-12-18 00:04:10 +00:00
|
|
|
|
|
|
|
class CustomSite(admin.AdminSite):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class TestRegistration(SimpleTestCase):
|
2010-09-27 15:11:55 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.site = admin.AdminSite()
|
|
|
|
|
|
|
|
def test_bare_registration(self):
|
|
|
|
self.site.register(Person)
|
2023-07-07 06:06:01 +00:00
|
|
|
self.assertIsInstance(self.site.get_model_admin(Person), admin.ModelAdmin)
|
2020-12-16 19:56:18 +00:00
|
|
|
self.site.unregister(Person)
|
|
|
|
self.assertEqual(self.site._registry, {})
|
2010-09-27 15:11:55 +00:00
|
|
|
|
|
|
|
def test_registration_with_model_admin(self):
|
|
|
|
self.site.register(Person, NameAdmin)
|
2023-07-07 06:06:01 +00:00
|
|
|
self.assertIsInstance(self.site.get_model_admin(Person), NameAdmin)
|
2020-12-16 19:56:18 +00:00
|
|
|
self.site.unregister(Person)
|
|
|
|
self.assertEqual(self.site._registry, {})
|
2010-09-27 15:11:55 +00:00
|
|
|
|
|
|
|
def test_prevent_double_registration(self):
|
|
|
|
self.site.register(Person)
|
2019-03-23 17:10:06 +00:00
|
|
|
msg = "The model Person is already registered in app 'admin_registration'."
|
2023-07-07 11:22:06 +00:00
|
|
|
with self.assertRaisesMessage(AlreadyRegistered, msg):
|
2016-01-17 11:26:39 +00:00
|
|
|
self.site.register(Person)
|
2010-09-27 15:11:55 +00:00
|
|
|
|
2019-03-23 17:10:06 +00:00
|
|
|
def test_prevent_double_registration_for_custom_admin(self):
|
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.site.register(Person, PersonAdmin)
|
|
|
|
msg = (
|
|
|
|
"The model Person is already registered with "
|
|
|
|
"'admin_registration.PersonAdmin'."
|
2022-02-04 07:08:27 +00:00
|
|
|
)
|
2023-07-07 11:22:06 +00:00
|
|
|
with self.assertRaisesMessage(AlreadyRegistered, msg):
|
2019-03-23 17:10:06 +00:00
|
|
|
self.site.register(Person, PersonAdmin)
|
|
|
|
|
2020-12-16 19:56:18 +00:00
|
|
|
def test_unregister_unregistered_model(self):
|
|
|
|
msg = "The model Person is not registered"
|
2023-07-07 11:22:06 +00:00
|
|
|
with self.assertRaisesMessage(NotRegistered, msg):
|
2020-12-16 19:56:18 +00:00
|
|
|
self.site.unregister(Person)
|
|
|
|
|
2010-09-27 15:11:55 +00:00
|
|
|
def test_registration_with_star_star_options(self):
|
|
|
|
self.site.register(Person, search_fields=["name"])
|
2023-07-07 06:06:01 +00:00
|
|
|
self.assertEqual(self.site.get_model_admin(Person).search_fields, ["name"])
|
|
|
|
|
|
|
|
def test_get_model_admin_unregister_model(self):
|
|
|
|
msg = "The model Person is not registered."
|
2023-07-07 11:22:06 +00:00
|
|
|
with self.assertRaisesMessage(NotRegistered, msg):
|
2023-07-07 06:06:01 +00:00
|
|
|
self.site.get_model_admin(Person)
|
2010-09-27 15:11:55 +00:00
|
|
|
|
|
|
|
def test_star_star_overrides(self):
|
2016-03-24 23:20:31 +00:00
|
|
|
self.site.register(
|
|
|
|
Person, NameAdmin, search_fields=["name"], list_display=["__str__"]
|
|
|
|
)
|
2023-07-07 06:06:01 +00:00
|
|
|
person_admin = self.site.get_model_admin(Person)
|
|
|
|
self.assertEqual(person_admin.search_fields, ["name"])
|
|
|
|
self.assertEqual(person_admin.list_display, ["__str__"])
|
|
|
|
self.assertIs(person_admin.save_on_top, True)
|
2010-09-27 15:11:55 +00:00
|
|
|
|
|
|
|
def test_iterable_registration(self):
|
|
|
|
self.site.register([Person, Place], search_fields=["name"])
|
2023-07-07 06:06:01 +00:00
|
|
|
self.assertIsInstance(self.site.get_model_admin(Person), admin.ModelAdmin)
|
|
|
|
self.assertEqual(self.site.get_model_admin(Person).search_fields, ["name"])
|
|
|
|
self.assertIsInstance(self.site.get_model_admin(Place), admin.ModelAdmin)
|
|
|
|
self.assertEqual(self.site.get_model_admin(Place).search_fields, ["name"])
|
2020-12-16 19:56:18 +00:00
|
|
|
self.site.unregister([Person, Place])
|
|
|
|
self.assertEqual(self.site._registry, {})
|
2011-02-23 13:43:21 +00:00
|
|
|
|
|
|
|
def test_abstract_model(self):
|
|
|
|
"""
|
|
|
|
Exception is raised when trying to register an abstract model.
|
|
|
|
Refs #12004.
|
|
|
|
"""
|
2017-05-28 19:37:21 +00:00
|
|
|
msg = "The model Location is abstract, so it cannot be registered with admin."
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2016-01-17 11:26:39 +00:00
|
|
|
self.site.register(Location)
|
2012-12-18 00:04:10 +00:00
|
|
|
|
2024-04-07 02:32:16 +00:00
|
|
|
def test_composite_pk_model(self):
|
|
|
|
msg = (
|
|
|
|
"The model Guest has a composite primary key, so it cannot be registered "
|
|
|
|
"with admin."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
self.site.register(Guest)
|
|
|
|
|
2014-07-25 12:07:04 +00: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 00:04:10 +00:00
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class TestRegistrationDecorator(SimpleTestCase):
|
2012-12-18 00:04:10 +00: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)
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2012-12-18 00:04:10 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.default_site = site
|
|
|
|
self.custom_site = CustomSite()
|
|
|
|
|
|
|
|
def test_basic_registration(self):
|
|
|
|
register(Person)(NameAdmin)
|
2023-07-07 06:06:01 +00:00
|
|
|
self.assertIsInstance(
|
|
|
|
self.default_site.get_model_admin(Person), admin.ModelAdmin
|
|
|
|
)
|
2015-05-14 12:14:34 +00:00
|
|
|
self.default_site.unregister(Person)
|
2012-12-18 00:04:10 +00:00
|
|
|
|
|
|
|
def test_custom_site_registration(self):
|
|
|
|
register(Person, site=self.custom_site)(NameAdmin)
|
2023-07-07 06:06:01 +00:00
|
|
|
self.assertIsInstance(
|
|
|
|
self.custom_site.get_model_admin(Person), admin.ModelAdmin
|
|
|
|
)
|
2012-12-18 00:04:10 +00:00
|
|
|
|
|
|
|
def test_multiple_registration(self):
|
|
|
|
register(Traveler, Place)(NameAdmin)
|
2023-07-07 06:06:01 +00:00
|
|
|
self.assertIsInstance(
|
|
|
|
self.default_site.get_model_admin(Traveler), admin.ModelAdmin
|
|
|
|
)
|
2015-05-14 12:14:34 +00:00
|
|
|
self.default_site.unregister(Traveler)
|
2023-07-07 06:06:01 +00:00
|
|
|
self.assertIsInstance(
|
|
|
|
self.default_site.get_model_admin(Place), admin.ModelAdmin
|
|
|
|
)
|
2015-05-14 12:14:34 +00:00
|
|
|
self.default_site.unregister(Place)
|
2012-12-18 00:04:10 +00:00
|
|
|
|
|
|
|
def test_wrapped_class_not_a_model_admin(self):
|
2016-01-04 08:50:08 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValueError, "Wrapped class must subclass ModelAdmin."
|
|
|
|
):
|
|
|
|
register(Person)(CustomSite)
|
2012-12-18 00:04:10 +00:00
|
|
|
|
|
|
|
def test_custom_site_not_an_admin_site(self):
|
2016-01-04 08:50:08 +00:00
|
|
|
with self.assertRaisesMessage(ValueError, "site must subclass AdminSite"):
|
|
|
|
register(Person, site=Traveler)(NameAdmin)
|
2015-11-06 06:21:29 +00:00
|
|
|
|
|
|
|
def test_empty_models_list_registration_fails(self):
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValueError, "At least one model must be passed to register."
|
|
|
|
):
|
|
|
|
register()(NameAdmin)
|