mirror of
https://github.com/django/django.git
synced 2025-07-24 13:44:32 +00:00
Fixed #16905 -- Added extensible checks (nee validation) framework
This is the result of Christopher Medrela's 2013 Summer of Code project. Thanks also to Preston Holmes, Tim Graham, Anssi Kääriäinen, Florian Apolloner, and Alex Gaynor for review notes along the way. Also: Fixes #8579, fixes #3055, fixes #19844.
This commit is contained in:
parent
6e7bd0b63b
commit
d818e0c9b2
101 changed files with 7058 additions and 1958 deletions
|
@ -3,6 +3,8 @@ import os
|
|||
|
||||
from django import forms
|
||||
from django.db.models.fields import Field
|
||||
from django.core import checks
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.core.files.base import File
|
||||
from django.core.files.storage import default_storage
|
||||
from django.core.files.images import ImageFile
|
||||
|
@ -223,9 +225,8 @@ class FileField(Field):
|
|||
description = _("File")
|
||||
|
||||
def __init__(self, verbose_name=None, name=None, upload_to='', storage=None, **kwargs):
|
||||
for arg in ('primary_key', 'unique'):
|
||||
if arg in kwargs:
|
||||
raise TypeError("'%s' is not a valid argument for %s." % (arg, self.__class__))
|
||||
self._primary_key_set_explicitly = 'primary_key' in kwargs
|
||||
self._unique_set_explicitly = 'unique' in kwargs
|
||||
|
||||
self.storage = storage or default_storage
|
||||
self.upload_to = upload_to
|
||||
|
@ -235,6 +236,52 @@ class FileField(Field):
|
|||
kwargs['max_length'] = kwargs.get('max_length', 100)
|
||||
super(FileField, self).__init__(verbose_name, name, **kwargs)
|
||||
|
||||
def check(self, **kwargs):
|
||||
errors = super(FileField, self).check(**kwargs)
|
||||
#errors.extend(self._check_upload_to())
|
||||
errors.extend(self._check_unique())
|
||||
errors.extend(self._check_primary_key())
|
||||
return errors
|
||||
|
||||
def _check_upload_to(self):
|
||||
if not self.upload_to:
|
||||
return [
|
||||
checks.Error(
|
||||
'The field requires an "upload_to" attribute.',
|
||||
hint=None,
|
||||
obj=self,
|
||||
id='E031',
|
||||
)
|
||||
]
|
||||
else:
|
||||
return []
|
||||
|
||||
def _check_unique(self):
|
||||
if self._unique_set_explicitly:
|
||||
return [
|
||||
checks.Error(
|
||||
'"unique" is not a valid argument for %s.' % self.__class__.__name__,
|
||||
hint=None,
|
||||
obj=self,
|
||||
id='E049',
|
||||
)
|
||||
]
|
||||
else:
|
||||
return []
|
||||
|
||||
def _check_primary_key(self):
|
||||
if self._primary_key_set_explicitly:
|
||||
return [
|
||||
checks.Error(
|
||||
'"primary_key" is not a valid argument for %s.' % self.__class__.__name__,
|
||||
hint=None,
|
||||
obj=self,
|
||||
id='E050',
|
||||
)
|
||||
]
|
||||
else:
|
||||
return []
|
||||
|
||||
def deconstruct(self):
|
||||
name, path, args, kwargs = super(FileField, self).deconstruct()
|
||||
if kwargs.get("max_length", None) != 100:
|
||||
|
@ -348,6 +395,27 @@ class ImageField(FileField):
|
|||
self.width_field, self.height_field = width_field, height_field
|
||||
super(ImageField, self).__init__(verbose_name, name, **kwargs)
|
||||
|
||||
def check(self, **kwargs):
|
||||
errors = super(ImageField, self).check(**kwargs)
|
||||
errors.extend(self._check_image_library_installed())
|
||||
return errors
|
||||
|
||||
def _check_image_library_installed(self):
|
||||
try:
|
||||
from django.utils.image import Image # NOQA
|
||||
except ImproperlyConfigured:
|
||||
return [
|
||||
checks.Error(
|
||||
'To use ImageFields, Pillow must be installed.',
|
||||
hint=('Get Pillow at https://pypi.python.org/pypi/Pillow '
|
||||
'or run command "pip install pillow".'),
|
||||
obj=self,
|
||||
id='E032',
|
||||
)
|
||||
]
|
||||
else:
|
||||
return []
|
||||
|
||||
def deconstruct(self):
|
||||
name, path, args, kwargs = super(ImageField, self).deconstruct()
|
||||
if self.width_field:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue