Author: kmtracey Date: 2009-02-16 12:34:28 -0600 (Mon, 16 Feb 2009) New Revision: 9841
Added: django/trunk/tests/modeltests/model_forms/test2.png Modified: django/trunk/AUTHORS django/trunk/django/db/models/fields/files.py django/trunk/tests/modeltests/model_forms/models.py Log: 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. Modified: django/trunk/AUTHORS =================================================================== --- django/trunk/AUTHORS 2009-02-16 17:30:12 UTC (rev 9840) +++ django/trunk/AUTHORS 2009-02-16 18:34:28 UTC (rev 9841) @@ -35,6 +35,7 @@ Jeff Anderson <jeffe...@programmerq.net> Marian Andre <dja...@andre.sk> Andreas + Victor Andrée a...@jadedplanet.net Fabrice Aneche <a...@nobugware.com> ant9...@netwise.it Modified: django/trunk/django/db/models/fields/files.py =================================================================== --- django/trunk/django/db/models/fields/files.py 2009-02-16 17:30:12 UTC (rev 9840) +++ django/trunk/django/db/models/fields/files.py 2009-02-16 18:34:28 UTC (rev 9841) @@ -132,7 +132,7 @@ # have the FieldFile interface added to them file_copy = copy.copy(file) file_copy.__class__ = type(file.__class__.__name__, - (file.__class__, FieldFile), {}) + (file.__class__, self.field.attr_class), {}) file_copy.instance = instance file_copy.field = self.field file_copy.storage = self.field.storage Modified: django/trunk/tests/modeltests/model_forms/models.py =================================================================== --- django/trunk/tests/modeltests/model_forms/models.py 2009-02-16 17:30:12 UTC (rev 9840) +++ django/trunk/tests/modeltests/model_forms/models.py 2009-02-16 18:34:28 UTC (rev 9841) @@ -98,26 +98,47 @@ def __unicode__(self): return self.description -class ImageFile(models.Model): - def custom_upload_path(self, filename): - path = self.path or 'tests' - return '%s/%s' % (path, filename) +try: + # If PIL is available, try testing ImageFields. + # Checking for the existence of Image is enough for CPython, but + # for PyPy, you need to check for the underlying modules + # If PIL is not available, ImageField tests are omitted. + from PIL import Image, _imaging + test_images = True - description = models.CharField(max_length=20) - try: - # If PIL is available, try testing PIL. - # Checking for the existence of Image is enough for CPython, but - # for PyPy, you need to check for the underlying modules - # If PIL is not available, this test is equivalent to TextFile above. - from PIL import Image, _imaging - image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path) - except ImportError: - image = models.FileField(storage=temp_storage, upload_to=custom_upload_path) - path = models.CharField(max_length=16, blank=True, default='') + class ImageFile(models.Model): + def custom_upload_path(self, filename): + path = self.path or 'tests' + return '%s/%s' % (path, filename) + + description = models.CharField(max_length=20) + image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path, + width_field='width', height_field='height') + width = models.IntegerField(editable=False) + height = models.IntegerField(editable=False) + path = models.CharField(max_length=16, blank=True, default='') - def __unicode__(self): - return self.description + def __unicode__(self): + return self.description + + class OptionalImageFile(models.Model): + def custom_upload_path(self, filename): + path = self.path or 'tests' + return '%s/%s' % (path, filename) + + description = models.CharField(max_length=20) + image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path, + width_field='width', height_field='height', + blank=True, null=True) + width = models.IntegerField(editable=False, null=True) + height = models.IntegerField(editable=False, null=True) + path = models.CharField(max_length=16, blank=True, default='') + def __unicode__(self): + return self.description +except ImportError: + test_images = False + class CommaSeparatedInteger(models.Model): field = models.CommaSeparatedIntegerField(max_length=20) @@ -1056,7 +1077,10 @@ # Delete the current file since this is not done by Django. >>> instance.file.delete() >>> instance.delete() +"""} +if test_images: + __test__['API_TESTS'] += """ # ImageField ################################################################### # ImageField and FileField are nearly identical, but they differ slighty when @@ -1068,6 +1092,7 @@ ... model = ImageFile >>> image_data = open(os.path.join(os.path.dirname(__file__), "test.png"), >>> 'rb').read() +>>> image_data2 = open(os.path.join(os.path.dirname(__file__), "test2.png"), 'rb').read() >>> f = ImageFileForm(data={'description': u'An image'}, files={'image': >>> SimpleUploadedFile('test.png', image_data)}) >>> f.is_valid() @@ -1077,6 +1102,10 @@ >>> instance = f.save() >>> instance.image <...FieldFile: tests/test.png> +>>> instance.width +16 +>>> instance.height +16 # Delete the current file since this is not done by Django. >>> instance.image.delete() @@ -1089,8 +1118,12 @@ >>> instance = f.save() >>> instance.image <...FieldFile: tests/test.png> +>>> instance.width +16 +>>> instance.height +16 -# Edit an instance that already has the image defined in the model. This will not +# Edit an instance that already has the (required) image defined in the model. This will not # save the image again, but leave it exactly as it is. >>> f = ImageFileForm(data={'description': u'Look, it changed'}, >>> instance=instance) @@ -1101,6 +1134,10 @@ >>> instance = f.save() >>> instance.image <...FieldFile: tests/test.png> +>>> instance.height +16 +>>> instance.width +16 # Delete the current image since this is not done by Django. @@ -1108,23 +1145,31 @@ # Override the file by uploading a new one. ->>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data)}, instance=instance) +>>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data2)}, instance=instance) >>> f.is_valid() True >>> instance = f.save() >>> instance.image <...FieldFile: tests/test2.png> +>>> instance.height +32 +>>> instance.width +48 # Delete the current file since this is not done by Django. >>> instance.image.delete() >>> instance.delete() ->>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data)}) +>>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data2)}) >>> f.is_valid() True >>> instance = f.save() >>> instance.image <...FieldFile: tests/test2.png> +>>> instance.height +32 +>>> instance.width +48 # Delete the current file since this is not done by Django. >>> instance.image.delete() @@ -1132,31 +1177,58 @@ # Test the non-required ImageField ->>> f = ImageFileForm(data={'description': u'Test'}) ->>> f.fields['image'].required = False +>>> class OptionalImageFileForm(ModelForm): +... class Meta: +... model = OptionalImageFile + +>>> f = OptionalImageFileForm(data={'description': u'Test'}) >>> f.is_valid() True >>> instance = f.save() >>> instance.image <...FieldFile: None> +>>> instance.width +>>> instance.height ->>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)}, instance=instance) +>>> f = OptionalImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)}, instance=instance) >>> f.is_valid() True >>> instance = f.save() >>> instance.image <...FieldFile: tests/test3.png> +>>> instance.width +16 +>>> instance.height +16 +# Editing the instance without re-uploading the image should not affect the image or its width/height properties +>>> f = OptionalImageFileForm(data={'description': u'New Description'}, instance=instance) +>>> f.is_valid() +True +>>> instance = f.save() +>>> instance.description +u'New Description' +>>> instance.image +<...FieldFile: tests/test3.png> +>>> instance.width +16 +>>> instance.height +16 + # Delete the current file since this is not done by Django. >>> instance.image.delete() >>> instance.delete() ->>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)}) +>>> f = OptionalImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test4.png', image_data2)}) >>> f.is_valid() True >>> instance = f.save() >>> instance.image -<...FieldFile: tests/test3.png> +<...FieldFile: tests/test4.png> +>>> instance.width +48 +>>> instance.height +32 >>> instance.delete() # Test callable upload_to behavior that's dependent on the value of another field in the model @@ -1167,7 +1239,10 @@ >>> instance.image <...FieldFile: foo/test4.png> >>> instance.delete() +""" +__test__['API_TESTS'] += """ + # Media on a ModelForm ######################################################## # Similar to a regular Form class you can define custom media to be used on @@ -1355,4 +1430,4 @@ # Clean up >>> import shutil >>> shutil.rmtree(temp_storage_dir) -"""} +""" Added: django/trunk/tests/modeltests/model_forms/test2.png =================================================================== (Binary files differ) Property changes on: django/trunk/tests/modeltests/model_forms/test2.png ___________________________________________________________________ Name: svn:mime-type + application/octet-stream --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-updates?hl=en -~----------~----~----~----~------~----~------~--~---