Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10196: Restored setting of image file width and height fields …

…lost in r9766, and added tests for this function. Thanks to vicvicvic for the ticket and patch and for Alex for reminding me not to break non-PIL-boxes with the new tests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9841 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ecc55503f40e375b957aa9dfaf8db34e8f348f63 1 parent 11bfe72
Karen Tracey authored February 16, 2009
1  AUTHORS
@@ -35,6 +35,7 @@ answer newbie questions, and generally made Django that much better:
35 35
     Jeff Anderson <jefferya@programmerq.net>
36 36
     Marian Andre <django@andre.sk>
37 37
     Andreas
  38
+    Victor Andrée    
38 39
     andy@jadedplanet.net
39 40
     Fabrice Aneche <akh@nobugware.com>
40 41
     ant9000@netwise.it
2  django/db/models/fields/files.py
@@ -132,7 +132,7 @@ def __get__(self, instance=None, owner=None):
132 132
             # have the FieldFile interface added to them
133 133
             file_copy = copy.copy(file)
134 134
             file_copy.__class__ = type(file.__class__.__name__, 
135  
-                                       (file.__class__, FieldFile), {})
  135
+                                       (file.__class__, self.field.attr_class), {})
136 136
             file_copy.instance = instance
137 137
             file_copy.field = self.field
138 138
             file_copy.storage = self.field.storage
131  tests/modeltests/model_forms/models.py
@@ -98,26 +98,47 @@ class TextFile(models.Model):
98 98
     def __unicode__(self):
99 99
         return self.description
100 100
 
101  
-class ImageFile(models.Model):
102  
-    def custom_upload_path(self, filename):
103  
-        path = self.path or 'tests'
104  
-        return '%s/%s' % (path, filename)
  101
+try:
  102
+    # If PIL is available, try testing ImageFields.
  103
+    # Checking for the existence of Image is enough for CPython, but
  104
+    # for PyPy, you need to check for the underlying modules
  105
+    # If PIL is not available, ImageField tests are omitted.
  106
+    from PIL import Image, _imaging
  107
+    test_images = True
  108
+    
  109
+    class ImageFile(models.Model):
  110
+        def custom_upload_path(self, filename):
  111
+            path = self.path or 'tests'
  112
+            return '%s/%s' % (path, filename)
  113
+    
  114
+        description = models.CharField(max_length=20)
  115
+        image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
  116
+                                  width_field='width', height_field='height')
  117
+        width = models.IntegerField(editable=False)
  118
+        height = models.IntegerField(editable=False)
  119
+        path = models.CharField(max_length=16, blank=True, default='')
  120
+
  121
+        def __unicode__(self):
  122
+            return self.description
  123
+    
  124
+    class OptionalImageFile(models.Model):
  125
+        def custom_upload_path(self, filename):
  126
+            path = self.path or 'tests'
  127
+            return '%s/%s' % (path, filename)
  128
+    
  129
+        description = models.CharField(max_length=20)
  130
+        image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
  131
+                                  width_field='width', height_field='height', 
  132
+                                  blank=True, null=True)
  133
+        width = models.IntegerField(editable=False, null=True)
  134
+        height = models.IntegerField(editable=False, null=True)
  135
+        path = models.CharField(max_length=16, blank=True, default='')
  136
+
  137
+        def __unicode__(self):
  138
+            return self.description
  139
+except ImportError:
  140
+    test_images = False
105 141
     
106  
-    description = models.CharField(max_length=20)
107  
-    try:
108  
-        # If PIL is available, try testing PIL.
109  
-        # Checking for the existence of Image is enough for CPython, but
110  
-        # for PyPy, you need to check for the underlying modules
111  
-        # If PIL is not available, this test is equivalent to TextFile above.
112  
-        from PIL import Image, _imaging
113  
-        image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path)
114  
-    except ImportError:
115  
-        image = models.FileField(storage=temp_storage, upload_to=custom_upload_path)
116  
-    path = models.CharField(max_length=16, blank=True, default='')
117  
-
118  
-    def __unicode__(self):
119  
-        return self.description
120  
-
121 142
 class CommaSeparatedInteger(models.Model):
122 143
     field = models.CommaSeparatedIntegerField(max_length=20)
123 144
 
@@ -1056,7 +1077,10 @@ def __unicode__(self):
1056 1077
 # Delete the current file since this is not done by Django.
1057 1078
 >>> instance.file.delete()
1058 1079
 >>> instance.delete()
  1080
+"""}
1059 1081
 
  1082
+if test_images:
  1083
+    __test__['API_TESTS'] += """
1060 1084
 # ImageField ###################################################################
1061 1085
 
1062 1086
 # ImageField and FileField are nearly identical, but they differ slighty when
@@ -1068,6 +1092,7 @@ def __unicode__(self):
1068 1092
 ...         model = ImageFile
1069 1093
 
1070 1094
 >>> image_data = open(os.path.join(os.path.dirname(__file__), "test.png"), 'rb').read()
  1095
+>>> image_data2 = open(os.path.join(os.path.dirname(__file__), "test2.png"), 'rb').read()
1071 1096
 
1072 1097
 >>> f = ImageFileForm(data={'description': u'An image'}, files={'image': SimpleUploadedFile('test.png', image_data)})
1073 1098
 >>> f.is_valid()
@@ -1077,6 +1102,10 @@ def __unicode__(self):
1077 1102
 >>> instance = f.save()
1078 1103
 >>> instance.image
1079 1104
 <...FieldFile: tests/test.png>
  1105
+>>> instance.width
  1106
+16
  1107
+>>> instance.height
  1108
+16
1080 1109
 
1081 1110
 # Delete the current file since this is not done by Django.
1082 1111
 >>> instance.image.delete()
