[Products.Silva][Emiliano D'Alterio] Fixed bug "Once scaled, Sil...

[email protected] Thu, 19 Sep 2013 11:11:13 +0200
Newsgroups gmane.comp.web.zope.silva.cvs
Message-ID <[email protected]>
author:    Emiliano D'Alterio
date:      Thu Sep 19 11:11:05 2013 +0200
revision:  11860:f3f0db71327e in Products.Silva
branch:    2.4
details:   https://hg.infrae.com/Products.Silva?cmd=changeset;node=f3f0db71327e
modified:  Products/Silva/Image/content.py Products/Silva/tests/test_image.py
added:     
removed:   
log:       Fixed bug "Once scaled, Silva Images cannot have scaling removed"
	(https://bugs.launchpad.net/silva/+bug/1190145). Added tests to
	cover this.


diffstat:

 Products/Silva/Image/content.py    |   9 ++++++---
 Products/Silva/tests/test_image.py |  37 +++++++++++++++++++++++++++++++++----
 2 files changed, 39 insertions(+), 7 deletions(-)

diffs (105 lines):

diff -r d88cd4ce0728 -r f3f0db71327e Products/Silva/Image/content.py
--- a/Products/Silva/Image/content.py	Tue Sep 17 11:48:00 2013 +0200
+++ b/Products/Silva/Image/content.py	Thu Sep 19 11:11:05 2013 +0200
@@ -268,7 +268,6 @@
         web_scale (str): WidthXHeight or nn.n%.
         web_crop (str): X1xY1-X2xY2, crop-box or empty for no cropping.
 
-        Raises ValueError if web_scale cannot be parsed.
 
         Automaticaly updates cached web presentation image.
         """
@@ -286,7 +285,12 @@
             else:
                 raise ValueError('Unknown image format %s' % web_format)
         # check if web_scale can be parsed:
-        self.get_canonical_web_scale(web_scale)
+        try:
+            self.get_canonical_web_scale(web_scale)
+        except ValueError:
+            # if not, we set web_scale back to default value
+            web_scale = '100%'
+
         if self.web_scale != web_scale:
             self.web_scale = web_scale
             update = True
@@ -719,4 +723,3 @@
         if image_file is None:
             continue
         guess_filename(image_file, event.newName)
-
diff -r d88cd4ce0728 -r f3f0db71327e Products/Silva/tests/test_image.py
--- a/Products/Silva/tests/test_image.py	Tue Sep 17 11:48:00 2013 +0200
+++ b/Products/Silva/tests/test_image.py	Thu Sep 19 11:11:05 2013 +0200
@@ -53,7 +53,7 @@
         image = self.root._getOb('test_image')
         metadata = getUtility(IMetadataService).getMetadata(image)
         metadata.setValues('silva-extra', {
-                'modificationtime': DateTime('2010-04-25T12:00:00Z')})
+            'modificationtime': DateTime('2010-04-25T12:00:00Z')})
 
     def test_image(self):
         """Test image content.
@@ -103,6 +103,32 @@
         """Test set web presentation.
         """
         image = self.root._getOb('test_image')
+        original_dimensions = image.get_dimensions()
+
+        ## resizing image to 200x100.
+        image.set_web_presentation_properties('', '200x100', '')
+        ## it should be 200x100 now.
+        self.assertEquals((200, 100), image.get_dimensions())
+
+        ## resizing passing an empty size paramenter.
+        image.set_web_presentation_properties('', '', '')
+        ## it should be back to the original size now (100%).
+        self.assertEquals(original_dimensions, image.get_dimensions())
+
+        ## resizing passing an invalid size parameter.
+        image.set_web_presentation_properties('', 'invalid image size', '')
+        ## it should still have the original size.
+        self.assertEquals(original_dimensions, image.get_dimensions())
+
+        ## resizing image to 370x230.
+        image.set_web_presentation_properties('', '370x230', '')
+        ## it should be 370x230 now.
+        self.assertEquals((370, 230), image.get_dimensions())
+
+        ## resizing passing an invalid size parameter (again).
+        image.set_web_presentation_properties('', ' again an invalid image size ', '')
+        ## it should be back to the original size now (100%).
+        self.assertEquals(original_dimensions, image.get_dimensions())
 
     def test_add_image_with_existing_id(self):
         with Transaction():
@@ -110,7 +136,8 @@
                 factory = self.root.manage_addProduct['Silva']
                 factory.manage_addImage('test_image_id', 'Test Image 1', image)
                 with self.assertRaises(ValueError) as error:
-                    factory.manage_addImage('test_image_id', 'Test Image 2', image)
+                    factory.manage_addImage('test_image_id',
+                                            'Test Image 2', image)
 
         self.assertEqual(
             str(error.exception),
@@ -267,7 +294,8 @@
         """If you access the preview of an image, the cache should be disabled.
         """
         with self.layer.get_browser() as browser:
-            self.assertEqual(browser.open('/root/++preview++/test_image?thumbnail'), 200)
+            self.assertEqual(
+                browser.open('/root/++preview++/test_image?thumbnail'), 200)
             self.assertEqual(
                 browser.headers['Content-Disposition'],
                 'inline;filename=test_image.jpeg')
@@ -298,7 +326,8 @@
         data = self.root.test_image.image
         with self.layer.get_browser() as browser:
             browser.options.handle_errors = False
-            self.assertEquals(browser.open('/root/test_image', method='HEAD'), 200)
+            self.assertEquals(
+                browser.open('/root/test_image', method='HEAD'), 200)
             self.assertEquals(
                 browser.headers['Content-Disposition'],
                 'inline;filename=test_image.jpeg')