Back to index

plone3  3.1.7
propertiestool.py
Go to the documentation of this file.
00001 """Plone Properties tool setup handlers.
00002 
00003 $Id:$
00004 """
00005 
00006 from zope.app import zapi
00007 from Products.CMFCore.utils import getToolByName
00008 from Products.GenericSetup.interfaces import IBody
00009 from Products.GenericSetup.interfaces import INode
00010 from Products.GenericSetup.utils import XMLAdapterBase
00011 from Products.GenericSetup.utils import ObjectManagerHelpers
00012 from Products.GenericSetup.utils import PropertyManagerHelpers
00013 from Products.GenericSetup.utils import I18NURI
00014 from Products.CMFPlone.PropertiesTool import SimpleItemWithProperties
00015 from Products.CMFPlone.interfaces import IPropertiesTool as IPlonePropertiesTool
00016 from Products.CMFPlone.interfaces import ISimpleItemWithProperties
00017 
00018 _FILENAME = 'propertiestool.xml'
00019 
00020 def importPloneProperties(context):
00021     """ Import plone properties tool.
00022     """
00023     site = context.getSite()
00024     logger = context.getLogger('propertiestool')
00025     ptool = getToolByName(site, 'portal_properties')
00026 
00027     body = context.readDataFile(_FILENAME)
00028     if body is None:
00029         logger.info('Nothing to import.')
00030         return
00031 
00032     importer = zapi.queryMultiAdapter((ptool, context), IBody)
00033     if importer is None:
00034         logger.warning('Import adapter missing.')
00035         return
00036 
00037     importer.body = body
00038     logger.info('Properties tool imported.')
00039 
00040 def exportPloneProperties(context):
00041     """ Export plone properties tool.
00042     """
00043     site = context.getSite()
00044     logger = context.getLogger('propertiestool')
00045     ptool = getToolByName(site, 'portal_properties', None)
00046     if ptool is None:
00047         logger.info('Nothing to export.')
00048         return
00049 
00050     exporter = zapi.queryMultiAdapter((ptool, context), IBody)
00051     #IBody(ptool)
00052     if exporter is None:
00053         logger.warning('Export adapter missing.')
00054         return
00055 
00056     context.writeDataFile(_FILENAME, exporter.body, exporter.mime_type)
00057     logger.info('Properties tool exported.')
00058 
00059 class SimpleItemWithPropertiesXMLAdapter(XMLAdapterBase, PropertyManagerHelpers):
00060 
00061     """Node im- and exporter for SimpleItemWithProperties.
00062     """
00063 
00064     __used_for__ = ISimpleItemWithProperties
00065 
00066     def _exportNode(self):
00067         """Export the object as a DOM node.
00068         """
00069         #self._doc = doc
00070         node = self._getObjectNode('object')
00071         node.appendChild(self._extractProperties())
00072         return node
00073 
00074     def _importNode(self, node):
00075         """Import the object from the DOM node.
00076         """
00077         self._initProperties(node)
00078 
00079     node = property(_exportNode, _importNode)
00080 
00081 class PlonePropertiesToolXMLAdapter(XMLAdapterBase, ObjectManagerHelpers):
00082 
00083     """Node im- and exporter for Plone PropertiesTool.
00084     """
00085 
00086     __used_for__ = IPlonePropertiesTool
00087 
00088     def _exportNode(self):
00089         """Export the object as a DOM node.
00090         """
00091         #self._doc = doc
00092         node = self._getObjectNode('object')
00093         #node.setAttribute('xmlns:i18n', I18NURI)
00094         node.appendChild(self._extractObjects())
00095         return node
00096 
00097     def _importNode(self, node):
00098         """Import the object from the DOM node.
00099         """
00100         if self.environ.shouldPurge():
00101             self._purgeObjects()
00102 
00103         self._initObjects(node)
00104 
00105     def _initObjects(self, node):
00106         """Import subobjects"""
00107         ## XXX: We could just use the _initObjects() from
00108         ## ObjectManagerHelpers except that it looks up the object
00109         ## constructor from Products.meta_type and
00110         ## SimpleItemWithProperties doesn't get registered there.
00111         for child in node.childNodes:
00112             if child.nodeName != 'object':
00113                 continue
00114             if child.hasAttribute('deprecated'):
00115                 continue
00116             parent = self.context
00117 
00118             obj_id = str(child.getAttribute('name'))
00119             if obj_id not in parent.objectIds():
00120                 parent._setObject(obj_id, SimpleItemWithProperties(obj_id))
00121 ##                 Original _initObjects code:
00122 ##                 meta_type = str(child.getAttribute('meta_type'))
00123 ##                 for mt_info in Products.meta_types:
00124 ##                     if mt_info['name'] == meta_type:
00125 ##                         parent._setObject(obj_id, mt_info['instance'](obj_id))
00126 ##                         break
00127 ##                 else:
00128 ##                     raise ValueError('unknown meta_type \'%s\'' % obj_id)
00129 
00130             if child.hasAttribute('insert-before'):
00131                 insert_before = child.getAttribute('insert-before')
00132                 if insert_before == '*':
00133                     parent.moveObjectsToTop(obj_id)
00134                 else:
00135                     try:
00136                         position = parent.getObjectPosition(insert_before)
00137                         parent.moveObjectToPosition(obj_id, position)
00138                     except ValueError:
00139                         pass
00140             elif child.hasAttribute('insert-after'):
00141                 insert_after = child.getAttribute('insert-after')
00142                 if insert_after == '*':
00143                     parent.moveObjectsToBottom(obj_id)
00144                 else:
00145                     try:
00146                         position = parent.getObjectPosition(insert_after)
00147                         parent.moveObjectToPosition(obj_id, position+1)
00148                     except ValueError:
00149                         pass
00150 
00151             obj = getattr(self.context, obj_id)
00152             importer = zapi.queryMultiAdapter((obj, self.environ), INode)
00153             if importer:
00154                 importer.node = child