1
0
mirror of https://github.com/django/django.git synced 2025-07-07 19:29:12 +00:00

[soc2009/model-validation] FileFields migrated

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@10908 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Honza Král 2009-06-03 02:38:23 +00:00
parent 92934650f6
commit a9efb44c01

View File

@ -472,12 +472,9 @@ class FileField(Field):
self.max_length = kwargs.pop('max_length', None) self.max_length = kwargs.pop('max_length', None)
super(FileField, self).__init__(*args, **kwargs) super(FileField, self).__init__(*args, **kwargs)
def clean(self, data, initial=None): def to_python(self, data):
super(FileField, self).clean(initial or data) if data in EMPTY_VALUES:
if not self.required and data in EMPTY_VALUES:
return None return None
elif not data and initial:
return initial
# UploadedFile objects should have name and size attributes. # UploadedFile objects should have name and size attributes.
try: try:
@ -496,21 +493,25 @@ class FileField(Field):
return data return data
def clean(self, data, initial=None):
if not data and initial:
return initial
return super(FileField, self).clean(data)
class ImageField(FileField): class ImageField(FileField):
default_error_messages = { default_error_messages = {
'invalid_image': _(u"Upload a valid image. The file you uploaded was either not an image or a corrupted image."), 'invalid_image': _(u"Upload a valid image. The file you uploaded was either not an image or a corrupted image."),
} }
def clean(self, data, initial=None): def to_python(self, data):
""" """
Checks that the file-upload field data contains a valid image (GIF, JPG, Checks that the file-upload field data contains a valid image (GIF, JPG,
PNG, possibly others -- whatever the Python Imaging Library supports). PNG, possibly others -- whatever the Python Imaging Library supports).
""" """
f = super(ImageField, self).clean(data, initial) f = super(ImageField, self).to_python(data)
if f is None: if f is None:
return None return None
elif not data and initial:
return initial
from PIL import Image from PIL import Image
# We need to get a file object for PIL. We might have a path or we might # We need to get a file object for PIL. We might have a path or we might