From daa467d79ec7b40e376294eabc51222a67690181 Mon Sep 17 00:00:00 2001 From: Joseph Kocherhans Date: Thu, 17 Jan 2008 18:12:24 +0000 Subject: newforms-admin: Merged from trunk up to [7021]. git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7022 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/fields.py | 10 ++++++---- django/newforms/forms.py | 8 ++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) (limited to 'django/newforms') diff --git a/django/newforms/fields.py b/django/newforms/fields.py index 3b8f4195b0..0761b8f2af 100644 --- a/django/newforms/fields.py +++ b/django/newforms/fields.py @@ -437,10 +437,12 @@ class FileField(Field): def __init__(self, *args, **kwargs): super(FileField, self).__init__(*args, **kwargs) - def clean(self, data): - super(FileField, self).clean(data) + def clean(self, data, initial=None): + super(FileField, self).clean(initial or data) if not self.required and data in EMPTY_VALUES: return None + elif not data and initial: + return initial try: f = UploadedFile(data['filename'], data['content']) except TypeError: @@ -456,12 +458,12 @@ class ImageField(FileField): 'invalid_image': _(u"Upload a valid image. The file you uploaded was either not an image or a corrupted image."), } - def clean(self, data): + def clean(self, data, initial=None): """ Checks that the file-upload field data contains a valid image (GIF, JPG, PNG, possibly others -- whatever the Python Imaging Library supports). """ - f = super(ImageField, self).clean(data) + f = super(ImageField, self).clean(data, initial) if f is None: return None from PIL import Image diff --git a/django/newforms/forms.py b/django/newforms/forms.py index 88d5f8b3df..9e1b576710 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -9,7 +9,7 @@ from django.utils.html import escape from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode from django.utils.safestring import mark_safe -from fields import Field +from fields import Field, FileField from widgets import Media, media_property, TextInput, Textarea from util import flatatt, ErrorDict, ErrorList, ValidationError @@ -205,7 +205,11 @@ class BaseForm(StrAndUnicode): # widgets split data over several HTML fields. value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) try: - value = field.clean(value) + if isinstance(field, FileField): + initial = self.initial.get(name, field.initial) + value = field.clean(value, initial) + else: + value = field.clean(value) self.cleaned_data[name] = value if hasattr(self, 'clean_%s' % name): value = getattr(self, 'clean_%s' % name)() -- cgit v1.3