Ticket #10196: 10196.diff
File 10196.diff, 6.7 KB (added by , 16 years ago) |
---|
-
django/db/models/fields/files.py
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 -
tests/modeltests/model_forms/models.py
Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream Property changes on: tests/modeltests/model_forms/test2.png ___________________________________________________________________ Name: svn:mime-type + application/octet-stream
110 110 # for PyPy, you need to check for the underlying modules 111 111 # If PIL is not available, this test is equivalent to TextFile above. 112 112 from PIL import Image, _imaging 113 image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path) 113 image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path, 114 width_field='width', height_field='height') 115 width = models.IntegerField(editable=False) 116 height = models.IntegerField(editable=False) 114 117 except ImportError: 115 118 image = models.FileField(storage=temp_storage, upload_to=custom_upload_path) 116 119 path = models.CharField(max_length=16, blank=True, default='') 117 120 118 121 def __unicode__(self): 119 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 try: 131 # If PIL is available, try testing PIL. 132 # Checking for the existence of Image is enough for CPython, but 133 # for PyPy, you need to check for the underlying modules 134 # If PIL is not available, this test is equivalent to TextFile above. 135 from PIL import Image, _imaging 136 image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path, 137 width_field='width', height_field='height', 138 blank=True, null=True) 139 width = models.IntegerField(editable=False, null=True) 140 height = models.IntegerField(editable=False, null=True) 141 except ImportError: 142 image = models.FileField(storage=temp_storage, upload_to=custom_upload_path) 143 path = models.CharField(max_length=16, blank=True, default='') 120 144 145 def __unicode__(self): 146 return self.description 147 121 148 class CommaSeparatedInteger(models.Model): 122 149 field = models.CommaSeparatedIntegerField(max_length=20) 123 150 … … 1056 1083 ... model = ImageFile 1057 1084 1058 1085 >>> image_data = open(os.path.join(os.path.dirname(__file__), "test.png"), 'rb').read() 1086 >>> image_data2 = open(os.path.join(os.path.dirname(__file__), "test2.png"), 'rb').read() 1059 1087 1060 1088 >>> f = ImageFileForm(data={'description': u'An image'}, files={'image': SimpleUploadedFile('test.png', image_data)}) 1061 1089 >>> f.is_valid() … … 1065 1093 >>> instance = f.save() 1066 1094 >>> instance.image 1067 1095 <...FieldFile: tests/test.png> 1096 >>> instance.width 1097 16 1098 >>> instance.height 1099 16 1068 1100 1069 1101 # Delete the current file since this is not done by Django. 1070 1102 >>> instance.image.delete() … … 1077 1109 >>> instance = f.save() 1078 1110 >>> instance.image 1079 1111 <...FieldFile: tests/test.png> 1112 >>> instance.width 1113 16 1114 >>> instance.height 1115 16 1080 1116 1081 1117 # Edit an instance that already has the image defined in the model. This will not 1082 1118 # save the image again, but leave it exactly as it is. … … 1089 1125 >>> instance = f.save() 1090 1126 >>> instance.image 1091 1127 <...FieldFile: tests/test.png> 1128 >>> instance.height 1129 16 1130 >>> instance.width 1131 16 1092 1132 1093 1133 # Delete the current image since this is not done by Django. 1094 1134 … … 1102 1142 >>> instance = f.save() 1103 1143 >>> instance.image 1104 1144 <...FieldFile: tests/test2.png> 1145 >>> instance.height 1146 16 1147 >>> instance.width 1148 16 1105 1149 1106 1150 # Delete the current file since this is not done by Django. 1107 1151 >>> instance.image.delete() 1108 1152 >>> instance.delete() 1109 1153 1110 >>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data )})1154 >>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data2)}) 1111 1155 >>> f.is_valid() 1112 1156 True 1113 1157 >>> instance = f.save() 1114 1158 >>> instance.image 1115 1159 <...FieldFile: tests/test2.png> 1160 >>> instance.height 1161 32 1162 >>> instance.width 1163 48 1116 1164 1117 1165 # Delete the current file since this is not done by Django. 1118 1166 >>> instance.image.delete() … … 1120 1168 1121 1169 # Test the non-required ImageField 1122 1170 1123 >>> f = ImageFileForm(data={'description': u'Test'}) 1124 >>> f.fields['image'].required = False 1171 >>> class OptionalImageFileForm(ModelForm): 1172 ... class Meta: 1173 ... model = OptionalImageFile 1174 1175 >>> f = OptionalImageFileForm(data={'description': u'Test'}) 1125 1176 >>> f.is_valid() 1126 1177 True 1127 1178 >>> instance = f.save() 1128 1179 >>> instance.image 1129 1180 <...FieldFile: None> 1181 >>> instance.width 1182 >>> instance.height 1130 1183 1131 >>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)}, instance=instance)1184 >>> f = OptionalImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)}, instance=instance) 1132 1185 >>> f.is_valid() 1133 1186 True 1134 1187 >>> instance = f.save() 1135 1188 >>> instance.image 1136 1189 <...FieldFile: tests/test3.png> 1190 >>> instance.width 1191 16 1192 >>> instance.height 1193 16 1137 1194 1138 1195 # Delete the current file since this is not done by Django. 1139 1196 >>> instance.image.delete() 1140 1197 >>> instance.delete() 1141 1198 1142 >>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)})1199 >>> f = OptionalImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test4.png', image_data2)}) 1143 1200 >>> f.is_valid() 1144 1201 True 1145 1202 >>> instance = f.save() 1146 1203 >>> instance.image 1147 <...FieldFile: tests/test3.png> 1204 <...FieldFile: tests/test4.png> 1205 >>> instance.width 1206 48 1207 >>> instance.height 1208 32 1148 1209 >>> instance.delete() 1149 1210 1150 1211 # Test callable upload_to behavior that's dependent on the value of another field in the model