From 67a76500a56d6dbe970126e142e02254dad7dbf3 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sun, 18 Jan 2015 14:43:21 -0500 Subject: [PATCH] Removed support for admin validators per deprecation timeline; refs #16905. --- django/contrib/admin/options.py | 45 +-- django/contrib/admin/validation.py | 448 ----------------------------- tests/admin_checks/tests.py | 23 +- tests/admin_validation/__init__.py | 0 tests/admin_validation/models.py | 57 ---- tests/admin_validation/tests.py | 319 -------------------- tests/admin_views/tests.py | 19 -- tests/modeladmin/tests.py | 20 +- 8 files changed, 4 insertions(+), 927 deletions(-) delete mode 100644 django/contrib/admin/validation.py delete mode 100644 tests/admin_validation/__init__.py delete mode 100644 tests/admin_validation/models.py delete mode 100644 tests/admin_validation/tests.py diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 42685dc0ed..1419e86589 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -2,13 +2,11 @@ from collections import OrderedDict import copy import operator from functools import partial, reduce, update_wrapper -import warnings from django import forms from django.conf import settings from django.contrib import messages from django.contrib.admin import widgets, helpers -from django.contrib.admin import validation from django.contrib.admin.checks import (BaseModelAdminChecks, ModelAdminChecks, InlineModelAdminChecks) from django.contrib.admin.exceptions import DisallowedModelAdminToField @@ -18,9 +16,8 @@ from django.contrib.admin.utils import (quote, unquote, flatten_fieldsets, from django.contrib.admin.templatetags.admin_static import static from django.contrib.admin.templatetags.admin_urls import add_preserved_filters from django.contrib.auth import get_permission_codename -from django.core import checks from django.core.exceptions import (PermissionDenied, ValidationError, - FieldDoesNotExist, FieldError, ImproperlyConfigured) + FieldDoesNotExist, FieldError) from django.core.paginator import Paginator from django.core.urlresolvers import reverse from django.db import models, transaction, router @@ -38,7 +35,6 @@ from django.shortcuts import get_object_or_404 from django.template.response import SimpleTemplateResponse, TemplateResponse from django.utils import six from django.utils.decorators import method_decorator -from django.utils.deprecation import RemovedInDjango19Warning from django.utils.encoding import force_text, python_2_unicode_compatible from django.utils.html import escape, escapejs from django.utils.http import urlencode @@ -111,42 +107,11 @@ class BaseModelAdmin(six.with_metaclass(forms.MediaDefiningClass)): ordering = None view_on_site = True show_full_result_count = True - - # Validation of ModelAdmin definitions - # Old, deprecated style: - validator_class = None - default_validator_class = validation.BaseValidator - # New style: checks_class = BaseModelAdminChecks - @classmethod - def validate(cls, model): - warnings.warn( - 'ModelAdmin.validate() is deprecated. Use "check()" instead.', - RemovedInDjango19Warning) - if cls.validator_class: - validator = cls.validator_class() - else: - validator = cls.default_validator_class() - validator.validate(cls, model) - @classmethod def check(cls, model, **kwargs): - if cls.validator_class: - warnings.warn( - 'ModelAdmin.validator_class is deprecated. ' - 'ModelAdmin validators must be converted to use ' - 'the system check framework.', - RemovedInDjango19Warning) - validator = cls.validator_class() - try: - validator.validate(cls, model) - except ImproperlyConfigured as e: - return [checks.Error(e.args[0], hint=None, obj=cls)] - else: - return [] - else: - return cls.checks_class().check(cls, model, **kwargs) + return cls.checks_class().check(cls, model, **kwargs) def __init__(self): overrides = FORMFIELD_FOR_DBFIELD_DEFAULTS.copy() @@ -546,11 +511,6 @@ class ModelAdmin(BaseModelAdmin): actions_on_top = True actions_on_bottom = False actions_selection_counter = True - - # validation - # Old, deprecated style: - default_validator_class = validation.ModelAdminValidator - # New style: checks_class = ModelAdminChecks def __init__(self, model, admin_site): @@ -1763,7 +1723,6 @@ class InlineModelAdmin(BaseModelAdmin): verbose_name_plural = None can_delete = True show_change_link = False - checks_class = InlineModelAdminChecks def __init__(self, parent_model, admin_site): diff --git a/django/contrib/admin/validation.py b/django/contrib/admin/validation.py deleted file mode 100644 index 67b97f776e..0000000000 --- a/django/contrib/admin/validation.py +++ /dev/null @@ -1,448 +0,0 @@ -from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured -from django.db import models -from django.forms.models import BaseModelForm, BaseModelFormSet, _get_foreign_key -from django.contrib.admin.utils import get_fields_from_path, NotRelationField - -""" -Does basic ModelAdmin option validation. Calls custom validation -classmethod in the end if it is provided in cls. The signature of the -custom validation classmethod should be: def validate(cls, model). -""" - -__all__ = ['BaseValidator', 'InlineValidator'] - - -class BaseValidator(object): - - def validate(self, cls, model): - for m in dir(self): - if m.startswith('validate_'): - getattr(self, m)(cls, model) - - def check_field_spec(self, cls, model, flds, label): - """ - Validate the fields specification in `flds` from a ModelAdmin subclass - `cls` for the `model` model. Use `label` for reporting problems to the user. - - The fields specification can be a ``fields`` option or a ``fields`` - sub-option from a ``fieldsets`` option component. - """ - for fields in flds: - # The entry in fields might be a tuple. If it is a standalone - # field, make it into a tuple to make processing easier. - if type(fields) != tuple: - fields = (fields,) - for field in fields: - if field in cls.readonly_fields: - # 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. - continue - try: - f = model._meta.get_field(field) - except FieldDoesNotExist: - # If we can't find a field on the model that matches, it could be an - # extra field on the form; nothing to check so move on to the next field. - continue - if isinstance(f, models.ManyToManyField) and not f.rel.through._meta.auto_created: - raise ImproperlyConfigured("'%s.%s' " - "can't include the ManyToManyField field '%s' because " - "'%s' manually specifies a 'through' model." % ( - cls.__name__, label, field, field)) - - def validate_raw_id_fields(self, cls, model): - " Validate that raw_id_fields only contains field names that are listed on the model. " - if hasattr(cls, 'raw_id_fields'): - check_isseq(cls, 'raw_id_fields', cls.raw_id_fields) - for idx, field in enumerate(cls.raw_id_fields): - f = get_field(cls, model, 'raw_id_fields', field) - if not isinstance(f, (models.ForeignKey, models.ManyToManyField)): - raise ImproperlyConfigured("'%s.raw_id_fields[%d]', '%s' must " - "be either a ForeignKey or ManyToManyField." - % (cls.__name__, idx, field)) - - def validate_fields(self, cls, model): - " Validate that fields only refer to existing fields, doesn't contain duplicates. " - # fields - if cls.fields: # default value is None - check_isseq(cls, 'fields', cls.fields) - self.check_field_spec(cls, model, cls.fields, 'fields') - if cls.fieldsets: - raise ImproperlyConfigured('Both fieldsets and fields are specified in %s.' % cls.__name__) - if len(cls.fields) > len(set(cls.fields)): - raise ImproperlyConfigured('There are duplicate field(s) in %s.fields' % cls.__name__) - - def validate_fieldsets(self, cls, model): - " Validate that fieldsets is properly formatted and doesn't contain duplicates. " - from django.contrib.admin.options import flatten_fieldsets - if cls.fieldsets: # default value is None - check_isseq(cls, 'fieldsets', cls.fieldsets) - for idx, fieldset in enumerate(cls.fieldsets): - check_isseq(cls, 'fieldsets[%d]' % idx, fieldset) - if len(fieldset) != 2: - raise ImproperlyConfigured("'%s.fieldsets[%d]' does not " - "have exactly two elements." % (cls.__name__, idx)) - check_isdict(cls, 'fieldsets[%d][1]' % idx, fieldset[1]) - if 'fields' not in fieldset[1]: - raise ImproperlyConfigured("'fields' key is required in " - "%s.fieldsets[%d][1] field options dict." - % (cls.__name__, idx)) - self.check_field_spec(cls, model, fieldset[1]['fields'], "fieldsets[%d][1]['fields']" % idx) - flattened_fieldsets = flatten_fieldsets(cls.fieldsets) - if len(flattened_fieldsets) > len(set(flattened_fieldsets)): - raise ImproperlyConfigured('There are duplicate field(s) in %s.fieldsets' % cls.__name__) - - def validate_exclude(self, cls, model): - " Validate that exclude is a sequence without duplicates. " - if cls.exclude: # default value is None - check_isseq(cls, 'exclude', cls.exclude) - if len(cls.exclude) > len(set(cls.exclude)): - raise ImproperlyConfigured('There are duplicate field(s) in %s.exclude' % cls.__name__) - - def validate_form(self, cls, model): - " Validate that form subclasses BaseModelForm. " - if hasattr(cls, 'form') and not issubclass(cls.form, BaseModelForm): - raise ImproperlyConfigured("%s.form does not inherit from " - "BaseModelForm." % cls.__name__) - - def validate_filter_vertical(self, cls, model): - " Validate that filter_vertical is a sequence of field names. " - if hasattr(cls, 'filter_vertical'): - check_isseq(cls, 'filter_vertical', cls.filter_vertical) - for idx, field in enumerate(cls.filter_vertical): - f = get_field(cls, model, 'filter_vertical', field) - if not isinstance(f, models.ManyToManyField): - raise ImproperlyConfigured("'%s.filter_vertical[%d]' must be " - "a ManyToManyField." % (cls.__name__, idx)) - - def validate_filter_horizontal(self, cls, model): - " Validate that filter_horizontal is a sequence of field names. " - if hasattr(cls, 'filter_horizontal'): - check_isseq(cls, 'filter_horizontal', cls.filter_horizontal) - for idx, field in enumerate(cls.filter_horizontal): - f = get_field(cls, model, 'filter_horizontal', field) - if not isinstance(f, models.ManyToManyField): - raise ImproperlyConfigured("'%s.filter_horizontal[%d]' must be " - "a ManyToManyField." % (cls.__name__, idx)) - - def validate_radio_fields(self, cls, model): - " Validate that radio_fields is a dictionary of choice or foreign key fields. " - from django.contrib.admin.options import HORIZONTAL, VERTICAL - if hasattr(cls, 'radio_fields'): - check_isdict(cls, 'radio_fields', cls.radio_fields) - for field, val in cls.radio_fields.items(): - f = get_field(cls, model, 'radio_fields', field) - if not (isinstance(f, models.ForeignKey) or f.choices): - raise ImproperlyConfigured("'%s.radio_fields['%s']' " - "is neither an instance of ForeignKey nor does " - "have choices set." % (cls.__name__, field)) - if val not in (HORIZONTAL, VERTICAL): - raise ImproperlyConfigured("'%s.radio_fields['%s']' " - "is neither admin.HORIZONTAL nor admin.VERTICAL." - % (cls.__name__, field)) - - def validate_prepopulated_fields(self, cls, model): - " Validate that prepopulated_fields if a dictionary containing allowed field types. " - # prepopulated_fields - if hasattr(cls, 'prepopulated_fields'): - check_isdict(cls, 'prepopulated_fields', cls.prepopulated_fields) - for field, val in cls.prepopulated_fields.items(): - f = get_field(cls, model, 'prepopulated_fields', field) - if isinstance(f, (models.DateTimeField, models.ForeignKey, - models.ManyToManyField)): - raise ImproperlyConfigured("'%s.prepopulated_fields['%s']' " - "is either a DateTimeField, ForeignKey or " - "ManyToManyField. This isn't allowed." - % (cls.__name__, field)) - check_isseq(cls, "prepopulated_fields['%s']" % field, val) - for idx, f in enumerate(val): - get_field(cls, model, "prepopulated_fields['%s'][%d]" % (field, idx), f) - - def validate_view_on_site_url(self, cls, model): - if hasattr(cls, 'view_on_site'): - if not callable(cls.view_on_site) and not isinstance(cls.view_on_site, bool): - raise ImproperlyConfigured("%s.view_on_site is not a callable or a boolean value." % cls.__name__) - - def validate_ordering(self, cls, model): - " Validate that ordering refers to existing fields or is random. " - # ordering = None - if cls.ordering: - check_isseq(cls, 'ordering', cls.ordering) - for idx, field in enumerate(cls.ordering): - if field == '?' and len(cls.ordering) != 1: - raise ImproperlyConfigured("'%s.ordering' has the random " - "ordering marker '?', but contains other fields as " - "well. Please either remove '?' or the other fields." - % cls.__name__) - if field == '?': - continue - if field.startswith('-'): - field = field[1:] - # Skip ordering in the format field1__field2 (FIXME: checking - # this format would be nice, but it's a little fiddly). - if '__' in field: - continue - get_field(cls, model, 'ordering[%d]' % idx, field) - - def validate_readonly_fields(self, cls, model): - " Validate that readonly_fields refers to proper attribute or field. " - if hasattr(cls, "readonly_fields"): - check_isseq(cls, "readonly_fields", cls.readonly_fields) - for idx, field in enumerate(cls.readonly_fields): - if not callable(field): - if not hasattr(cls, field): - if not hasattr(model, field): - try: - model._meta.get_field(field) - except FieldDoesNotExist: - raise ImproperlyConfigured( - "%s.readonly_fields[%d], %r is not a callable or " - "an attribute of %r or found in the model %r." - % (cls.__name__, idx, field, cls.__name__, model._meta.object_name) - ) - - -class ModelAdminValidator(BaseValidator): - def validate_save_as(self, cls, model): - " Validate save_as is a boolean. " - check_type(cls, 'save_as', bool) - - def validate_save_on_top(self, cls, model): - " Validate save_on_top is a boolean. " - check_type(cls, 'save_on_top', bool) - - def validate_inlines(self, cls, model): - " Validate inline model admin classes. " - from django.contrib.admin.options import BaseModelAdmin - if hasattr(cls, 'inlines'): - check_isseq(cls, 'inlines', cls.inlines) - for idx, inline in enumerate(cls.inlines): - if not issubclass(inline, BaseModelAdmin): - raise ImproperlyConfigured("'%s.inlines[%d]' does not inherit " - "from BaseModelAdmin." % (cls.__name__, idx)) - if not inline.model: - raise ImproperlyConfigured("'model' is a required attribute " - "of '%s.inlines[%d]'." % (cls.__name__, idx)) - if not issubclass(inline.model, models.Model): - raise ImproperlyConfigured("'%s.inlines[%d].model' does not " - "inherit from models.Model." % (cls.__name__, idx)) - inline.validate(inline.model) - self.check_inline(inline, model) - - def check_inline(self, cls, parent_model): - " Validate inline class's fk field is not excluded. " - fk = _get_foreign_key(parent_model, cls.model, fk_name=cls.fk_name, can_fail=True) - if hasattr(cls, 'exclude') and cls.exclude: - if fk and fk.name in cls.exclude: - raise ImproperlyConfigured("%s cannot exclude the field " - "'%s' - this is the foreign key to the parent model " - "%s.%s." % (cls.__name__, fk.name, parent_model._meta.app_label, parent_model.__name__)) - - def validate_list_display(self, cls, model): - " Validate that list_display only contains fields or usable attributes. " - if hasattr(cls, 'list_display'): - check_isseq(cls, 'list_display', cls.list_display) - for idx, field in enumerate(cls.list_display): - if not callable(field): - if not hasattr(cls, field): - if not hasattr(model, field): - try: - model._meta.get_field(field) - except FieldDoesNotExist: - raise ImproperlyConfigured( - "%s.list_display[%d], %r is not a callable or " - "an attribute of %r or found in the model %r." - % (cls.__name__, idx, field, cls.__name__, model._meta.object_name) - ) - else: - # getattr(model, field) could be an X_RelatedObjectsDescriptor - f = fetch_attr(cls, model, "list_display[%d]" % idx, field) - if isinstance(f, models.ManyToManyField): - raise ImproperlyConfigured( - "'%s.list_display[%d]', '%s' is a ManyToManyField " - "which is not supported." - % (cls.__name__, idx, field) - ) - - def validate_list_display_links(self, cls, model): - " Validate that list_display_links either is None or a unique subset of list_display." - if hasattr(cls, 'list_display_links'): - if cls.list_display_links is None: - return - check_isseq(cls, 'list_display_links', cls.list_display_links) - for idx, field in enumerate(cls.list_display_links): - if field not in cls.list_display: - raise ImproperlyConfigured("'%s.list_display_links[%d]' " - "refers to '%s' which is not defined in 'list_display'." - % (cls.__name__, idx, field)) - - def validate_list_filter(self, cls, model): - """ - Validate that list_filter is a sequence of one of three options: - 1: 'field' - a basic field filter, possibly w/ relationships (eg, '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 hasattr(cls, 'list_filter'): - check_isseq(cls, 'list_filter', cls.list_filter) - for idx, item in enumerate(cls.list_filter): - if callable(item) and not isinstance(item, models.Field): - # If item is option 3, it should be a ListFilter... - if not issubclass(item, ListFilter): - raise ImproperlyConfigured("'%s.list_filter[%d]' is '%s'" - " which is not a descendant of ListFilter." - % (cls.__name__, idx, item.__name__)) - # ... but not a FieldListFilter. - if issubclass(item, FieldListFilter): - raise ImproperlyConfigured("'%s.list_filter[%d]' is '%s'" - " which is of type FieldListFilter but is not" - " associated with a field name." - % (cls.__name__, idx, item.__name__)) - else: - if isinstance(item, (tuple, list)): - # item is option #2 - field, list_filter_class = item - if not issubclass(list_filter_class, FieldListFilter): - raise ImproperlyConfigured("'%s.list_filter[%d][1]'" - " is '%s' which is not of type FieldListFilter." - % (cls.__name__, idx, list_filter_class.__name__)) - else: - # item is option #1 - field = item - # Validate the field string - try: - get_fields_from_path(model, field) - except (NotRelationField, FieldDoesNotExist): - raise ImproperlyConfigured("'%s.list_filter[%d]' refers to '%s'" - " which does not refer to a Field." - % (cls.__name__, idx, field)) - - def validate_list_select_related(self, cls, model): - " Validate that list_select_related is a boolean, a list or a tuple. " - list_select_related = getattr(cls, 'list_select_related', None) - if list_select_related: - types = (bool, tuple, list) - if not isinstance(list_select_related, types): - raise ImproperlyConfigured("'%s.list_select_related' should be " - "either a bool, a tuple or a list" % - cls.__name__) - - def validate_list_per_page(self, cls, model): - " Validate that list_per_page is an integer. " - check_type(cls, 'list_per_page', int) - - def validate_list_max_show_all(self, cls, model): - " Validate that list_max_show_all is an integer. " - check_type(cls, 'list_max_show_all', int) - - def validate_list_editable(self, cls, model): - """ - Validate that list_editable is a sequence of editable fields from - list_display without first element. - """ - if hasattr(cls, 'list_editable') and cls.list_editable: - check_isseq(cls, 'list_editable', cls.list_editable) - for idx, field_name in enumerate(cls.list_editable): - try: - field = model._meta.get_field(field_name) - except FieldDoesNotExist: - raise ImproperlyConfigured("'%s.list_editable[%d]' refers to a " - "field, '%s', not defined on %s.%s." - % (cls.__name__, idx, field_name, model._meta.app_label, model.__name__)) - if field_name not in cls.list_display: - raise ImproperlyConfigured("'%s.list_editable[%d]' refers to " - "'%s' which is not defined in 'list_display'." - % (cls.__name__, idx, field_name)) - if cls.list_display_links is not None: - if field_name in cls.list_display_links: - raise ImproperlyConfigured("'%s' cannot be in both '%s.list_editable'" - " and '%s.list_display_links'" - % (field_name, cls.__name__, cls.__name__)) - if not cls.list_display_links and cls.list_display[0] in cls.list_editable: - raise ImproperlyConfigured("'%s.list_editable[%d]' refers to" - " the first field in list_display, '%s', which can't be" - " used unless list_display_links is set." - % (cls.__name__, idx, cls.list_display[0])) - if not field.editable: - raise ImproperlyConfigured("'%s.list_editable[%d]' refers to a " - "field, '%s', which isn't editable through the admin." - % (cls.__name__, idx, field_name)) - - def validate_search_fields(self, cls, model): - " Validate search_fields is a sequence. " - if hasattr(cls, 'search_fields'): - check_isseq(cls, 'search_fields', cls.search_fields) - - def validate_date_hierarchy(self, cls, model): - " Validate that date_hierarchy refers to DateField or DateTimeField. " - if cls.date_hierarchy: - f = get_field(cls, model, 'date_hierarchy', cls.date_hierarchy) - if not isinstance(f, (models.DateField, models.DateTimeField)): - raise ImproperlyConfigured("'%s.date_hierarchy is " - "neither an instance of DateField nor DateTimeField." - % cls.__name__) - - -class InlineValidator(BaseValidator): - def validate_fk_name(self, cls, model): - " Validate that fk_name refers to a ForeignKey. " - if cls.fk_name: # default value is None - f = get_field(cls, model, 'fk_name', cls.fk_name) - if not isinstance(f, models.ForeignKey): - raise ImproperlyConfigured("'%s.fk_name is not an instance of " - "models.ForeignKey." % cls.__name__) - - def validate_extra(self, cls, model): - " Validate that extra is an integer. " - check_type(cls, 'extra', int) - - def validate_max_num(self, cls, model): - " Validate that max_num is an integer. " - check_type(cls, 'max_num', int) - - def validate_formset(self, cls, model): - " Validate formset is a subclass of BaseModelFormSet. " - if hasattr(cls, 'formset') and not issubclass(cls.formset, BaseModelFormSet): - raise ImproperlyConfigured("'%s.formset' does not inherit from " - "BaseModelFormSet." % cls.__name__) - - -def check_type(cls, attr, type_): - if getattr(cls, attr, None) is not None and not isinstance(getattr(cls, attr), type_): - raise ImproperlyConfigured("'%s.%s' should be a %s." - % (cls.__name__, attr, type_.__name__)) - - -def check_isseq(cls, label, obj): - if not isinstance(obj, (list, tuple)): - raise ImproperlyConfigured("'%s.%s' must be a list or tuple." % (cls.__name__, label)) - - -def check_isdict(cls, label, obj): - if not isinstance(obj, dict): - raise ImproperlyConfigured("'%s.%s' must be a dictionary." % (cls.__name__, label)) - - -def get_field(cls, model, label, field): - try: - return model._meta.get_field(field) - except FieldDoesNotExist: - raise ImproperlyConfigured("'%s.%s' refers to field '%s' that is missing from model '%s.%s'." - % (cls.__name__, label, field, model._meta.app_label, model.__name__)) - - -def fetch_attr(cls, model, label, field): - try: - return model._meta.get_field(field) - except FieldDoesNotExist: - pass - try: - return getattr(model, field) - except AttributeError: - raise ImproperlyConfigured( - "'%s.%s' refers to '%s' that is neither a field, method or " - "property of model '%s.%s'." - % (cls.__name__, label, field, model._meta.app_label, model.__name__) - ) diff --git a/tests/admin_checks/tests.py b/tests/admin_checks/tests.py index ea0151cced..4f5bd5e48a 100644 --- a/tests/admin_checks/tests.py +++ b/tests/admin_checks/tests.py @@ -4,8 +4,7 @@ from django import forms from django.contrib import admin from django.contrib.contenttypes.admin import GenericStackedInline from django.core import checks -from django.core.exceptions import ImproperlyConfigured -from django.test import TestCase, ignore_warnings, override_settings +from django.test import TestCase, override_settings from .models import Song, Book, Album, TwoAlbumFKAndAnE, City, State, Influence @@ -635,26 +634,6 @@ class SystemChecksTestCase(TestCase): errors = FieldsOnFormOnlyAdmin.check(model=Song) self.assertEqual(errors, []) - @ignore_warnings(module='django.contrib.admin.options') - def test_validator_compatibility(self): - class MyValidator(object): - def validate(self, cls, model): - raise ImproperlyConfigured("error!") - - class MyModelAdmin(admin.ModelAdmin): - validator_class = MyValidator - - errors = MyModelAdmin.check(model=Song) - - expected = [ - checks.Error( - 'error!', - hint=None, - obj=MyModelAdmin, - ) - ] - self.assertEqual(errors, expected) - def test_check_sublists_for_duplicates(self): class MyModelAdmin(admin.ModelAdmin): fields = ['state', ['state']] diff --git a/tests/admin_validation/__init__.py b/tests/admin_validation/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/tests/admin_validation/models.py b/tests/admin_validation/models.py deleted file mode 100644 index d23849a2a8..0000000000 --- a/tests/admin_validation/models.py +++ /dev/null @@ -1,57 +0,0 @@ -""" -Tests of ModelAdmin validation logic. -""" - -from django.db import models -from django.utils.encoding import python_2_unicode_compatible - - -class Album(models.Model): - title = models.CharField(max_length=150) - - -@python_2_unicode_compatible -class Song(models.Model): - title = models.CharField(max_length=150) - album = models.ForeignKey(Album) - original_release = models.DateField(editable=False) - - class Meta: - ordering = ('title',) - - def __str__(self): - return self.title - - def readonly_method_on_model(self): - # does nothing - pass - - -class TwoAlbumFKAndAnE(models.Model): - album1 = models.ForeignKey(Album, related_name="album1_set") - album2 = models.ForeignKey(Album, related_name="album2_set") - e = models.CharField(max_length=1) - - -class Author(models.Model): - name = models.CharField(max_length=100) - - -class Book(models.Model): - name = models.CharField(max_length=100) - subtitle = models.CharField(max_length=100) - price = models.FloatField() - authors = models.ManyToManyField(Author, through='AuthorsBooks') - - -class AuthorsBooks(models.Model): - author = models.ForeignKey(Author) - book = models.ForeignKey(Book) - - -class State(models.Model): - name = models.CharField(max_length=15) - - -class City(models.Model): - state = models.ForeignKey(State) diff --git a/tests/admin_validation/tests.py b/tests/admin_validation/tests.py deleted file mode 100644 index 321e10428c..0000000000 --- a/tests/admin_validation/tests.py +++ /dev/null @@ -1,319 +0,0 @@ -from __future__ import unicode_literals - -from django import forms -from django.contrib import admin -from django.core.exceptions import ImproperlyConfigured -from django.test import TestCase, ignore_warnings -from django.test.utils import str_prefix -from django.utils.deprecation import RemovedInDjango19Warning - -from .models import Song, Book, Album, TwoAlbumFKAndAnE, City - - -class SongForm(forms.ModelForm): - pass - - -class ValidFields(admin.ModelAdmin): - form = SongForm - fields = ['title'] - - -class ValidFormFieldsets(admin.ModelAdmin): - def get_form(self, request, obj=None, **kwargs): - class ExtraFieldForm(SongForm): - name = forms.CharField(max_length=50) - return ExtraFieldForm - - fieldsets = ( - (None, { - 'fields': ('name',), - }), - ) - - -@ignore_warnings(category=RemovedInDjango19Warning) -class ValidationTestCase(TestCase): - - def test_readonly_and_editable(self): - class SongAdmin(admin.ModelAdmin): - readonly_fields = ["original_release"] - fieldsets = [ - (None, { - "fields": ["title", "original_release"], - }), - ] - - SongAdmin.validate(Song) - - def test_custom_modelforms_with_fields_fieldsets(self): - """ - # Regression test for #8027: custom ModelForms with fields/fieldsets - """ - ValidFields.validate(Song) - - def test_custom_get_form_with_fieldsets(self): - """ - Ensure that the fieldsets validation is skipped when the ModelAdmin.get_form() method - is overridden. - Refs #19445. - """ - ValidFormFieldsets.validate(Song) - - def test_exclude_values(self): - """ - Tests for basic validation of 'exclude' option values (#12689) - """ - class ExcludedFields1(admin.ModelAdmin): - exclude = ('foo') - - self.assertRaisesMessage(ImproperlyConfigured, - "'ExcludedFields1.exclude' must be a list or tuple.", - ExcludedFields1.validate, - Book) - - def test_exclude_duplicate_values(self): - class ExcludedFields2(admin.ModelAdmin): - exclude = ('name', 'name') - - self.assertRaisesMessage(ImproperlyConfigured, - "There are duplicate field(s) in ExcludedFields2.exclude", - ExcludedFields2.validate, - Book) - - def test_exclude_in_inline(self): - class ExcludedFieldsInline(admin.TabularInline): - model = Song - exclude = ('foo') - - class ExcludedFieldsAlbumAdmin(admin.ModelAdmin): - model = Album - inlines = [ExcludedFieldsInline] - - self.assertRaisesMessage(ImproperlyConfigured, - "'ExcludedFieldsInline.exclude' must be a list or tuple.", - ExcludedFieldsAlbumAdmin.validate, - Album) - - def test_exclude_inline_model_admin(self): - """ - # Regression test for #9932 - exclude in InlineModelAdmin - # should not contain the ForeignKey field used in ModelAdmin.model - """ - class SongInline(admin.StackedInline): - model = Song - exclude = ['album'] - - class AlbumAdmin(admin.ModelAdmin): - model = Album - inlines = [SongInline] - - self.assertRaisesMessage(ImproperlyConfigured, - "SongInline cannot exclude the field 'album' - this is the foreign key to the parent model admin_validation.Album.", - AlbumAdmin.validate, - Album) - - def test_app_label_in_admin_validation(self): - """ - Regression test for #15669 - Include app label in admin validation messages - """ - class RawIdNonexistingAdmin(admin.ModelAdmin): - raw_id_fields = ('nonexisting',) - - self.assertRaisesMessage(ImproperlyConfigured, - "'RawIdNonexistingAdmin.raw_id_fields' refers to field 'nonexisting' that is missing from model 'admin_validation.Album'.", - RawIdNonexistingAdmin.validate, - Album) - - def test_fk_exclusion(self): - """ - Regression test for #11709 - when testing for fk excluding (when exclude is - given) make sure fk_name is honored or things blow up when there is more - than one fk to the parent model. - """ - class TwoAlbumFKAndAnEInline(admin.TabularInline): - model = TwoAlbumFKAndAnE - exclude = ("e",) - fk_name = "album1" - - class MyAdmin(admin.ModelAdmin): - inlines = [TwoAlbumFKAndAnEInline] - - MyAdmin.validate(Album) - - def test_inline_self_validation(self): - class TwoAlbumFKAndAnEInline(admin.TabularInline): - model = TwoAlbumFKAndAnE - - class MyAdmin(admin.ModelAdmin): - inlines = [TwoAlbumFKAndAnEInline] - - self.assertRaisesMessage(ValueError, - "'admin_validation.TwoAlbumFKAndAnE' has more than one ForeignKey to 'admin_validation.Album'.", - MyAdmin.validate, Album) - - def test_inline_with_specified(self): - class TwoAlbumFKAndAnEInline(admin.TabularInline): - model = TwoAlbumFKAndAnE - fk_name = "album1" - - class MyAdmin(admin.ModelAdmin): - inlines = [TwoAlbumFKAndAnEInline] - - MyAdmin.validate(Album) - - def test_readonly(self): - class SongAdmin(admin.ModelAdmin): - readonly_fields = ("title",) - - SongAdmin.validate(Song) - - def test_readonly_on_method(self): - def my_function(obj): - pass - - class SongAdmin(admin.ModelAdmin): - readonly_fields = (my_function,) - - SongAdmin.validate(Song) - - def test_readonly_on_modeladmin(self): - class SongAdmin(admin.ModelAdmin): - readonly_fields = ("readonly_method_on_modeladmin",) - - def readonly_method_on_modeladmin(self, obj): - pass - - SongAdmin.validate(Song) - - def test_readonly_method_on_model(self): - class SongAdmin(admin.ModelAdmin): - readonly_fields = ("readonly_method_on_model",) - - SongAdmin.validate(Song) - - def test_nonexistent_field(self): - class SongAdmin(admin.ModelAdmin): - readonly_fields = ("title", "nonexistent") - - self.assertRaisesMessage(ImproperlyConfigured, - str_prefix("SongAdmin.readonly_fields[1], %(_)s'nonexistent' is not a callable " - "or an attribute of 'SongAdmin' or found in the model 'Song'."), - SongAdmin.validate, - Song) - - def test_nonexistent_field_on_inline(self): - class CityInline(admin.TabularInline): - model = City - readonly_fields = ['i_dont_exist'] # Missing attribute - - self.assertRaisesMessage(ImproperlyConfigured, - str_prefix("CityInline.readonly_fields[0], %(_)s'i_dont_exist' is not a callable " - "or an attribute of 'CityInline' or found in the model 'City'."), - CityInline.validate, - City) - - def test_extra(self): - class SongAdmin(admin.ModelAdmin): - def awesome_song(self, instance): - if instance.title == "Born to Run": - return "Best Ever!" - return "Status unknown." - - SongAdmin.validate(Song) - - def test_readonly_lambda(self): - class SongAdmin(admin.ModelAdmin): - readonly_fields = (lambda obj: "test",) - - SongAdmin.validate(Song) - - def test_graceful_m2m_fail(self): - """ - Regression test for #12203/#12237 - Fail more gracefully when a M2M field that - specifies the 'through' option is included in the 'fields' or the 'fieldsets' - ModelAdmin options. - """ - - class BookAdmin(admin.ModelAdmin): - fields = ['authors'] - - self.assertRaisesMessage(ImproperlyConfigured, - "'BookAdmin.fields' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.", - BookAdmin.validate, - Book) - - def test_cannot_include_through(self): - class FieldsetBookAdmin(admin.ModelAdmin): - fieldsets = ( - ('Header 1', {'fields': ('name',)}), - ('Header 2', {'fields': ('authors',)}), - ) - - self.assertRaisesMessage(ImproperlyConfigured, - "'FieldsetBookAdmin.fieldsets[1][1]['fields']' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.", - FieldsetBookAdmin.validate, - Book) - - def test_nested_fields(self): - class NestedFieldsAdmin(admin.ModelAdmin): - fields = ('price', ('name', 'subtitle')) - - NestedFieldsAdmin.validate(Book) - - def test_nested_fieldsets(self): - class NestedFieldsetAdmin(admin.ModelAdmin): - fieldsets = ( - ('Main', {'fields': ('price', ('name', 'subtitle'))}), - ) - - NestedFieldsetAdmin.validate(Book) - - def test_explicit_through_override(self): - """ - Regression test for #12209 -- If the explicitly provided through model - is specified as a string, the admin should still be able use - Model.m2m_field.through - """ - - class AuthorsInline(admin.TabularInline): - model = Book.authors.through - - class BookAdmin(admin.ModelAdmin): - inlines = [AuthorsInline] - - # If the through model is still a string (and hasn't been resolved to a model) - # the validation will fail. - BookAdmin.validate(Book) - - def test_non_model_fields(self): - """ - Regression for ensuring ModelAdmin.fields can contain non-model fields - that broke with r11737 - """ - class SongForm(forms.ModelForm): - extra_data = forms.CharField() - - class FieldsOnFormOnlyAdmin(admin.ModelAdmin): - form = SongForm - fields = ['title', 'extra_data'] - - FieldsOnFormOnlyAdmin.validate(Song) - - def test_non_model_first_field(self): - """ - Regression for ensuring ModelAdmin.field can handle first elem being a - non-model field (test fix for UnboundLocalError introduced with r16225). - """ - class SongForm(forms.ModelForm): - extra_data = forms.CharField() - - class Meta: - model = Song - fields = '__all__' - - class FieldsOnFormOnlyAdmin(admin.ModelAdmin): - form = SongForm - fields = ['extra_data', 'title'] - - FieldsOnFormOnlyAdmin.validate(Song) diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index c41a348078..486680fda7 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -9,7 +9,6 @@ import unittest from django.core import mail from django.core.checks import Error from django.core.files import temp as tempfile -from django.core.exceptions import ImproperlyConfigured from django.core.urlresolvers import (NoReverseMatch, get_script_prefix, resolve, reverse, set_script_prefix) # Register auth models with the admin. @@ -22,7 +21,6 @@ from django.contrib.admin.templatetags.admin_static import static from django.contrib.admin.templatetags.admin_urls import add_preserved_filters from django.contrib.admin.tests import AdminSeleniumWebDriverTestCase from django.contrib.admin.utils import quote -from django.contrib.admin.validation import ModelAdminValidator from django.contrib.admin.views.main import IS_POPUP_VAR from django.contrib.auth import REDIRECT_FIELD_NAME from django.contrib.auth.models import Group, User, Permission @@ -5272,23 +5270,6 @@ class InlineAdminViewOnSiteTest(TestCase): ) -class AdminGenericRelationTests(TestCase): - def test_generic_relation_fk_list_filter(self): - """ - Validates a model with a generic relation to a model with - a foreign key can specify the generic+fk relationship - path as a list_filter. See trac #21428. - """ - class GenericFKAdmin(ModelAdmin): - list_filter = ('tags__content_type',) - - validator = ModelAdminValidator() - try: - validator.validate_list_filter(GenericFKAdmin, Plot) - except ImproperlyConfigured: - self.fail("Couldn't validate a GenericRelation -> FK path in ModelAdmin.list_filter") - - @override_settings(ROOT_URLCONF="admin_views.urls") class TestEtagWithAdminView(TestCase): # See https://code.djangoproject.com/ticket/16003 diff --git a/tests/modeladmin/tests.py b/tests/modeladmin/tests.py index ee8c26c3ca..6157d6c1d4 100644 --- a/tests/modeladmin/tests.py +++ b/tests/modeladmin/tests.py @@ -7,16 +7,13 @@ from django.contrib.admin.options import (ModelAdmin, TabularInline, HORIZONTAL, VERTICAL) from django.contrib.admin.sites import AdminSite from django.contrib.admin.widgets import AdminDateWidget, AdminRadioSelect -from django.contrib.admin.validation import ModelAdminValidator from django.contrib.admin import (SimpleListFilter, BooleanFieldListFilter) from django.core.checks import Error -from django.core.exceptions import ImproperlyConfigured from django.forms.models import BaseModelFormSet from django.forms.widgets import Select -from django.test import TestCase, ignore_warnings +from django.test import TestCase from django.utils import six -from django.utils.deprecation import RemovedInDjango19Warning from .models import Band, Concert, ValidationTestModel, ValidationTestInlineModel @@ -1503,21 +1500,6 @@ class FormsetCheckTests(CheckTestCase): self.assertIsValid(ValidationTestModelAdmin, ValidationTestModel) -class CustomModelAdminTests(CheckTestCase): - @ignore_warnings(category=RemovedInDjango19Warning) - def test_deprecation(self): - "Deprecated Custom Validator definitions still work with the check framework." - - class CustomValidator(ModelAdminValidator): - def validate_me(self, model_admin, model): - raise ImproperlyConfigured('error!') - - class CustomModelAdmin(ModelAdmin): - validator_class = CustomValidator - - self.assertIsInvalid(CustomModelAdmin, ValidationTestModel, 'error!') - - class ListDisplayEditableTests(CheckTestCase): def test_list_display_links_is_none(self): """