2014-01-20 10:45:21 +08:00
|
|
|
from itertools import chain
|
|
|
|
|
2015-12-10 20:45:21 +08:00
|
|
|
from django.apps import apps
|
|
|
|
from django.conf import settings
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.admin.utils import (
|
|
|
|
NotRelationField, flatten, get_fields_from_path,
|
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
from django.core import checks
|
2015-01-02 23:14:23 +08:00
|
|
|
from django.core.exceptions import FieldDoesNotExist
|
2014-01-20 10:45:21 +08:00
|
|
|
from django.db import models
|
2016-10-06 18:02:48 +08:00
|
|
|
from django.db.models.constants import LOOKUP_SEP
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.forms.models import (
|
|
|
|
BaseModelForm, BaseModelFormSet, _get_foreign_key,
|
|
|
|
)
|
2015-12-10 20:45:21 +08:00
|
|
|
from django.template.engine import Engine
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
2017-01-03 05:42:27 +08:00
|
|
|
def check_admin_app(app_configs, **kwargs):
|
|
|
|
from django.contrib.admin.sites import all_sites
|
|
|
|
errors = []
|
|
|
|
for site in all_sites:
|
|
|
|
errors.extend(site.check(app_configs))
|
|
|
|
return errors
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
2015-12-10 20:45:21 +08:00
|
|
|
def check_dependencies(**kwargs):
|
|
|
|
"""
|
|
|
|
Check that the admin's dependencies are correctly installed.
|
|
|
|
"""
|
|
|
|
errors = []
|
|
|
|
# contrib.contenttypes must be installed.
|
|
|
|
if not apps.is_installed('django.contrib.contenttypes'):
|
|
|
|
missing_app = checks.Error(
|
|
|
|
"'django.contrib.contenttypes' must be in INSTALLED_APPS in order "
|
|
|
|
"to use the admin application.",
|
|
|
|
id="admin.E401",
|
|
|
|
)
|
|
|
|
errors.append(missing_app)
|
|
|
|
# The auth context processor must be installed if using the default
|
|
|
|
# authentication backend.
|
|
|
|
try:
|
|
|
|
default_template_engine = Engine.get_default()
|
|
|
|
except Exception:
|
|
|
|
# Skip this non-critical check:
|
|
|
|
# 1. if the user has a non-trivial TEMPLATES setting and Django
|
|
|
|
# can't find a default template engine
|
|
|
|
# 2. if anything goes wrong while loading template engines, in
|
|
|
|
# order to avoid raising an exception from a confusing location
|
|
|
|
# Catching ImproperlyConfigured suffices for 1. but 2. requires
|
|
|
|
# catching all exceptions.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if ('django.contrib.auth.context_processors.auth'
|
2016-04-04 08:37:32 +08:00
|
|
|
not in default_template_engine.context_processors and
|
|
|
|
'django.contrib.auth.backends.ModelBackend' in settings.AUTHENTICATION_BACKENDS):
|
2015-12-10 20:45:21 +08:00
|
|
|
missing_template = checks.Error(
|
|
|
|
"'django.contrib.auth.context_processors.auth' must be in "
|
|
|
|
"TEMPLATES in order to use the admin application.",
|
|
|
|
id="admin.E402"
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2015-12-10 20:45:21 +08:00
|
|
|
errors.append(missing_template)
|
|
|
|
return errors
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class BaseModelAdminChecks:
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def check(self, admin_obj, **kwargs):
|
2014-01-20 10:45:21 +08:00
|
|
|
errors = []
|
2015-09-10 21:05:31 +08:00
|
|
|
errors.extend(self._check_raw_id_fields(admin_obj))
|
|
|
|
errors.extend(self._check_fields(admin_obj))
|
|
|
|
errors.extend(self._check_fieldsets(admin_obj))
|
|
|
|
errors.extend(self._check_exclude(admin_obj))
|
|
|
|
errors.extend(self._check_form(admin_obj))
|
|
|
|
errors.extend(self._check_filter_vertical(admin_obj))
|
|
|
|
errors.extend(self._check_filter_horizontal(admin_obj))
|
|
|
|
errors.extend(self._check_radio_fields(admin_obj))
|
|
|
|
errors.extend(self._check_prepopulated_fields(admin_obj))
|
|
|
|
errors.extend(self._check_view_on_site_url(admin_obj))
|
|
|
|
errors.extend(self._check_ordering(admin_obj))
|
|
|
|
errors.extend(self._check_readonly_fields(admin_obj))
|
2014-01-20 10:45:21 +08:00
|
|
|
return errors
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_raw_id_fields(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that `raw_id_fields` only contains field names that are listed
|
|
|
|
on the model. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if not isinstance(obj.raw_id_fields, (list, tuple)):
|
|
|
|
return must_be('a list or tuple', option='raw_id_fields', obj=obj, id='admin.E001')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_raw_id_fields_item(obj, obj.model, field_name, 'raw_id_fields[%d]' % index)
|
|
|
|
for index, field_name in enumerate(obj.raw_id_fields)
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_raw_id_fields_item(self, obj, model, field_name, label):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check an item of `raw_id_fields`, i.e. check that field named
|
|
|
|
`field_name` exists in model `model` and is a ForeignKey or a
|
|
|
|
ManyToManyField. """
|
|
|
|
|
|
|
|
try:
|
|
|
|
field = model._meta.get_field(field_name)
|
2015-01-02 23:14:23 +08:00
|
|
|
except FieldDoesNotExist:
|
2014-01-20 10:45:21 +08:00
|
|
|
return refer_to_missing_field(field=field_name, option=label,
|
2015-09-10 21:05:31 +08:00
|
|
|
model=model, obj=obj, id='admin.E002')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2016-03-11 01:21:25 +08:00
|
|
|
if not field.many_to_many and not isinstance(field, models.ForeignKey):
|
|
|
|
return must_be('a foreign key or a many-to-many field',
|
2015-09-10 21:05:31 +08:00
|
|
|
option=label, obj=obj, id='admin.E003')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_fields(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that `fields` only refer to existing fields, doesn't contain
|
|
|
|
duplicates. Check if at most one of `fields` and `fieldsets` is defined.
|
|
|
|
"""
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if obj.fields is None:
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
2015-09-10 21:05:31 +08:00
|
|
|
elif not isinstance(obj.fields, (list, tuple)):
|
|
|
|
return must_be('a list or tuple', option='fields', obj=obj, id='admin.E004')
|
|
|
|
elif obj.fieldsets:
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"Both 'fieldsets' and 'fields' are specified.",
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E005',
|
|
|
|
)
|
|
|
|
]
|
2015-09-10 21:05:31 +08:00
|
|
|
fields = flatten(obj.fields)
|
2014-02-15 18:28:09 +08:00
|
|
|
if len(fields) != len(set(fields)):
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of 'fields' contains duplicate field(s).",
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E006',
|
|
|
|
)
|
|
|
|
]
|
2014-02-15 18:28:09 +08:00
|
|
|
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_field_spec(obj, obj.model, field_name, 'fields')
|
|
|
|
for field_name in obj.fields
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_fieldsets(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that fieldsets is properly formatted and doesn't contain
|
|
|
|
duplicates. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if obj.fieldsets is None:
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
2015-09-10 21:05:31 +08:00
|
|
|
elif not isinstance(obj.fieldsets, (list, tuple)):
|
|
|
|
return must_be('a list or tuple', option='fieldsets', obj=obj, id='admin.E007')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_fieldsets_item(obj, obj.model, fieldset, 'fieldsets[%d]' % index)
|
|
|
|
for index, fieldset in enumerate(obj.fieldsets)
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_fieldsets_item(self, obj, model, fieldset, label):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check an item of `fieldsets`, i.e. check that this is a pair of a
|
|
|
|
set name and a dictionary containing "fields" key. """
|
|
|
|
|
|
|
|
if not isinstance(fieldset, (list, tuple)):
|
2015-09-10 21:05:31 +08:00
|
|
|
return must_be('a list or tuple', option=label, obj=obj, id='admin.E008')
|
2014-01-20 10:45:21 +08:00
|
|
|
elif len(fieldset) != 2:
|
2015-09-10 21:05:31 +08:00
|
|
|
return must_be('of length 2', option=label, obj=obj, id='admin.E009')
|
2014-01-20 10:45:21 +08:00
|
|
|
elif not isinstance(fieldset[1], dict):
|
2015-09-10 21:05:31 +08:00
|
|
|
return must_be('a dictionary', option='%s[1]' % label, obj=obj, id='admin.E010')
|
2014-01-20 10:45:21 +08:00
|
|
|
elif 'fields' not in fieldset[1]:
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of '%s[1]' must contain the key 'fields'." % label,
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E011',
|
|
|
|
)
|
|
|
|
]
|
2015-01-11 00:45:05 +08:00
|
|
|
elif not isinstance(fieldset[1]['fields'], (list, tuple)):
|
2015-09-10 21:05:31 +08:00
|
|
|
return must_be('a list or tuple', option="%s[1]['fields']" % label, obj=obj, id='admin.E008')
|
2014-02-15 18:28:09 +08:00
|
|
|
|
|
|
|
fields = flatten(fieldset[1]['fields'])
|
|
|
|
if len(fields) != len(set(fields)):
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"There are duplicate field(s) in '%s[1]'." % label,
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E012',
|
|
|
|
)
|
|
|
|
]
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_field_spec(obj, model, fieldset_fields, '%s[1]["fields"]' % label)
|
2014-02-16 00:21:15 +08:00
|
|
|
for fieldset_fields in fieldset[1]['fields']
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_field_spec(self, obj, model, fields, label):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" `fields` should be an item of `fields` or an item of
|
|
|
|
fieldset[1]['fields'] for any `fieldset` in `fieldsets`. It should be a
|
|
|
|
field name or a tuple of field names. """
|
|
|
|
|
|
|
|
if isinstance(fields, tuple):
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_field_spec_item(obj, model, field_name, "%s[%d]" % (label, index))
|
2014-01-20 10:45:21 +08:00
|
|
|
for index, field_name in enumerate(fields)
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2015-09-10 21:05:31 +08:00
|
|
|
return self._check_field_spec_item(obj, model, fields, label)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_field_spec_item(self, obj, model, field_name, label):
|
|
|
|
if field_name in obj.readonly_fields:
|
2014-01-20 10:45:21 +08:00
|
|
|
# Stuff can be put in fields that isn't actually a model field if
|
|
|
|
# it's in readonly_fields, readonly_fields will handle the
|
|
|
|
# validation of such things.
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
field = model._meta.get_field(field_name)
|
2015-01-02 23:14:23 +08:00
|
|
|
except FieldDoesNotExist:
|
2014-01-20 10:45:21 +08:00
|
|
|
# If we can't find a field on the model that matches, it could
|
|
|
|
# be an extra field on the form.
|
|
|
|
return []
|
|
|
|
else:
|
2016-08-19 00:45:27 +08:00
|
|
|
if (isinstance(field, models.ManyToManyField) and
|
|
|
|
not field.remote_field.through._meta.auto_created):
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2016-08-19 00:45:27 +08:00
|
|
|
"The value of '%s' cannot include the ManyToManyField '%s', "
|
2016-02-13 00:36:46 +08:00
|
|
|
"because that field manually specifies a relationship model."
|
2014-03-03 13:27:17 +08:00
|
|
|
% (label, field_name),
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E013',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_exclude(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that exclude is a sequence without duplicates. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if obj.exclude is None: # default value is None
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
2015-09-10 21:05:31 +08:00
|
|
|
elif not isinstance(obj.exclude, (list, tuple)):
|
|
|
|
return must_be('a list or tuple', option='exclude', obj=obj, id='admin.E014')
|
|
|
|
elif len(obj.exclude) > len(set(obj.exclude)):
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of 'exclude' contains duplicate field(s).",
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E015',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_form(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that form subclasses BaseModelForm. """
|
2017-06-03 02:02:25 +08:00
|
|
|
if not issubclass(obj.form, BaseModelForm):
|
2014-01-20 10:45:21 +08:00
|
|
|
return must_inherit_from(parent='BaseModelForm', option='form',
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj, id='admin.E016')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_filter_vertical(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that filter_vertical is a sequence of field names. """
|
2017-06-03 02:02:25 +08:00
|
|
|
if not isinstance(obj.filter_vertical, (list, tuple)):
|
2015-09-10 21:05:31 +08:00
|
|
|
return must_be('a list or tuple', option='filter_vertical', obj=obj, id='admin.E017')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_filter_item(obj, obj.model, field_name, "filter_vertical[%d]" % index)
|
|
|
|
for index, field_name in enumerate(obj.filter_vertical)
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_filter_horizontal(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that filter_horizontal is a sequence of field names. """
|
2017-06-03 02:02:25 +08:00
|
|
|
if not isinstance(obj.filter_horizontal, (list, tuple)):
|
2015-09-10 21:05:31 +08:00
|
|
|
return must_be('a list or tuple', option='filter_horizontal', obj=obj, id='admin.E018')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_filter_item(obj, obj.model, field_name, "filter_horizontal[%d]" % index)
|
|
|
|
for index, field_name in enumerate(obj.filter_horizontal)
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_filter_item(self, obj, model, field_name, label):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check one item of `filter_vertical` or `filter_horizontal`, i.e.
|
|
|
|
check that given field exists and is a ManyToManyField. """
|
|
|
|
|
|
|
|
try:
|
|
|
|
field = model._meta.get_field(field_name)
|
2015-01-02 23:14:23 +08:00
|
|
|
except FieldDoesNotExist:
|
2014-01-20 10:45:21 +08:00
|
|
|
return refer_to_missing_field(field=field_name, option=label,
|
2015-09-10 21:05:31 +08:00
|
|
|
model=model, obj=obj, id='admin.E019')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2016-03-11 01:21:25 +08:00
|
|
|
if not field.many_to_many:
|
|
|
|
return must_be('a many-to-many field', option=label, obj=obj, id='admin.E020')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_radio_fields(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that `radio_fields` is a dictionary. """
|
2017-06-03 02:02:25 +08:00
|
|
|
if not isinstance(obj.radio_fields, dict):
|
2015-09-10 21:05:31 +08:00
|
|
|
return must_be('a dictionary', option='radio_fields', obj=obj, id='admin.E021')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_radio_fields_key(obj, obj.model, field_name, 'radio_fields') +
|
|
|
|
self._check_radio_fields_value(obj, val, 'radio_fields["%s"]' % field_name)
|
|
|
|
for field_name, val in obj.radio_fields.items()
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_radio_fields_key(self, obj, model, field_name, label):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that a key of `radio_fields` dictionary is name of existing
|
|
|
|
field and that the field is a ForeignKey or has `choices` defined. """
|
|
|
|
|
|
|
|
try:
|
|
|
|
field = model._meta.get_field(field_name)
|
2015-01-02 23:14:23 +08:00
|
|
|
except FieldDoesNotExist:
|
2014-01-20 10:45:21 +08:00
|
|
|
return refer_to_missing_field(field=field_name, option=label,
|
2015-09-10 21:05:31 +08:00
|
|
|
model=model, obj=obj, id='admin.E022')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
if not (isinstance(field, models.ForeignKey) or field.choices):
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-09-04 20:15:09 +08:00
|
|
|
"The value of '%s' refers to '%s', which is not an "
|
|
|
|
"instance of ForeignKey, and does not have a 'choices' definition." % (
|
2014-01-20 10:45:21 +08:00
|
|
|
label, field_name
|
|
|
|
),
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E023',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_radio_fields_value(self, obj, val, label):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check type of a value of `radio_fields` dictionary. """
|
|
|
|
|
|
|
|
from django.contrib.admin.options import HORIZONTAL, VERTICAL
|
|
|
|
|
|
|
|
if val not in (HORIZONTAL, VERTICAL):
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of '%s' must be either admin.HORIZONTAL or admin.VERTICAL." % label,
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E024',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_view_on_site_url(self, obj):
|
2017-06-03 02:02:25 +08:00
|
|
|
if not callable(obj.view_on_site) and not isinstance(obj.view_on_site, bool):
|
|
|
|
return [
|
|
|
|
checks.Error(
|
|
|
|
"The value of 'view_on_site' must be a callable or a boolean value.",
|
|
|
|
obj=obj.__class__,
|
|
|
|
id='admin.E025',
|
|
|
|
)
|
|
|
|
]
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_prepopulated_fields(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that `prepopulated_fields` is a dictionary containing allowed
|
|
|
|
field types. """
|
2017-06-03 02:02:25 +08:00
|
|
|
if not isinstance(obj.prepopulated_fields, dict):
|
2015-09-10 21:05:31 +08:00
|
|
|
return must_be('a dictionary', option='prepopulated_fields', obj=obj, id='admin.E026')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_prepopulated_fields_key(obj, obj.model, field_name, 'prepopulated_fields') +
|
|
|
|
self._check_prepopulated_fields_value(obj, obj.model, val, 'prepopulated_fields["%s"]' % field_name)
|
|
|
|
for field_name, val in obj.prepopulated_fields.items()
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_prepopulated_fields_key(self, obj, model, field_name, label):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check a key of `prepopulated_fields` dictionary, i.e. check that it
|
|
|
|
is a name of existing field and the field is one of the allowed types.
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
field = model._meta.get_field(field_name)
|
2015-01-02 23:14:23 +08:00
|
|
|
except FieldDoesNotExist:
|
2014-01-20 10:45:21 +08:00
|
|
|
return refer_to_missing_field(field=field_name, option=label,
|
2015-09-10 21:05:31 +08:00
|
|
|
model=model, obj=obj, id='admin.E027')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2016-08-19 00:45:27 +08:00
|
|
|
if isinstance(field, (models.DateTimeField, models.ForeignKey, models.ManyToManyField)):
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of '%s' refers to '%s', which must not be a DateTimeField, "
|
2016-12-19 21:33:46 +08:00
|
|
|
"a ForeignKey, a OneToOneField, or a ManyToManyField." % (label, field_name),
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E028',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_prepopulated_fields_value(self, obj, model, val, label):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check a value of `prepopulated_fields` dictionary, i.e. it's an
|
|
|
|
iterable of existing fields. """
|
|
|
|
|
|
|
|
if not isinstance(val, (list, tuple)):
|
2015-09-10 21:05:31 +08:00
|
|
|
return must_be('a list or tuple', option=label, obj=obj, id='admin.E029')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_prepopulated_fields_value_item(obj, model, subfield_name, "%s[%r]" % (label, index))
|
2014-01-20 10:45:21 +08:00
|
|
|
for index, subfield_name in enumerate(val)
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_prepopulated_fields_value_item(self, obj, model, field_name, label):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" For `prepopulated_fields` equal to {"slug": ("title",)},
|
|
|
|
`field_name` is "title". """
|
|
|
|
|
|
|
|
try:
|
|
|
|
model._meta.get_field(field_name)
|
2015-01-02 23:14:23 +08:00
|
|
|
except FieldDoesNotExist:
|
2016-02-13 00:36:46 +08:00
|
|
|
return refer_to_missing_field(field=field_name, option=label, model=model, obj=obj, id='admin.E030')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_ordering(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that ordering refers to existing fields or is random. """
|
|
|
|
|
|
|
|
# ordering = None
|
2015-09-10 21:05:31 +08:00
|
|
|
if obj.ordering is None: # The default value is None
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
2015-09-10 21:05:31 +08:00
|
|
|
elif not isinstance(obj.ordering, (list, tuple)):
|
|
|
|
return must_be('a list or tuple', option='ordering', obj=obj, id='admin.E031')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_ordering_item(obj, obj.model, field_name, 'ordering[%d]' % index)
|
|
|
|
for index, field_name in enumerate(obj.ordering)
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_ordering_item(self, obj, model, field_name, label):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that `ordering` refers to existing fields. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if field_name == '?' and len(obj.ordering) != 1:
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2016-02-13 00:36:46 +08:00
|
|
|
"The value of 'ordering' has the random ordering marker '?', "
|
|
|
|
"but contains other fields as well.",
|
2014-01-20 10:45:21 +08:00
|
|
|
hint='Either remove the "?", or remove the other fields.',
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E032',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
elif field_name == '?':
|
|
|
|
return []
|
2016-10-06 18:02:48 +08:00
|
|
|
elif LOOKUP_SEP in field_name:
|
2014-01-20 10:45:21 +08:00
|
|
|
# Skip ordering in the format field1__field2 (FIXME: checking
|
|
|
|
# this format would be nice, but it's a little fiddly).
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
if field_name.startswith('-'):
|
|
|
|
field_name = field_name[1:]
|
2017-03-02 23:25:05 +08:00
|
|
|
if field_name == 'pk':
|
|
|
|
return []
|
2014-01-20 10:45:21 +08:00
|
|
|
try:
|
|
|
|
model._meta.get_field(field_name)
|
2015-01-02 23:14:23 +08:00
|
|
|
except FieldDoesNotExist:
|
2016-02-13 00:36:46 +08:00
|
|
|
return refer_to_missing_field(field=field_name, option=label, model=model, obj=obj, id='admin.E033')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_readonly_fields(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that readonly_fields refers to proper attribute or field. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if obj.readonly_fields == ():
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
2015-09-10 21:05:31 +08:00
|
|
|
elif not isinstance(obj.readonly_fields, (list, tuple)):
|
|
|
|
return must_be('a list or tuple', option='readonly_fields', obj=obj, id='admin.E034')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_readonly_fields_item(obj, obj.model, field_name, "readonly_fields[%d]" % index)
|
|
|
|
for index, field_name in enumerate(obj.readonly_fields)
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_readonly_fields_item(self, obj, model, field_name, label):
|
2014-01-20 10:45:21 +08:00
|
|
|
if callable(field_name):
|
|
|
|
return []
|
2015-09-10 21:05:31 +08:00
|
|
|
elif hasattr(obj, field_name):
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
|
|
|
elif hasattr(model, field_name):
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
model._meta.get_field(field_name)
|
2015-01-02 23:14:23 +08:00
|
|
|
except FieldDoesNotExist:
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of '%s' is not a callable, an attribute of '%s', or an attribute of '%s.%s'." % (
|
2015-09-10 21:05:31 +08:00
|
|
|
label, obj.__class__.__name__, model._meta.app_label, model._meta.object_name
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E035',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
class ModelAdminChecks(BaseModelAdminChecks):
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def check(self, admin_obj, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
errors = super().check(admin_obj)
|
2015-09-10 21:05:31 +08:00
|
|
|
errors.extend(self._check_save_as(admin_obj))
|
|
|
|
errors.extend(self._check_save_on_top(admin_obj))
|
|
|
|
errors.extend(self._check_inlines(admin_obj))
|
|
|
|
errors.extend(self._check_list_display(admin_obj))
|
|
|
|
errors.extend(self._check_list_display_links(admin_obj))
|
|
|
|
errors.extend(self._check_list_filter(admin_obj))
|
|
|
|
errors.extend(self._check_list_select_related(admin_obj))
|
|
|
|
errors.extend(self._check_list_per_page(admin_obj))
|
|
|
|
errors.extend(self._check_list_max_show_all(admin_obj))
|
|
|
|
errors.extend(self._check_list_editable(admin_obj))
|
|
|
|
errors.extend(self._check_search_fields(admin_obj))
|
|
|
|
errors.extend(self._check_date_hierarchy(admin_obj))
|
2014-01-20 10:45:21 +08:00
|
|
|
return errors
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_save_as(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check save_as is a boolean. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if not isinstance(obj.save_as, bool):
|
2014-01-20 10:45:21 +08:00
|
|
|
return must_be('a boolean', option='save_as',
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj, id='admin.E101')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_save_on_top(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check save_on_top is a boolean. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if not isinstance(obj.save_on_top, bool):
|
2014-01-20 10:45:21 +08:00
|
|
|
return must_be('a boolean', option='save_on_top',
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj, id='admin.E102')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_inlines(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check all inline model admin classes. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if not isinstance(obj.inlines, (list, tuple)):
|
|
|
|
return must_be('a list or tuple', option='inlines', obj=obj, id='admin.E103')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_inlines_item(obj, obj.model, item, "inlines[%d]" % index)
|
|
|
|
for index, item in enumerate(obj.inlines)
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_inlines_item(self, obj, model, inline, label):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check one inline model admin. """
|
2017-05-21 05:16:36 +08:00
|
|
|
inline_label = inline.__module__ + '.' + inline.__name__
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2016-08-25 05:34:32 +08:00
|
|
|
from django.contrib.admin.options import InlineModelAdmin
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2016-08-25 05:34:32 +08:00
|
|
|
if not issubclass(inline, InlineModelAdmin):
|
2014-03-03 13:27:17 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2016-08-25 05:34:32 +08:00
|
|
|
"'%s' must inherit from 'InlineModelAdmin'." % inline_label,
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-03-03 13:27:17 +08:00
|
|
|
id='admin.E104',
|
|
|
|
)
|
|
|
|
]
|
2014-01-20 10:45:21 +08:00
|
|
|
elif not inline.model:
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"'%s' must have a 'model' attribute." % inline_label,
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E105',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
elif not issubclass(inline.model, models.Model):
|
2016-02-13 00:36:46 +08:00
|
|
|
return must_be('a Model', option='%s.model' % inline_label, obj=obj, id='admin.E106')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2015-09-10 21:05:31 +08:00
|
|
|
return inline(model, obj.admin_site).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_list_display(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that list_display only contains fields or usable attributes.
|
|
|
|
"""
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if not isinstance(obj.list_display, (list, tuple)):
|
|
|
|
return must_be('a list or tuple', option='list_display', obj=obj, id='admin.E107')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_list_display_item(obj, obj.model, item, "list_display[%d]" % index)
|
|
|
|
for index, item in enumerate(obj.list_display)
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_list_display_item(self, obj, model, item, label):
|
2014-01-20 10:45:21 +08:00
|
|
|
if callable(item):
|
|
|
|
return []
|
2015-09-10 21:05:31 +08:00
|
|
|
elif hasattr(obj, item):
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
|
|
|
elif hasattr(model, item):
|
|
|
|
# getattr(model, item) could be an X_RelatedObjectsDescriptor
|
|
|
|
try:
|
|
|
|
field = model._meta.get_field(item)
|
2015-01-02 23:14:23 +08:00
|
|
|
except FieldDoesNotExist:
|
2014-01-20 10:45:21 +08:00
|
|
|
try:
|
|
|
|
field = getattr(model, item)
|
|
|
|
except AttributeError:
|
|
|
|
field = None
|
|
|
|
|
|
|
|
if field is None:
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-09-04 20:15:09 +08:00
|
|
|
"The value of '%s' refers to '%s', which is not a "
|
|
|
|
"callable, an attribute of '%s', or an attribute or method on '%s.%s'." % (
|
2015-09-10 21:05:31 +08:00
|
|
|
label, item, obj.__class__.__name__, model._meta.app_label, model._meta.object_name
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E108',
|
|
|
|
)
|
|
|
|
]
|
2016-08-19 00:45:27 +08:00
|
|
|
elif isinstance(field, models.ManyToManyField):
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2016-08-19 00:45:27 +08:00
|
|
|
"The value of '%s' must not be a ManyToManyField." % label,
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E109',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
model._meta.get_field(item)
|
2015-01-02 23:14:23 +08:00
|
|
|
except FieldDoesNotExist:
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
2014-03-03 13:27:17 +08:00
|
|
|
# This is a deliberate repeat of E108; there's more than one path
|
|
|
|
# required to test this condition.
|
2014-01-20 10:45:21 +08:00
|
|
|
checks.Error(
|
2014-09-04 20:15:09 +08:00
|
|
|
"The value of '%s' refers to '%s', which is not a callable, "
|
|
|
|
"an attribute of '%s', or an attribute or method on '%s.%s'." % (
|
2015-09-10 21:05:31 +08:00
|
|
|
label, item, obj.__class__.__name__, model._meta.app_label, model._meta.object_name
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-03-03 13:27:17 +08:00
|
|
|
id='admin.E108',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_list_display_links(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that list_display_links is a unique subset of list_display.
|
|
|
|
"""
|
2016-11-30 23:16:40 +08:00
|
|
|
from django.contrib.admin.options import ModelAdmin
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if obj.list_display_links is None:
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
2015-09-10 21:05:31 +08:00
|
|
|
elif not isinstance(obj.list_display_links, (list, tuple)):
|
|
|
|
return must_be('a list, a tuple, or None', option='list_display_links', obj=obj, id='admin.E110')
|
2016-11-30 23:16:40 +08:00
|
|
|
# Check only if ModelAdmin.get_list_display() isn't overridden.
|
2017-03-28 01:12:27 +08:00
|
|
|
elif obj.get_list_display.__func__ is ModelAdmin.get_list_display:
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_list_display_links_item(obj, field_name, "list_display_links[%d]" % index)
|
|
|
|
for index, field_name in enumerate(obj.list_display_links)
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2016-11-30 23:16:40 +08:00
|
|
|
return []
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_list_display_links_item(self, obj, field_name, label):
|
|
|
|
if field_name not in obj.list_display:
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of '%s' refers to '%s', which is not defined in 'list_display'." % (
|
2014-01-20 10:45:21 +08:00
|
|
|
label, field_name
|
|
|
|
),
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-03-03 13:27:17 +08:00
|
|
|
id='admin.E111',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_list_filter(self, obj):
|
|
|
|
if not isinstance(obj.list_filter, (list, tuple)):
|
|
|
|
return must_be('a list or tuple', option='list_filter', obj=obj, id='admin.E112')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_list_filter_item(obj, obj.model, item, "list_filter[%d]" % index)
|
|
|
|
for index, item in enumerate(obj.list_filter)
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_list_filter_item(self, obj, model, item, label):
|
2014-01-20 10:45:21 +08:00
|
|
|
"""
|
|
|
|
Check one item of `list_filter`, i.e. check if it is one of three options:
|
|
|
|
1. 'field' -- a basic field filter, possibly w/ relationships (e.g.
|
|
|
|
'field__rel')
|
|
|
|
2. ('field', SomeFieldListFilter) - a field-based list filter class
|
|
|
|
3. SomeListFilter - a non-field list filter class
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.contrib.admin import ListFilter, FieldListFilter
|
|
|
|
|
|
|
|
if callable(item) and not isinstance(item, models.Field):
|
|
|
|
# If item is option 3, it should be a ListFilter...
|
|
|
|
if not issubclass(item, ListFilter):
|
|
|
|
return must_inherit_from(parent='ListFilter', option=label,
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj, id='admin.E113')
|
2014-01-20 10:45:21 +08:00
|
|
|
# ... but not a FieldListFilter.
|
|
|
|
elif issubclass(item, FieldListFilter):
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of '%s' must not inherit from 'FieldListFilter'." % label,
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-03-03 13:27:17 +08:00
|
|
|
id='admin.E114',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
elif isinstance(item, (tuple, list)):
|
|
|
|
# item is option #2
|
|
|
|
field, list_filter_class = item
|
|
|
|
if not issubclass(list_filter_class, FieldListFilter):
|
2016-02-13 00:36:46 +08:00
|
|
|
return must_inherit_from(parent='FieldListFilter', option='%s[1]' % label, obj=obj, id='admin.E115')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
# item is option #1
|
|
|
|
field = item
|
|
|
|
|
|
|
|
# Validate the field string
|
|
|
|
try:
|
|
|
|
get_fields_from_path(model, field)
|
|
|
|
except (NotRelationField, FieldDoesNotExist):
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of '%s' refers to '%s', which does not refer to a Field." % (label, field),
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-03-03 13:27:17 +08:00
|
|
|
id='admin.E116',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_list_select_related(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that list_select_related is a boolean, a list or a tuple. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if not isinstance(obj.list_select_related, (bool, list, tuple)):
|
2016-02-13 00:36:46 +08:00
|
|
|
return must_be('a boolean, tuple or list', option='list_select_related', obj=obj, id='admin.E117')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_list_per_page(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that list_per_page is an integer. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if not isinstance(obj.list_per_page, int):
|
|
|
|
return must_be('an integer', option='list_per_page', obj=obj, id='admin.E118')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_list_max_show_all(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that list_max_show_all is an integer. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if not isinstance(obj.list_max_show_all, int):
|
|
|
|
return must_be('an integer', option='list_max_show_all', obj=obj, id='admin.E119')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_list_editable(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that list_editable is a sequence of editable fields from
|
|
|
|
list_display without first element. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if not isinstance(obj.list_editable, (list, tuple)):
|
|
|
|
return must_be('a list or tuple', option='list_editable', obj=obj, id='admin.E120')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2017-04-26 12:04:10 +08:00
|
|
|
return list(chain.from_iterable(
|
2015-09-10 21:05:31 +08:00
|
|
|
self._check_list_editable_item(obj, obj.model, item, "list_editable[%d]" % index)
|
|
|
|
for index, item in enumerate(obj.list_editable)
|
2017-04-26 12:04:10 +08:00
|
|
|
))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_list_editable_item(self, obj, model, field_name, label):
|
2014-01-20 10:45:21 +08:00
|
|
|
try:
|
2015-01-07 08:16:35 +08:00
|
|
|
field = model._meta.get_field(field_name)
|
2015-01-02 23:14:23 +08:00
|
|
|
except FieldDoesNotExist:
|
2016-02-13 00:36:46 +08:00
|
|
|
return refer_to_missing_field(field=field_name, option=label, model=model, obj=obj, id='admin.E121')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2015-09-10 21:05:31 +08:00
|
|
|
if field_name not in obj.list_display:
|
2015-08-13 16:03:20 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
|
|
|
"The value of '%s' refers to '%s', which is not "
|
|
|
|
"contained in 'list_display'." % (label, field_name),
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2015-08-13 16:03:20 +08:00
|
|
|
id='admin.E122',
|
|
|
|
)
|
|
|
|
]
|
2015-09-10 21:05:31 +08:00
|
|
|
elif obj.list_display_links and field_name in obj.list_display_links:
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of '%s' cannot be in both 'list_editable' and 'list_display_links'." % field_name,
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-03-03 13:27:17 +08:00
|
|
|
id='admin.E123',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
2016-02-29 19:12:25 +08:00
|
|
|
# If list_display[0] is in list_editable, check that
|
|
|
|
# list_display_links is set. See #22792 and #26229 for use cases.
|
2016-04-04 08:37:32 +08:00
|
|
|
elif (obj.list_display[0] == field_name and not obj.list_display_links and
|
|
|
|
obj.list_display_links is not None):
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of '%s' refers to the first field in 'list_display' ('%s'), "
|
|
|
|
"which cannot be used unless 'list_display_links' is set." % (
|
2015-09-10 21:05:31 +08:00
|
|
|
label, obj.list_display[0]
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-03-03 13:27:17 +08:00
|
|
|
id='admin.E124',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
elif not field.editable:
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of '%s' refers to '%s', which is not editable through the admin." % (
|
2014-01-20 10:45:21 +08:00
|
|
|
label, field_name
|
|
|
|
),
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-03-03 13:27:17 +08:00
|
|
|
id='admin.E125',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
2014-01-24 21:35:17 +08:00
|
|
|
else:
|
|
|
|
return []
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_search_fields(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check search_fields is a sequence. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if not isinstance(obj.search_fields, (list, tuple)):
|
|
|
|
return must_be('a list or tuple', option='search_fields', obj=obj, id='admin.E126')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_date_hierarchy(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that date_hierarchy refers to DateField or DateTimeField. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if obj.date_hierarchy is None:
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
|
|
|
else:
|
|
|
|
try:
|
2016-05-06 01:52:54 +08:00
|
|
|
field = get_fields_from_path(obj.model, obj.date_hierarchy)[-1]
|
|
|
|
except (NotRelationField, FieldDoesNotExist):
|
|
|
|
return [
|
|
|
|
checks.Error(
|
|
|
|
"The value of 'date_hierarchy' refers to '%s', which "
|
|
|
|
"does not refer to a Field." % obj.date_hierarchy,
|
|
|
|
obj=obj.__class__,
|
|
|
|
id='admin.E127',
|
|
|
|
)
|
|
|
|
]
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
if not isinstance(field, (models.DateField, models.DateTimeField)):
|
2016-02-13 00:36:46 +08:00
|
|
|
return must_be('a DateField or DateTimeField', option='date_hierarchy', obj=obj, id='admin.E128')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
class InlineModelAdminChecks(BaseModelAdminChecks):
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def check(self, inline_obj, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
errors = super().check(inline_obj)
|
2015-09-10 21:05:31 +08:00
|
|
|
parent_model = inline_obj.parent_model
|
|
|
|
errors.extend(self._check_relation(inline_obj, parent_model))
|
|
|
|
errors.extend(self._check_exclude_of_parent_model(inline_obj, parent_model))
|
|
|
|
errors.extend(self._check_extra(inline_obj))
|
|
|
|
errors.extend(self._check_max_num(inline_obj))
|
|
|
|
errors.extend(self._check_min_num(inline_obj))
|
|
|
|
errors.extend(self._check_formset(inline_obj))
|
2014-01-20 10:45:21 +08:00
|
|
|
return errors
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_exclude_of_parent_model(self, obj, parent_model):
|
2014-01-20 10:45:21 +08:00
|
|
|
# Do not perform more specific checks if the base checks result in an
|
|
|
|
# error.
|
2017-01-21 21:13:44 +08:00
|
|
|
errors = super()._check_exclude(obj)
|
2014-01-20 10:45:21 +08:00
|
|
|
if errors:
|
|
|
|
return []
|
|
|
|
|
|
|
|
# Skip if `fk_name` is invalid.
|
2015-09-10 21:05:31 +08:00
|
|
|
if self._check_relation(obj, parent_model):
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if obj.exclude is None:
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
fk = _get_foreign_key(parent_model, obj.model, fk_name=obj.fk_name)
|
|
|
|
if fk.name in obj.exclude:
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"Cannot exclude the field '%s', because it is the foreign key "
|
|
|
|
"to the parent model '%s.%s'." % (
|
2014-01-20 10:45:21 +08:00
|
|
|
fk.name, parent_model._meta.app_label, parent_model._meta.object_name
|
|
|
|
),
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E201',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_relation(self, obj, parent_model):
|
2014-01-20 10:45:21 +08:00
|
|
|
try:
|
2015-09-10 21:05:31 +08:00
|
|
|
_get_foreign_key(parent_model, obj.model, fk_name=obj.fk_name)
|
2014-01-20 10:45:21 +08:00
|
|
|
except ValueError as e:
|
2016-02-13 00:36:46 +08:00
|
|
|
return [checks.Error(e.args[0], obj=obj.__class__, id='admin.E202')]
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_extra(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that extra is an integer. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if not isinstance(obj.extra, int):
|
|
|
|
return must_be('an integer', option='extra', obj=obj, id='admin.E203')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_max_num(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check that max_num is an integer. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if obj.max_num is None:
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
2015-09-10 21:05:31 +08:00
|
|
|
elif not isinstance(obj.max_num, int):
|
|
|
|
return must_be('an integer', option='max_num', obj=obj, id='admin.E204')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_min_num(self, obj):
|
2014-03-06 04:19:40 +08:00
|
|
|
""" Check that min_num is an integer. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if obj.min_num is None:
|
2014-03-06 04:19:40 +08:00
|
|
|
return []
|
2015-09-10 21:05:31 +08:00
|
|
|
elif not isinstance(obj.min_num, int):
|
|
|
|
return must_be('an integer', option='min_num', obj=obj, id='admin.E205')
|
2014-03-06 04:19:40 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
def _check_formset(self, obj):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Check formset is a subclass of BaseModelFormSet. """
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
if not issubclass(obj.formset, BaseModelFormSet):
|
2016-02-13 00:36:46 +08:00
|
|
|
return must_inherit_from(parent='BaseModelFormSet', option='formset', obj=obj, id='admin.E206')
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
def must_be(type, option, obj, id):
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of '%s' must be %s." % (option, type),
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id=id,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def must_inherit_from(parent, option, obj, id):
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of '%s' must inherit from '%s'." % (option, parent),
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id=id,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def refer_to_missing_field(field, option, model, obj, id):
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of '%s' refers to '%s', which is not an attribute of '%s.%s'." % (
|
2014-01-20 10:45:21 +08:00
|
|
|
option, field, model._meta.app_label, model._meta.object_name
|
|
|
|
),
|
2015-09-10 21:05:31 +08:00
|
|
|
obj=obj.__class__,
|
2014-01-20 10:45:21 +08:00
|
|
|
id=id,
|
|
|
|
),
|
|
|
|
]
|