summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorJoseph Kocherhans <joseph@jkocherhans.com>2009-03-30 22:52:16 +0000
committerJoseph Kocherhans <joseph@jkocherhans.com>2009-03-30 22:52:16 +0000
commitfea6c73538148a7362a9cdd85bbc4732ddb4204d (patch)
treec1686c3f8f6960fb958992235a817ad39698aac5 /django
parent6ce25d3be9804ca199b65110d10240559293a917 (diff)
Fixed #10149. FileFields in a form now validate max_length. Based on a patch by Massimo Scamarcia.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10227 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/db/models/fields/files.py2
-rw-r--r--django/forms/fields.py5
2 files changed, 6 insertions, 1 deletions
diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
index cfc434741b..43853369d1 100644
--- a/django/db/models/fields/files.py
+++ b/django/db/models/fields/files.py
@@ -222,7 +222,7 @@ class FileField(Field):
setattr(instance, self.name, data)
def formfield(self, **kwargs):
- defaults = {'form_class': forms.FileField}
+ defaults = {'form_class': forms.FileField, 'max_length': self.max_length}
# If a file has been provided previously, then the form doesn't require
# that a new file is provided this time.
# The code to mark the form field as not required is used by
diff --git a/django/forms/fields.py b/django/forms/fields.py
index a414e76890..9c4084450a 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -447,9 +447,11 @@ class FileField(Field):
'invalid': _(u"No file was submitted. Check the encoding type on the form."),
'missing': _(u"No file was submitted."),
'empty': _(u"The submitted file is empty."),
+ 'max_length': _(u'Ensure this filename has at most %(max)d characters (it has %(length)d).'),
}
def __init__(self, *args, **kwargs):
+ self.max_length = kwargs.pop('max_length', None)
super(FileField, self).__init__(*args, **kwargs)
def clean(self, data, initial=None):
@@ -466,6 +468,9 @@ class FileField(Field):
except AttributeError:
raise ValidationError(self.error_messages['invalid'])
+ if self.max_length is not None and len(file_name) > self.max_length:
+ error_values = {'max': self.max_length, 'length': len(file_name)}
+ raise ValidationError(self.error_messages['max_length'] % error_values)
if not file_name:
raise ValidationError(self.error_messages['invalid'])
if not file_size: