Ticket #10149: 10149.diff

File 10149.diff, 3.8 KB (added by jkocherhans, 16 years ago)
  • django/db/models/fields/files.py

    diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
    index cfc4347..4385336 100644
    a b class FileField(Field):  
    222222            setattr(instance, self.name, data)
    223223
    224224    def formfield(self, **kwargs):
    225         defaults = {'form_class': forms.FileField}
     225        defaults = {'form_class': forms.FileField, 'max_length': self.max_length}
    226226        # If a file has been provided previously, then the form doesn't require
    227227        # that a new file is provided this time.
    228228        # The code to mark the form field as not required is used by
  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index a414e76..dc71c57 100644
    a b class FileField(Field):  
    447447        'invalid': _(u"No file was submitted. Check the encoding type on the form."),
    448448        'missing': _(u"No file was submitted."),
    449449        'empty': _(u"The submitted file is empty."),
     450        'max_length': _(u'Ensure this value has at most %(max)d characters (it has %(length)d).'),
    450451    }
    451452
    452453    def __init__(self, *args, **kwargs):
     454        self.max_length = kwargs.pop('max_length', None)
    453455        super(FileField, self).__init__(*args, **kwargs)
    454456
    455457    def clean(self, data, initial=None):
    class FileField(Field):  
    466468        except AttributeError:
    467469            raise ValidationError(self.error_messages['invalid'])
    468470
     471        if self.max_length is not None and len(file_name) > self.max_length:
     472            error_values =  {'max': self.max_length, 'length': len(file_name)}
     473            raise ValidationError(self.error_messages['max_length'] % error_values)
    469474        if not file_name:
    470475            raise ValidationError(self.error_messages['invalid'])
    471476        if not file_size:
  • tests/modeltests/model_forms/models.py

    diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
    index 4075250..c540319 100644
    a b class PhoneNumber(models.Model):  
    9393
    9494class TextFile(models.Model):
    9595    description = models.CharField(max_length=20)
    96     file = models.FileField(storage=temp_storage, upload_to='tests')
     96    file = models.FileField(storage=temp_storage, upload_to='tests', max_length=15)
    9797
    9898    def __unicode__(self):
    9999        return self.description
    True  
    10181018>>> instance.file
    10191019<FieldFile: tests/test1.txt>
    10201020
     1021# Check if the max_length attribute has been inherited from the model.
     1022>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test-maxlength.txt', 'hello world')})
     1023>>> f.is_valid()
     1024False
     1025
    10211026# Edit an instance that already has the file defined in the model. This will not
    10221027# save the file again, but leave it exactly as it is.
    10231028
  • tests/regressiontests/forms/fields.py

    diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py
    index fa5a62a..ac6ab5a 100644
    a b ValidationError: [u'The submitted file is empty.']  
    845845>>> type(f.clean(SimpleUploadedFile('name', 'Some File Content'), 'files/test4.pdf'))
    846846<class 'django.core.files.uploadedfile.SimpleUploadedFile'>
    847847
     848>>> f = FileField(max_length = 5)
     849>>> f.clean(SimpleUploadedFile('test_maxlength.txt', 'hello world'))
     850Traceback (most recent call last):
     851...
     852ValidationError: [u'Ensure this value has at most 5 characters (it has 18).']
     853
     854>>> f.clean('', 'files/test1.pdf')
     855'files/test1.pdf'
     856
     857>>> f.clean(None, 'files/test2.pdf')
     858'files/test2.pdf'
     859
     860>>> type(f.clean(SimpleUploadedFile('name', 'Some File Content')))
     861<class 'django.core.files.uploadedfile.SimpleUploadedFile'>
     862
    848863# URLField ##################################################################
    849864
    850865>>> f = URLField()
Back to Top