summaryrefslogtreecommitdiff
path: root/tests/model_forms
diff options
context:
space:
mode:
authorManatsawin Hanmongkolchai <manatsawin+git@gmail.com>2017-05-28 14:05:21 +0700
committerTim Graham <timograham@gmail.com>2017-06-01 10:13:23 -0400
commita0c07d77fc313388c72a17cd59411265069f037f (patch)
tree6ca6565eb41de7db58122fe41cc853667ee83a76 /tests/model_forms
parent6bb3b2bff4bca24896023758a720f16fa6b1e2bb (diff)
downloaddjango-a0c07d77fc313388c72a17cd59411265069f037f.tar.gz
Fixed #28242 -- Moved ImageField file extension validation to the form field.
Diffstat (limited to 'tests/model_forms')
-rw-r--r--tests/model_forms/models.py11
-rw-r--r--tests/model_forms/tests.py20
2 files changed, 30 insertions, 1 deletions
diff --git a/tests/model_forms/models.py b/tests/model_forms/models.py
index 6e08eaec71..aecbeb18f3 100644
--- a/tests/model_forms/models.py
+++ b/tests/model_forms/models.py
@@ -192,6 +192,17 @@ try:
def __str__(self):
return self.description
+
+ class NoExtensionImageFile(models.Model):
+ def upload_to(self, filename):
+ return 'tests/no_extension'
+
+ description = models.CharField(max_length=20)
+ image = models.ImageField(storage=temp_storage, upload_to=upload_to)
+
+ def __str__(self):
+ return self.description
+
except ImportError:
test_images = False
diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py
index 6520e54d06..e636d212d8 100644
--- a/tests/model_forms/tests.py
+++ b/tests/model_forms/tests.py
@@ -32,7 +32,7 @@ from .models import (
)
if test_images:
- from .models import ImageFile, OptionalImageFile
+ from .models import ImageFile, OptionalImageFile, NoExtensionImageFile
class ImageFileForm(forms.ModelForm):
class Meta:
@@ -44,6 +44,11 @@ if test_images:
model = OptionalImageFile
fields = '__all__'
+ class NoExtensionImageFileForm(forms.ModelForm):
+ class Meta:
+ model = NoExtensionImageFile
+ fields = '__all__'
+
class ProductForm(forms.ModelForm):
class Meta:
@@ -2461,6 +2466,19 @@ class FileAndImageFieldTests(TestCase):
self.assertEqual(instance.image.name, 'foo/test4.png')
instance.delete()
+ # Editing an instance that has an image without an extension shouldn't
+ # fail validation. First create:
+ f = NoExtensionImageFileForm(
+ data={'description': 'An image'},
+ files={'image': SimpleUploadedFile('test.png', image_data)},
+ )
+ self.assertTrue(f.is_valid())
+ instance = f.save()
+ self.assertEqual(instance.image.name, 'tests/no_extension')
+ # Then edit:
+ f = NoExtensionImageFileForm(data={'description': 'Edited image'}, instance=instance)
+ self.assertTrue(f.is_valid())
+
class ModelOtherFieldTests(SimpleTestCase):
def test_big_integer_field(self):