Author: jmorliaguet
Date: Thu May  4 23:30:46 2006
New Revision: 3046

Modified:
   cpsskins/branches/paris-sprint-2006/setup/README.txt
   cpsskins/branches/paris-sprint-2006/setup/io.py

Log:

- attempt to call attachments according to the data they contain.



Modified: cpsskins/branches/paris-sprint-2006/setup/README.txt
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/README.txt        (original)
+++ cpsskins/branches/paris-sprint-2006/setup/README.txt        Thu May  4 
23:30:46 2006
@@ -500,6 +500,7 @@
     ...     document = dom.createDocument(None, title, None)
     ...     doc = document.documentElement
     ...     exporter = getMultiAdapter((obj, doc), IDOMAdapter)
+    ...     exporter.archive = {}
     ...     exporter.document = document
     ...     exporter.fields_as_attributes = attributes
     ...     exporter.ignored_fields = ignored
@@ -695,6 +696,35 @@
     </formats>
     <BLANKLINE>
 
+Export of binary data
+---------------------
+
+    >>> from cpsskins.standard.portlets.image.portlet import Image
+    >>> portlet = Image(title=u'Image portlet')
+    >>> portlet.contentType = u'image/png'
+
+    >>> portlet.data = "\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00x" \
+    ... "\x00\x00\x00x\x01\x03\x00\x00\x00\x03\xaa\xa4\x91\x00\x00\x00" \
+    ... "\x03PLTE\xff\xff\xff\xa7\xc4\x1b\xc8\x00\x00\x00\x17IDAT8\xcbc`" \
+    ... "\x18\x05\xa3`\x14\x8c\x82Q0\nF\x01\xbd\x01\x00\x07\x80\x00\x01:" \
+    ... "\xe7\xca\xd4\x00\x00\x00\x00IEND\xaeB`\x82"
+
+    >>> from zope.app.file.image import getImageInfo
+    >>> getImageInfo(portlet.data)
+    ('image/png', 120, 120)
+
+    >>> print toXML(portlet, u'portlets')
+    <?xml version="1.0" encoding="utf-8"?>
+    <portlets>
+      <portlet id="12345" type="standard.image">
+        <title value="Image portlet"/>
+        <link value=""/>
+        <data value="12345_data_120x120.png"/>
+        <contentType value="image/png"/>
+      </portlet>
+    </portlets>
+    <BLANKLINE>
+
 
 Resolving URIs
 ==============

Modified: cpsskins/branches/paris-sprint-2006/setup/io.py
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/io.py     (original)
+++ cpsskins/branches/paris-sprint-2006/setup/io.py     Thu May  4 23:30:46 2006
@@ -18,7 +18,7 @@
 __docformat__ = "reStructuredText"
 
 import logging
-
+import mimetypes
 from xml.dom.minidom import Node
 
 import zope.schema
@@ -27,6 +27,7 @@
 from zope.component import createObject
 from zope.interface import implements, Interface
 from zope.app.container.interfaces import IItemContainer, INameChooser
+from zope.app.file.image import getImageInfo
 
 from cpsskins.relations.interfaces import IRelatable
 from cpsskins.setup.interfaces import IDOMAdapter, IType
@@ -109,7 +110,8 @@
         type = IType(context)
 
         element_el = document.createElement(type.resourcename)
-        element_el.setAttribute(u'id', unicode(IRelatable(context)))
+        element_id = unicode(IRelatable(context))
+        element_el.setAttribute(u'id', element_id)
 
         if type.contentname != type.resourcename:
             element_el.setAttribute(u'type', unicode(type.contentname))
@@ -131,6 +133,7 @@
             # the value stored in the DOM is the name of the attachment in
             # the archive.
             if IBinaryFieldIO.providedBy(field_io):
+                value = u'%s_%s%s' % (element_id, attr, value)
                 archive[value] = obj
 
             # set the property as an attribute,
@@ -177,7 +180,8 @@
                 # the value stored in the DOM is the name of the attachment
                 # the archive.
                 if IBinaryFieldIO.providedBy(field_io):
-                    value = archive[value]
+                    filename = value
+                    value = archive[filename]
                 setattr(context, name, value)
 
 
@@ -192,7 +196,8 @@
                     text = child_el.getAttribute(u'value')
                     value = field_io.load(text)
                     if IBinaryFieldIO.providedBy(field_io):
-                        value = archive[value]
+                        filename = value
+                        value = archive[filename]
                     setattr(context, name, value)
                 continue
 
@@ -286,8 +291,14 @@
     def dump(self, obj):
         """Instead of storing the data, the name of the attachment is returned.
         """
-        # XXX for testing, must find a better name
-        return self.field.__name__
+        name = u''
+        # Attempt to guess the data encoding
+        encoding, width, height = getImageInfo(obj)
+        if width and height:
+            name += u'_%sx%s' % (width, height)
+        if encoding:
+            name += mimetypes.guess_extension(encoding)
+        return name
 
 class BytesLineField(BaseFieldIO):
     """A bytes line field.
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to