@@ -1089,8 +1118,12 @@ def __unicode__(self):
1089 1118
 >>> instance = f.save()
1090 1119
 >>> instance.image
1091 1120
 <...FieldFile: tests/test.png>
  1121
+>>> instance.width
  1122
+16
  1123
+>>> instance.height
  1124
+16
1092 1125
 
1093  
-# Edit an instance that already has the image defined in the model. This will not
  1126
+# Edit an instance that already has the (required) image defined in the model. This will not
1094 1127
 # save the image again, but leave it exactly as it is.
1095 1128
 
1096 1129
 >>> f = ImageFileForm(data={'description': u'Look, it changed'}, instance=instance)
@@ -1101,6 +1134,10 @@ def __unicode__(self):
1101 1134
 >>> instance = f.save()
1102 1135
 >>> instance.image
1103 1136
 <...FieldFile: tests/test.png>
  1137
+>>> instance.height
  1138
+16
  1139
+>>> instance.width
  1140
+16
1104 1141
 
1105 1142
 # Delete the current image since this is not done by Django.
1106 1143
 
@@ -1108,23 +1145,31 @@ def __unicode__(self):
1108 1145
 
1109 1146
 # Override the file by uploading a new one.
1110 1147
 
1111  
->>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data)}, instance=instance)
  1148
+>>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data2)}, instance=instance)
1112 1149
 >>> f.is_valid()
1113 1150
 True
1114 1151
 >>> instance = f.save()
1115 1152
 >>> instance.image
1116 1153
 <...FieldFile: tests/test2.png>
  1154
+>>> instance.height
  1155
+32
  1156
+>>> instance.width
  1157
+48
1117 1158
 
1118 1159
 # Delete the current file since this is not done by Django.
1119 1160
 >>> instance.image.delete()
1120 1161
 >>> instance.delete()
1121 1162
 
1122  
->>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data)})
  1163
+>>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data2)})
1123 1164
 >>> f.is_valid()
1124 1165
 True
1125 1166
 >>> instance = f.save()
1126 1167
 >>> instance.image
1127 1168
 <...FieldFile: tests/test2.png>
  1169
+>>> instance.height
  1170
+32
  1171
+>>> instance.width
  1172
+48
1128 1173
 
1129 1174
 # Delete the current file since this is not done by Django.
1130 1175
 >>> instance.image.delete()
@@ -1132,31 +1177,58 @@ def __unicode__(self):
1132 1177
 
1133 1178
 # Test the non-required ImageField
1134 1179
 
1135  
->>> f = ImageFileForm(data={'description': u'Test'})
1136  
->>> f.fields['image'].required = False
  1180
+>>> class OptionalImageFileForm(ModelForm):
  1181
+...     class Meta:
  1182
+...         model = OptionalImageFile
  1183
+
  1184
+>>> f = OptionalImageFileForm(data={'description': u'Test'})
1137 1185
 >>> f.is_valid()
1138 1186
 True
1139 1187
 >>> instance = f.save()
1140 1188
 >>> instance.image
1141 1189
 <...FieldFile: None>
  1190
+>>> instance.width
  1191
+>>> instance.height
1142 1192
 
1143  
->>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)}, instance=instance)
  1193
+>>> f = OptionalImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)}, instance=instance)
1144 1194
 >>> f.is_valid()
1145 1195
 True
1146 1196
 >>> instance = f.save()
1147 1197
 >>> instance.image
1148 1198
 <...FieldFile: tests/test3.png>
  1199
+>>> instance.width
  1200
+16
  1201
+>>> instance.height
  1202
+16
  1203
+
  1204
+# Editing the instance without re-uploading the image should not affect the image or its width/height properties
  1205
+>>> f = OptionalImageFileForm(data={'description': u'New Description'}, instance=instance)
  1206
+>>> f.is_valid()
  1207
+True
  1208
+>>> instance = f.save()
  1209
+>>> instance.description
  1210
+u'New Description'
  1211
+>>> instance.image
  1212
+<...FieldFile: tests/test3.png>
  1213
+>>> instance.width
  1214
+16
  1215
+>>> instance.height
  1216
+16
1149 1217
 
1150 1218
 # Delete the current file since this is not done by Django.
1151 1219
 >>> instance.image.delete()
1152 1220
 >>> instance.delete()
1153 1221
 
1154  
->>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)})
  1222
+>>> f = OptionalImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test4.png', image_data2)})
1155 1223
 >>> f.is_valid()
1156 1224
 True
1157 1225
 >>> instance = f.save()
1158 1226
 >>> instance.image
1159  
-<...FieldFile: tests/test3.png>
  1227
+<...FieldFile: tests/test4.png>
  1228
+>>> instance.width
  1229
+48
  1230
+>>> instance.height
  1231
+32
1160 1232
 >>> instance.delete()
1161 1233
 
1162 1234
 # Test callable upload_to behavior that's dependent on the value of another field in the model
@@ -1167,6 +1239,9 @@ def __unicode__(self):
1167 1239
 >>> instance.image
1168 1240
 <...FieldFile: foo/test4.png>
1169 1241
 >>> instance.delete()
  1242
+"""
  1243
+
  1244
+__test__['API_TESTS'] += """
1170 1245
 
1171 1246
 # Media on a ModelForm ########################################################
1172 1247
 
@@ -1355,4 +1430,4 @@ def __unicode__(self):
1355 1430
 # Clean up
1356 1431
 >>> import shutil
1357 1432
 >>> shutil.rmtree(temp_storage_dir)
1358  
-"""}
  1433
+"""
BIN  tests/modeltests/model_forms/test2.png

0 notes on commit ecc5550

Please sign in to comment.
Something went wrong with that request. Please try again.