[Checkins] SVN: z3c.widget/trunk/src/z3c/widget/image/ custom widget for imagefields. widget does not delete field values when delete is not explicitly selected

Manfred Schwendinger manfred.schwendiger at lovelysystems.com
Wed Mar 14 12:05:18 EDT 2007


Log message for revision 73170:
  custom widget for imagefields. widget does not delete field values when delete is not explicitly selected

Changed:
  A   z3c.widget/trunk/src/z3c/widget/image/
  A   z3c.widget/trunk/src/z3c/widget/image/README.txt
  A   z3c.widget/trunk/src/z3c/widget/image/__init__.py
  A   z3c.widget/trunk/src/z3c/widget/image/configure.zcml
  A   z3c.widget/trunk/src/z3c/widget/image/tests.py
  A   z3c.widget/trunk/src/z3c/widget/image/widget.py
  A   z3c.widget/trunk/src/z3c/widget/image/z3c.widget.image.zcml

-=-
Added: z3c.widget/trunk/src/z3c/widget/image/README.txt
===================================================================
--- z3c.widget/trunk/src/z3c/widget/image/README.txt	2007-03-14 13:42:53 UTC (rev 73169)
+++ z3c.widget/trunk/src/z3c/widget/image/README.txt	2007-03-14 16:05:17 UTC (rev 73170)
@@ -0,0 +1,78 @@
+================
+The Image Widget
+================
+
+this image widget should be used as a custom_widget for image fields.
+comparing to the default widget in zope3 it does not delete the
+data in the field if the "delete" checkbox is not explicitly selected.
+
+
+Adding an Image
+---------------
+
+  >>> import zope.schema
+  >>> from zope.publisher.browser import TestRequest
+  >>> from zope import interface
+  >>> from zope.schema.fieldproperty import FieldProperty
+  >>> from zope.app.file.interfaces import IImage
+  >>> from z3c.widget.image.widget import ImageWidget
+  >>> from zope.app.file.image import Image
+
+
+create a content type with an image field.
+
+  >>> class ITestObject(interface.Interface):
+  ...     image = zope.schema.Object(
+  ...     title=u'Image',
+  ...     schema=IImage)
+  >>> class TestObject(object):
+  ...     interface.implements(ITestObject)
+  ...     image = FieldProperty(ITestObject['image'])
+
+  >>> obj = TestObject()
+
+  >>> field = ITestObject['image'].bind(obj)
+
+
+Send the request without any image information. the empty field
+should not be changed...
+  >>> request = TestRequest(form={'field.image' : u''})
+  >>> widget = ImageWidget(field, request)
+  >>> widget._getFormInput() is None
+  True
+
+Send some Image information to the field. the image information
+should be stored in the field as a Image Object
+  >>> request = TestRequest(form={'field.image' : u'PNG123Test'})
+  >>> widget = ImageWidget(field, request)
+  >>> widget._getFormInput()
+  <zope.app.file.image.Image object at ...>
+
+
+Now we save the field again, but without any new image data.
+the old image information should not be lost
+  >>> obj.image = Image(u'PNG123Test')
+  >>> request = TestRequest(form={'field.image' : u''})
+  >>> widget = ImageWidget(field, request)
+  >>> widget._getFormInput() is obj.image
+  True
+
+Now we want to delete the image. the forminput should be None now.
+  >>> request = TestRequest(form={'field.image' : u'',
+  ...     'field.image.delete': u'true'})
+  >>> widget = ImageWidget(field, request)
+  >>> widget._getFormInput() is None
+  True
+
+
+  >>> print widget()
+  <div class="z3cImageWidget">
+       Image Widget
+       <input type="file" name="field.image" id="field.image" />
+       <input type="checkbox"
+              name="field.image.delete"
+              value="true" /> delete image
+  </div>
+
+  
+


Property changes on: z3c.widget/trunk/src/z3c/widget/image/README.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/image/__init__.py
===================================================================
--- z3c.widget/trunk/src/z3c/widget/image/__init__.py	2007-03-14 13:42:53 UTC (rev 73169)
+++ z3c.widget/trunk/src/z3c/widget/image/__init__.py	2007-03-14 16:05:17 UTC (rev 73170)
@@ -0,0 +1 @@
+# package


Property changes on: z3c.widget/trunk/src/z3c/widget/image/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/image/configure.zcml
===================================================================
--- z3c.widget/trunk/src/z3c/widget/image/configure.zcml	2007-03-14 13:42:53 UTC (rev 73169)
+++ z3c.widget/trunk/src/z3c/widget/image/configure.zcml	2007-03-14 16:05:17 UTC (rev 73170)
@@ -0,0 +1,5 @@
+<configure xmlns="http://namespaces.zope.org/zope"
+           xmlns:browser="http://namespaces.zope.org/browser">
+
+  
+</configure>
\ No newline at end of file


Property changes on: z3c.widget/trunk/src/z3c/widget/image/configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/image/tests.py
===================================================================
--- z3c.widget/trunk/src/z3c/widget/image/tests.py	2007-03-14 13:42:53 UTC (rev 73169)
+++ z3c.widget/trunk/src/z3c/widget/image/tests.py	2007-03-14 16:05:17 UTC (rev 73170)
@@ -0,0 +1,23 @@
+import doctest
+import unittest
+from zope.testing.doctestunit import DocFileSuite
+from zope.app.testing import setup
+
+def setUp(test):
+    setup.placefulSetUp(True)
+
+def tearDown(test):
+    setup.placefulTearDown()
+
+def test_suite():
+    
+    return unittest.TestSuite(
+        (
+        DocFileSuite('README.txt',
+                     setUp=setUp,tearDown=tearDown,
+                     optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+                     ),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: z3c.widget/trunk/src/z3c/widget/image/tests.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/image/widget.py
===================================================================
--- z3c.widget/trunk/src/z3c/widget/image/widget.py	2007-03-14 13:42:53 UTC (rev 73169)
+++ z3c.widget/trunk/src/z3c/widget/image/widget.py	2007-03-14 16:05:17 UTC (rev 73170)
@@ -0,0 +1,34 @@
+from zope.app.form.browser.widget import SimpleInputWidget
+from zope.app.file.image import Image
+
+template = u"""
+   <div class="z3cImageWidget">
+     Image Widget
+     <input type="file" name="%(id)s" id="%(id)s" />
+     <input type="checkbox" name="%(id)s.delete" value="true" /> delete image
+   </div>
+"""
+
+class ImageWidget(SimpleInputWidget):
+    pass
+
+    def __call__(self):
+        return template % {
+            'id' : self.name
+            }
+
+    def _getFormInput(self):
+        filedata = self.request.get(self.name)
+        delete = self.request.has_key(self.name+'.delete')
+        if delete:
+            return None
+        else:
+            if not filedata:
+                return self.context.get(self.context.context)
+            else:
+                fileObj = Image(filedata)
+                return fileObj
+        
+    def _toFieldValue(self, input):
+        return input
+


Property changes on: z3c.widget/trunk/src/z3c/widget/image/widget.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/image/z3c.widget.image.zcml
===================================================================
--- z3c.widget/trunk/src/z3c/widget/image/z3c.widget.image.zcml	2007-03-14 13:42:53 UTC (rev 73169)
+++ z3c.widget/trunk/src/z3c/widget/image/z3c.widget.image.zcml	2007-03-14 16:05:17 UTC (rev 73170)
@@ -0,0 +1 @@
+<include package="z3c.widget.image" />
\ No newline at end of file


Property changes on: z3c.widget/trunk/src/z3c/widget/image/z3c.widget.image.zcml
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Checkins mailing list