Ticket #18515: 18515-2.diff

File 18515-2.diff, 4.2 KB (added by claudep, 3 years ago)

Regrouped FileField max_length tests

  • 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..565d4c8 100644
    a b import shutil 
    55import tempfile
    66
    77from django.core.cache import cache
     8from django.core.exceptions import ValidationError
    89from django.core.files import File
    910from django.core.files.base import ContentFile
    1011from django.core.files.uploadedfile import SimpleUploadedFile
    class FileStorageTests(TestCase): 
    102103        obj4.random.save("random_file", ContentFile(b"random content"))
    103104        self.assertTrue(obj4.random.name.endswith("/random_file"))
    104105
    105         # Clean up the temporary files and dir.
    106         obj1.normal.delete()
    107         obj2.normal.delete()
    108         obj3.default.delete()
    109         obj4.random.delete()
     106    def test_max_length(self):
     107        """
     108        Test that FileField validates the length of the generated file name
     109        that will be stored in the database. Regression for #9893.
     110        """
     111        # upload_to = 'unused', so file names are saved as '456/xxxxx'.
     112        # max_length = 16, so names longer than 12 characters are rejected.
     113        s1 = Storage(random=SimpleUploadedFile(12 * 'x', b"content"))
     114        s1.full_clean()
     115        with self.assertRaises(ValidationError):
     116            Storage(random=SimpleUploadedFile(13 * 'x', b"content")).full_clean()
     117
     118        # Ticket #18515: validation for an already saved file should not check
     119        # against a regenerated file name (and potentially raise a ValidationError
     120        # if max_length is exceeded
     121        s1.save()
     122        s1.full_clean()
    110123
    111124
    112125class FileTests(unittest.TestCase):
  • tests/regressiontests/model_fields/tests.py

    diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
    index 5d3d42e..a89ffca 100644
    a b class FileFieldTests(unittest.TestCase): 
    365365        field = d._meta.get_field('myfile')
    366366        field.save_form_data(d, 'else.txt')
    367367        self.assertEqual(d.myfile, 'else.txt')
    368 
    369     def test_max_length(self):
    370         """
    371         Test that FileField validates the length of the generated file name
    372         that will be stored in the database. Regression for #9893.
    373 
    374         """
    375         # upload_to = 'unused', so file names are saved as 'unused/xxxxx'.
    376         # max_length = 100, so names longer than 93 characters are rejected.
    377         Document(myfile=93 * 'x').full_clean()
    378         with self.assertRaises(ValidationError):
    379             Document(myfile=94 * 'x').full_clean()
Back to Top