Back to index

plone3  3.1.7
Public Member Functions | Public Attributes
plone.app.portlets.exportimport.portlets.PropertyPortletAssignmentExportImportHandler Class Reference

List of all members.

Public Member Functions

def __init__
def import_assignment
def export_assignment
def import_node
def export_field
def extract_text
def from_unicode
def field_typecast

Public Attributes

 assignment

Detailed Description

Import portlet assignment settings based on zope.schema properties

Definition at line 69 of file portlets.py.


Constructor & Destructor Documentation

Definition at line 76 of file portlets.py.

00076 
00077     def __init__(self, assignment):
00078         self.assignment = assignment
        

Here is the caller graph for this function:


Member Function Documentation

Definition at line 84 of file portlets.py.

00084 
00085     def export_assignment(self, interface, doc, node):
00086         for field_name in interface:
00087             field = interface[field_name]
00088             
00089             if not IField.providedBy(field):
00090                 continue
00091             
00092             child = self.export_field(doc, field)
00093             node.appendChild(child)
        

Here is the call graph for this function:

Turn a zope.schema field into a node and return it

Definition at line 129 of file portlets.py.

00129 
00130     def export_field(self, doc, field):
00131         """Turn a zope.schema field into a node and return it
00132         """
00133         field = field.bind(self.assignment)
00134         value = field.get(self.assignment)
00135         
00136         child = doc.createElement('property')
00137         child.setAttribute('name', field.__name__)
00138         
00139         if value is not None:
00140             if ICollection.providedBy(field):
00141                 for e in value:
00142                     list_element = doc.createElement('element')
00143                     list_element.appendChild(doc.createTextNode(str(e)))
00144             else:
00145                 child.appendChild(doc.createTextNode(unicode(value)))
00146             
00147         return child
        

Here is the caller graph for this function:

Definition at line 148 of file portlets.py.

00148 
00149     def extract_text(self, node):
00150         node.normalize()
00151         text = u""
00152         for child in node.childNodes:
00153             if child.nodeType == node.TEXT_NODE:
00154                 text += child.nodeValue
00155         return text
        

Here is the caller graph for this function:

Definition at line 166 of file portlets.py.

00166 
00167     def field_typecast(self, field, value):
00168         # A slight hack to force sequence types to the right type
00169         typecast = getattr(field, '_type', None)
00170         if typecast is not None:
00171             if not isinstance(typecast, (list, tuple)):
00172                 typecast = (typecast,)
00173             for tc in reversed(typecast):
00174                 if callable(tc):
00175                     try:
00176                         value = tc(value)
00177                         break
00178                     except:
00179                         pass
00180         return value

Here is the caller graph for this function:

Definition at line 156 of file portlets.py.

00156 
00157     def from_unicode(self, field, value):
00158         
00159         # XXX: Bool incorrectly omits to declare that it implements
00160         # IFromUnicode, even though it does.
00161         import zope.schema
00162         if IFromUnicode.providedBy(field) or isinstance(field, zope.schema.Bool):
00163             return field.fromUnicode(value)
00164         else:
00165             return self.field_typecast(field, value)
    

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 79 of file portlets.py.

00079 
00080     def import_assignment(self, interface, node):
00081         for child in node.childNodes:
00082             if child.nodeName == 'property':
00083                 self.import_node(interface, child)
    

Here is the call graph for this function:

Import a single <property /> node

Definition at line 96 of file portlets.py.

00096 
00097     def import_node(self, interface, child):
00098         """Import a single <property /> node
00099         """
00100         property_name = child.getAttribute('name')
00101         
00102         field = interface.get(property_name, None)
00103         if field is None:
00104             return
00105         
00106         field = field.bind(self.assignment)
00107         value = None
00108         
00109         # If we have a collection, we need to look at the value_type.
00110         # We look for <element>value</element> child nodes and get the
00111         # value from there
00112         if ICollection.providedBy(field):
00113             value_type = field.value_type
00114             value = []
00115             for element in child.childNodes:
00116                 if element.nodeName != 'element':
00117                     continue
00118                 element_value = self.extract_text(element)
00119                 value.append(self.from_unicode(value_type, element_value))
00120             value = self.field_typecast(field, value)
00121         
00122         # Otherwise, just get the value of the <property /> node
00123         else:
00124             value = self.extract_text(child)
00125             value = self.from_unicode(field, value)
00126             
00127         field.validate(value)
00128         field.set(self.assignment, value)
        

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 77 of file portlets.py.


The documentation for this class was generated from the following file: