Ticket #18515: 18515-1.diff

File 18515-1.diff, 2.3 KB (added by Claude Paroz, 12 years ago)

Do not always regenerate filename in FileField validation

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

    diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
    index e0c0959..b0dce38 100644
    a b class FileField(Field):  
    242242        # (ie. upload_to='path/to/upload/dir'), the length of the generated
    243243        # name equals the length of the uploaded name plus a constant. Thus
    244244        # we can tell the user how much shorter the name should be (roughly).
    245         length = len(self.generate_filename(model_instance, value.name))
     245        if value and value._committed:
     246            filename = value.name
     247        else:
     248            filename = self.generate_filename(model_instance, value.name)
     249        length = len(filename)
    246250        if self.max_length and length > self.max_length:
    247251            error_values = {'extra': length - self.max_length}
    248252            raise ValidationError(self.error_messages['max_length'] % error_values)
  • tests/modeltests/files/models.py

    diff --git a/tests/modeltests/files/models.py b/tests/modeltests/files/models.py
    index 4134472..cefc7c7 100644
    a b class Storage(models.Model):  
    2626
    2727    normal = models.FileField(storage=temp_storage, upload_to='tests')
    2828    custom = models.FileField(storage=temp_storage, upload_to=custom_upload_to)
    29     random = models.FileField(storage=temp_storage, upload_to=random_upload_to)
     29    random = models.FileField(storage=temp_storage, upload_to=random_upload_to, max_length=16)
    3030    default = models.FileField(storage=temp_storage, upload_to='tests', default='tests/default.txt')
  • tests/modeltests/files/tests.py

    diff --git a/tests/modeltests/files/tests.py b/tests/modeltests/files/tests.py
    index 3e256f7..eb81615 100644
    a b class FileStorageTests(TestCase):  
    102102        obj4.random.save("random_file", ContentFile(b"random content"))
    103103        self.assertTrue(obj4.random.name.endswith("/random_file"))
    104104
     105        # Ticket #18515: validation for an already saved file should not check
     106        # against a regenerated file name (and potentially raise a ValidationError
     107        # if max_length is exceeded)
     108        obj4.full_clean()
     109
    105110        # Clean up the temporary files and dir.
    106111        obj1.normal.delete()
    107112        obj2.normal.delete()
Back to Top