mirror of https://github.com/django/django.git
Fixed #23497 -- Made admin system checks run for custom AdminSites.
This commit is contained in:
parent
c7786550c4
commit
b7219c7ba5
|
@ -11,12 +11,9 @@ from django.forms.models import BaseModelForm, _get_foreign_key, BaseModelFormSe
|
||||||
|
|
||||||
|
|
||||||
def check_admin_app(**kwargs):
|
def check_admin_app(**kwargs):
|
||||||
from django.contrib.admin.sites import site
|
from django.contrib.admin.sites import system_check_errors
|
||||||
|
|
||||||
return list(chain.from_iterable(
|
return system_check_errors
|
||||||
model_admin.check(model, **kwargs)
|
|
||||||
for model, model_admin in site._registry.items()
|
|
||||||
))
|
|
||||||
|
|
||||||
|
|
||||||
class BaseModelAdminChecks(object):
|
class BaseModelAdminChecks(object):
|
||||||
|
|
|
@ -15,6 +15,8 @@ from django.utils.translation import ugettext_lazy, ugettext as _
|
||||||
from django.views.decorators.cache import never_cache
|
from django.views.decorators.cache import never_cache
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
system_check_errors = []
|
||||||
|
|
||||||
|
|
||||||
class AlreadyRegistered(Exception):
|
class AlreadyRegistered(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -99,7 +101,7 @@ class AdminSite(object):
|
||||||
admin_class = type("%sAdmin" % model.__name__, (admin_class,), options)
|
admin_class = type("%sAdmin" % model.__name__, (admin_class,), options)
|
||||||
|
|
||||||
if admin_class is not ModelAdmin and settings.DEBUG:
|
if admin_class is not ModelAdmin and settings.DEBUG:
|
||||||
admin_class.check(model)
|
system_check_errors.extend(admin_class.check(model))
|
||||||
|
|
||||||
# Instantiate the admin class to save in the registry
|
# Instantiate the admin class to save in the registry
|
||||||
self._registry[model] = admin_class(model, self)
|
self._registry[model] = admin_class(model, self)
|
||||||
|
|
|
@ -149,3 +149,5 @@ Bugfixes
|
||||||
|
|
||||||
* Restored the ``pre_migrate`` signal if all apps have migrations
|
* Restored the ``pre_migrate`` signal if all apps have migrations
|
||||||
(:ticket:`23975`).
|
(:ticket:`23975`).
|
||||||
|
|
||||||
|
* Made admin system checks run for custom ``AdminSite``\s (:ticket:`23497`).
|
||||||
|
|
|
@ -35,18 +35,20 @@ class ValidFormFieldsets(admin.ModelAdmin):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class MyAdmin(admin.ModelAdmin):
|
||||||
|
@classmethod
|
||||||
|
def check(cls, model, **kwargs):
|
||||||
|
return ['error!']
|
||||||
|
|
||||||
|
|
||||||
@override_settings(
|
@override_settings(
|
||||||
SILENCED_SYSTEM_CHECKS=['fields.W342'], # ForeignKey(unique=True)
|
SILENCED_SYSTEM_CHECKS=['fields.W342'], # ForeignKey(unique=True)
|
||||||
INSTALLED_APPS=['django.contrib.auth', 'django.contrib.contenttypes', 'admin_checks']
|
INSTALLED_APPS=['django.contrib.auth', 'django.contrib.contenttypes', 'admin_checks']
|
||||||
)
|
)
|
||||||
class SystemChecksTestCase(TestCase):
|
class SystemChecksTestCase(TestCase):
|
||||||
|
|
||||||
|
@override_settings(DEBUG=True)
|
||||||
def test_checks_are_performed(self):
|
def test_checks_are_performed(self):
|
||||||
class MyAdmin(admin.ModelAdmin):
|
|
||||||
@classmethod
|
|
||||||
def check(self, model, **kwargs):
|
|
||||||
return ['error!']
|
|
||||||
|
|
||||||
admin.site.register(Song, MyAdmin)
|
admin.site.register(Song, MyAdmin)
|
||||||
try:
|
try:
|
||||||
errors = checks.run_checks()
|
errors = checks.run_checks()
|
||||||
|
@ -54,6 +56,22 @@ class SystemChecksTestCase(TestCase):
|
||||||
self.assertEqual(errors, expected)
|
self.assertEqual(errors, expected)
|
||||||
finally:
|
finally:
|
||||||
admin.site.unregister(Song)
|
admin.site.unregister(Song)
|
||||||
|
admin.sites.system_check_errors = []
|
||||||
|
|
||||||
|
@override_settings(DEBUG=True)
|
||||||
|
def test_custom_adminsite(self):
|
||||||
|
class CustomAdminSite(admin.AdminSite):
|
||||||
|
pass
|
||||||
|
|
||||||
|
custom_site = CustomAdminSite()
|
||||||
|
custom_site.register(Song, MyAdmin)
|
||||||
|
try:
|
||||||
|
errors = checks.run_checks()
|
||||||
|
expected = ['error!']
|
||||||
|
self.assertEqual(errors, expected)
|
||||||
|
finally:
|
||||||
|
custom_site.unregister(Song)
|
||||||
|
admin.sites.system_check_errors = []
|
||||||
|
|
||||||
def test_readonly_and_editable(self):
|
def test_readonly_and_editable(self):
|
||||||
class SongAdmin(admin.ModelAdmin):
|
class SongAdmin(admin.ModelAdmin):
|
||||||
|
|
Loading…
Reference in New Issue