Code

Ticket #10196: 10196.diff

File 10196.diff, 6.7 KB (added by kmtracey, 5 years ago)
Line 
1Index: django/db/models/fields/files.py
2===================================================================
3--- django/db/models/fields/files.py    (revision 9834)
4+++ django/db/models/fields/files.py    (working copy)
5@@ -132,7 +132,7 @@
6             # have the FieldFile interface added to them
7             file_copy = copy.copy(file)
8             file_copy.__class__ = type(file.__class__.__name__,
9-                                       (file.__class__, FieldFile), {})
10+                                       (file.__class__, self.field.attr_class), {})
11             file_copy.instance = instance
12             file_copy.field = self.field
13             file_copy.storage = self.field.storage
14Index: tests/modeltests/model_forms/test2.png
15===================================================================
16Cannot display: file marked as a binary type.
17svn:mime-type = application/octet-stream
18
19Property changes on: tests/modeltests/model_forms/test2.png
20___________________________________________________________________
21Name: svn:mime-type
22   + application/octet-stream
23
24Index: tests/modeltests/model_forms/models.py
25===================================================================
26--- tests/modeltests/model_forms/models.py      (revision 9834)
27+++ tests/modeltests/model_forms/models.py      (working copy)
28@@ -110,14 +110,41 @@
29         # for PyPy, you need to check for the underlying modules
30         # If PIL is not available, this test is equivalent to TextFile above.
31         from PIL import Image, _imaging
32-        image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path)
33+        image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
34+                                  width_field='width', height_field='height')
35+        width = models.IntegerField(editable=False)
36+        height = models.IntegerField(editable=False)
37     except ImportError:
38         image = models.FileField(storage=temp_storage, upload_to=custom_upload_path)
39     path = models.CharField(max_length=16, blank=True, default='')
40 
41     def __unicode__(self):
42         return self.description
43+   
44+class OptionalImageFile(models.Model):
45+    def custom_upload_path(self, filename):
46+        path = self.path or 'tests'
47+        return '%s/%s' % (path, filename)
48+   
49+    description = models.CharField(max_length=20)
50+    try:
51+        # If PIL is available, try testing PIL.
52+        # Checking for the existence of Image is enough for CPython, but
53+        # for PyPy, you need to check for the underlying modules
54+        # If PIL is not available, this test is equivalent to TextFile above.
55+        from PIL import Image, _imaging
56+        image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
57+                                  width_field='width', height_field='height',
58+                                  blank=True, null=True)
59+        width = models.IntegerField(editable=False, null=True)
60+        height = models.IntegerField(editable=False, null=True)
61+    except ImportError:
62+        image = models.FileField(storage=temp_storage, upload_to=custom_upload_path)
63+    path = models.CharField(max_length=16, blank=True, default='')
64 
65+    def __unicode__(self):
66+        return self.description
67+
68 class CommaSeparatedInteger(models.Model):
69     field = models.CommaSeparatedIntegerField(max_length=20)
70 
71@@ -1056,6 +1083,7 @@
72 ...         model = ImageFile
73 
74 >>> image_data = open(os.path.join(os.path.dirname(__file__), "test.png"), 'rb').read()
75+>>> image_data2 = open(os.path.join(os.path.dirname(__file__), "test2.png"), 'rb').read()
76 
77 >>> f = ImageFileForm(data={'description': u'An image'}, files={'image': SimpleUploadedFile('test.png', image_data)})
78 >>> f.is_valid()
79@@ -1065,6 +1093,10 @@
80 >>> instance = f.save()
81 >>> instance.image
82 <...FieldFile: tests/test.png>
83+>>> instance.width
84+16
85+>>> instance.height
86+16
87 
88 # Delete the current file since this is not done by Django.
89 >>> instance.image.delete()
90@@ -1077,6 +1109,10 @@
91 >>> instance = f.save()
92 >>> instance.image
93 <...FieldFile: tests/test.png>
94+>>> instance.width
95+16
96+>>> instance.height
97+16
98 
99 # Edit an instance that already has the image defined in the model. This will not
100 # save the image again, but leave it exactly as it is.
101@@ -1089,6 +1125,10 @@
102 >>> instance = f.save()
103 >>> instance.image
104 <...FieldFile: tests/test.png>
105+>>> instance.height
106+16
107+>>> instance.width
108+16
109 
110 # Delete the current image since this is not done by Django.
111 
112@@ -1102,17 +1142,25 @@
113 >>> instance = f.save()
114 >>> instance.image
115 <...FieldFile: tests/test2.png>
116+>>> instance.height
117+16
118+>>> instance.width
119+16
120 
121 # Delete the current file since this is not done by Django.
122 >>> instance.image.delete()
123 >>> instance.delete()
124 
125->>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data)})
126+>>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data2)})
127 >>> f.is_valid()
128 True
129 >>> instance = f.save()
130 >>> instance.image
131 <...FieldFile: tests/test2.png>
132+>>> instance.height
133+32
134+>>> instance.width
135+48
136 
137 # Delete the current file since this is not done by Django.
138 >>> instance.image.delete()
139@@ -1120,31 +1168,44 @@
140 
141 # Test the non-required ImageField
142 
143->>> f = ImageFileForm(data={'description': u'Test'})
144->>> f.fields['image'].required = False
145+>>> class OptionalImageFileForm(ModelForm):
146+...     class Meta:
147+...         model = OptionalImageFile
148+
149+>>> f = OptionalImageFileForm(data={'description': u'Test'})
150 >>> f.is_valid()
151 True
152 >>> instance = f.save()
153 >>> instance.image
154 <...FieldFile: None>
155+>>> instance.width
156+>>> instance.height
157 
158->>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)}, instance=instance)
159+>>> f = OptionalImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)}, instance=instance)
160 >>> f.is_valid()
161 True
162 >>> instance = f.save()
163 >>> instance.image
164 <...FieldFile: tests/test3.png>
165+>>> instance.width
166+16
167+>>> instance.height
168+16
169 
170 # Delete the current file since this is not done by Django.
171 >>> instance.image.delete()
172 >>> instance.delete()
173 
174->>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)})
175+>>> f = OptionalImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test4.png', image_data2)})
176 >>> f.is_valid()
177 True
178 >>> instance = f.save()
179 >>> instance.image
180-<...FieldFile: tests/test3.png>
181+<...FieldFile: tests/test4.png>
182+>>> instance.width
183+48
184+>>> instance.height
185+32
186 >>> instance.delete()
187 
188 # Test callable upload_to behavior that's dependent on the value of another field in the model