Ticket #10149: filefield_patch_and_tests-r9962.diff

File filefield_patch_and_tests-r9962.diff, 3.7 KB (added by Pablo Suárez Hdez., 15 years ago)

FieldField patch and tests

  • django/db/models/fields/files.py

     
    218218            setattr(instance, self.name, data)
    219219
    220220    def formfield(self, **kwargs):
    221         defaults = {'form_class': forms.FileField}
     221        defaults = { 'form_class': forms.FileField, 'max_length' : self.max_length }
    222222        # If a file has been provided previously, then the form doesn't require
    223223        # that a new file is provided this time.
    224224        # The code to mark the form field as not required is used by
  • django/forms/fields.py

     
    446446        'invalid': _(u"No file was submitted. Check the encoding type on the form."),
    447447        'missing': _(u"No file was submitted."),
    448448        'empty': _(u"The submitted file is empty."),
     449        'max_length': _(u"Filename too long. Max: %(max)d characters."),
    449450    }
    450451
    451     def __init__(self, *args, **kwargs):
     452    def __init__(self, max_length = None, *args, **kwargs):
     453        self.max_length = max_length
    452454        super(FileField, self).__init__(*args, **kwargs)
    453455
    454456    def clean(self, data, initial=None):
     
    465467        except AttributeError:
    466468            raise ValidationError(self.error_messages['invalid'])
    467469
     470        if self.max_length is not None and len(file_name) > self.max_length:
     471            raise ValidationError(self.error_messages['max_length'] % {'max': self.max_length, })
    468472        if not file_name:
    469473            raise ValidationError(self.error_messages['invalid'])
    470474        if not file_size:
  • tests/modeltests/model_forms/models.py

     
    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
     
    995995>>> instance.file
    996996<FieldFile: tests/test1.txt>
    997997
     998# Checking if max_length attr has been inherited from model to form by ModelForm.
     999>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test-maxlength.txt', 'hello world')})
     1000>>> f.is_valid()
     1001False
     1002
    9981003# Edit an instance that already has the file defined in the model. This will not
    9991004# save the file again, but leave it exactly as it is.
    10001005
  • tests/regressiontests/forms/fields.py

     
    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'Filename too long. Max: 5 characters.']
     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