Author: jmorliaguet
Date: Thu May  4 22:16:19 2006
New Revision: 3044

Modified:
   cpsskins/branches/paris-sprint-2006/setup/configure.zcml
   cpsskins/branches/paris-sprint-2006/setup/interfaces.py
   cpsskins/branches/paris-sprint-2006/setup/io.py

Log:

- we set the archive in DOMAdapters to make it possible to store attachments

- added an IBinaryFieldIO interface for field types considered as binary types
  (e.g. zope.schema.interfaces.IBytes)




Modified: cpsskins/branches/paris-sprint-2006/setup/configure.zcml
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/configure.zcml    (original)
+++ cpsskins/branches/paris-sprint-2006/setup/configure.zcml    Thu May  4 
22:16:19 2006
@@ -106,6 +106,10 @@
   />
 
   <adapter
+      factory="cpsskins.setup.io.BytesField"
+  />
+
+  <adapter
       factory="cpsskins.setup.io.FloatField"
   />
 

Modified: cpsskins/branches/paris-sprint-2006/setup/interfaces.py
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/interfaces.py     (original)
+++ cpsskins/branches/paris-sprint-2006/setup/interfaces.py     Thu May  4 
22:16:19 2006
@@ -137,6 +137,8 @@
 class IDOMAdapter(Interface):
     """A DOM adapter for loading and saving objects as DOM fragments."""
 
+    archive = Attribute(u"The archive")
+
     document = Attribute(u"The document element.")
 
     object_type = Attribute(u"Type name of objects.")
@@ -149,7 +151,7 @@
                            u"The list of fields written as DOM attributes.")
 
     reserved_fields = Attribute(u"Reserved fields.")
-   
+
     def save():
         """Save data as a DOM fragment."""
 

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 22:16:19 2006
@@ -37,6 +37,8 @@
 class BaseDOMAdapter(object):
     """Base class for DOM adapters.
     """
+    archive = None
+
     document = None
 
     object_type = u''
@@ -71,6 +73,7 @@
     def clone(self, other):
         """Clone the DOM adapter.
         """
+        self.archive = other.archive
         self.document = other.document
         self.object_type = other.object_type
         self.ignored_fields = other.ignored_fields
@@ -100,6 +103,7 @@
         context = self.context
         node = self.node
 
+        archive = self.archive
         document = self.document
 
         type = IType(context)
@@ -121,8 +125,13 @@
             field_io = IFieldIO(field, None)
             if field_io is None:
                 raise TypeError("No field adapter found for '%s'." % field)
-            else:
-                value = field_io.dump(obj)
+
+            value = field_io.dump(obj)
+            # the field IO adapter provides support for binary attachments
+            # the value stored in the DOM is the name of the attachment in
+            # the archive.
+            if IBinaryFieldIO.providedBy(field_io):
+                archive[value] = obj
 
             # set the property as an attribute,
             if attr in self.fields_as_attributes:
@@ -149,6 +158,8 @@
         context = self.context
         node = self.node
 
+        archive = self.archive
+
         # properties
         schema = IType(context).getContentType()
         for name, field in zope.schema.getFieldsInOrder(schema):
@@ -162,8 +173,14 @@
                 raise TypeError("No field adapter found for '%s'." % field)
             if text:
                 value = field_io.load(text)
+                # the field IO adapter provides support for binary attachments
+                # the value stored in the DOM is the name of the attachment
+                # the archive.
+                if IBinaryFieldIO.providedBy(field_io):
+                    value = archive[value]
                 setattr(context, name, value)
 
+
         # child nodes
         for child_el in node.childNodes:
             if child_el.nodeType != Node.ELEMENT_NODE:
@@ -173,9 +190,10 @@
                 field_name = child_el.tagName
                 if name == field_name:
                     text = child_el.getAttribute(u'value')
-                    if text:
-                        value = field_io.load(text)
-                        setattr(context, name, value)
+                    value = field_io.load(text)
+                    if IBinaryFieldIO.providedBy(field_io):
+                        value = archive[value]
+                    setattr(context, name, value)
                 continue
 
             contentname = child_el.getAttribute(u'type') or child_el.tagName
@@ -211,6 +229,9 @@
     def load(text):
         """convert the string into an object"""
 
+class IBinaryFieldIO(IFieldIO):
+    """A binary field importer / exporter."""
+
 class BaseFieldIO:
 
     def __init__(self, field):
@@ -256,6 +277,18 @@
     adapts(zope.schema.interfaces.ITextLine)
     implements(IFieldIO)
 
+class BytesField(BaseFieldIO):
+    """A bytes field.
+    """
+    adapts(zope.schema.interfaces.IBytes)
+    implements(IBinaryFieldIO)
+
+    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__
+
 class BytesLineField(BaseFieldIO):
     """A bytes line field.
     """
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to