Ticket #10149: filefield_patch_and_tests-r9962.diff
File filefield_patch_and_tests-r9962.diff, 3.7 KB (added by , 16 years ago) |
---|
-
django/db/models/fields/files.py
218 218 setattr(instance, self.name, data) 219 219 220 220 def formfield(self, **kwargs): 221 defaults = { 'form_class': forms.FileField}221 defaults = { 'form_class': forms.FileField, 'max_length' : self.max_length } 222 222 # If a file has been provided previously, then the form doesn't require 223 223 # that a new file is provided this time. 224 224 # The code to mark the form field as not required is used by -
django/forms/fields.py
446 446 'invalid': _(u"No file was submitted. Check the encoding type on the form."), 447 447 'missing': _(u"No file was submitted."), 448 448 'empty': _(u"The submitted file is empty."), 449 'max_length': _(u"Filename too long. Max: %(max)d characters."), 449 450 } 450 451 451 def __init__(self, *args, **kwargs): 452 def __init__(self, max_length = None, *args, **kwargs): 453 self.max_length = max_length 452 454 super(FileField, self).__init__(*args, **kwargs) 453 455 454 456 def clean(self, data, initial=None): … … 465 467 except AttributeError: 466 468 raise ValidationError(self.error_messages['invalid']) 467 469 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, }) 468 472 if not file_name: 469 473 raise ValidationError(self.error_messages['invalid']) 470 474 if not file_size: -
tests/modeltests/model_forms/models.py
93 93 94 94 class TextFile(models.Model): 95 95 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) 97 97 98 98 def __unicode__(self): 99 99 return self.description … … 995 995 >>> instance.file 996 996 <FieldFile: tests/test1.txt> 997 997 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() 1001 False 1002 998 1003 # Edit an instance that already has the file defined in the model. This will not 999 1004 # save the file again, but leave it exactly as it is. 1000 1005 -
tests/regressiontests/forms/fields.py
845 845 >>> type(f.clean(SimpleUploadedFile('name', 'Some File Content'), 'files/test4.pdf')) 846 846 <class 'django.core.files.uploadedfile.SimpleUploadedFile'> 847 847 848 >>> f = FileField(max_length = 5) 849 >>> f.clean(SimpleUploadedFile('test_maxlength.txt', 'hello world')) 850 Traceback (most recent call last): 851 ... 852 ValidationError: [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 848 863 # URLField ################################################################## 849 864 850 865 >>> f = URLField()