Back to index

plone3  3.1.7
typeinfo.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """Types tool xml adapters and setup handlers.
00014 
00015 $Id: typeinfo.py 76996 2007-06-24 00:18:49Z hannosch $
00016 """
00017 
00018 from zope.component import adapts
00019 
00020 from Products.GenericSetup.interfaces import ISetupEnviron
00021 from Products.GenericSetup.utils import exportObjects
00022 from Products.GenericSetup.utils import I18NURI
00023 from Products.GenericSetup.utils import importObjects
00024 from Products.GenericSetup.utils import ObjectManagerHelpers
00025 from Products.GenericSetup.utils import PropertyManagerHelpers
00026 from Products.GenericSetup.utils import XMLAdapterBase
00027 
00028 from Products.CMFCore.interfaces import ITypeInformation
00029 from Products.CMFCore.interfaces import ITypesTool
00030 from Products.CMFCore.utils import getToolByName
00031 
00032 
00033 class TypeInformationXMLAdapter(XMLAdapterBase, PropertyManagerHelpers):
00034 
00035     """XML im- and exporter for TypeInformation.
00036     """
00037 
00038     adapts(ITypeInformation, ISetupEnviron)
00039 
00040     _LOGGER_ID = 'types'
00041 
00042     def _exportNode(self):
00043         """Export the object as a DOM node.
00044         """
00045         node = self._getObjectNode('object')
00046         node.setAttribute('xmlns:i18n', I18NURI)
00047         node.appendChild(self._extractProperties())
00048         node.appendChild(self._extractAliases())
00049         node.appendChild(self._extractActions())
00050 
00051         self._logger.info('%r type info exported.' % self.context.getId())
00052         return node
00053 
00054     def _importNode(self, node):
00055         """Import the object from the DOM node.
00056         """
00057         if self.environ.shouldPurge():
00058             self._purgeProperties()
00059             self._purgeAliases()
00060             self._purgeActions()
00061 
00062         self._initProperties(node)
00063         self._initAliases(node)
00064         self._initActions(node)
00065 
00066         obj_id = str(node.getAttribute('name'))
00067         self._logger.info('%r type info imported.' % obj_id)
00068 
00069     def _extractAliases(self):
00070         fragment = self._doc.createDocumentFragment()
00071         aliases = self.context.getMethodAliases().items()
00072         aliases.sort()
00073         for k, v in aliases:
00074             child = self._doc.createElement('alias')
00075             child.setAttribute('from', k)
00076             child.setAttribute('to', v)
00077             fragment.appendChild(child)
00078         return fragment
00079 
00080     def _purgeAliases(self):
00081         self.context.setMethodAliases({})
00082 
00083     def _initAliases(self, node):
00084         aliases = self.context.getMethodAliases()
00085         for child in node.childNodes:
00086             if child.nodeName != 'alias':
00087                 continue
00088             k = str(child.getAttribute('from'))
00089             v = str(child.getAttribute('to'))
00090             aliases[k] = v
00091         self.context.setMethodAliases(aliases)
00092 
00093     def _extractActions(self):
00094         fragment = self._doc.createDocumentFragment()
00095         actions = self.context.listActions()
00096         for ai in actions:
00097             ai_info = ai.getMapping()
00098             child = self._doc.createElement('action')
00099             child.setAttribute('title', ai_info['title'])
00100             child.setAttribute('action_id', ai_info['id'])
00101             child.setAttribute('category', ai_info['category'])
00102             child.setAttribute('condition_expr', ai_info['condition'])
00103             child.setAttribute('url_expr', ai_info['action'])
00104             child.setAttribute('visible', str(bool(ai_info['visible'])))
00105             for permission in ai_info['permissions']:
00106                 sub = self._doc.createElement('permission')
00107                 sub.setAttribute('value', permission)
00108                 child.appendChild(sub)
00109             fragment.appendChild(child)
00110         return fragment
00111 
00112     def _purgeActions(self):
00113         self.context._actions = ()
00114 
00115     def _initActions(self, node):
00116         for child in node.childNodes:
00117             if child.nodeName != 'action':
00118                 continue
00119             title = str(child.getAttribute('title'))
00120             id = str(child.getAttribute('action_id'))
00121             category = str(child.getAttribute('category'))
00122             condition = str(child.getAttribute('condition_expr'))
00123             action = str(child.getAttribute('url_expr'))
00124             visible = self._convertToBoolean(child.getAttribute('visible'))
00125             permissions = []
00126             for sub in child.childNodes:
00127                 if sub.nodeName != 'permission':
00128                     continue
00129                 permission = sub.getAttribute('value')
00130                 permissions.append(permission)
00131             action_obj = self.context.getActionObject(category+'/'+id)
00132             if action_obj is None:
00133                 self.context.addAction(id, title, action, condition,
00134                                        tuple(permissions), category, visible)
00135             else:
00136                 action_obj.edit(title=title, action=action,
00137                                 condition=condition,
00138                                 permissions=tuple(permissions),
00139                                 visible=visible)
00140 
00141 
00142 class TypesToolXMLAdapter(XMLAdapterBase, ObjectManagerHelpers,
00143                           PropertyManagerHelpers):
00144 
00145     """XML im- and exporter for TypesTool.
00146     """
00147 
00148     adapts(ITypesTool, ISetupEnviron)
00149 
00150     _LOGGER_ID = 'types'
00151 
00152     name = 'types'
00153 
00154     def _exportNode(self):
00155         """Export the object as a DOM node.
00156         """
00157         node = self._getObjectNode('object')
00158         node.appendChild(self._extractProperties())
00159         node.appendChild(self._extractObjects())
00160 
00161         self._logger.info('Types tool exported.')
00162         return node
00163 
00164     def _importNode(self, node):
00165         """Import the object from the DOM node.
00166         """
00167         if self.environ.shouldPurge():
00168             self._purgeProperties()
00169             self._purgeObjects()
00170 
00171         self._initProperties(node)
00172         self._initObjects(node)
00173 
00174         self._logger.info('Types tool imported.')
00175 
00176 
00177 def importTypesTool(context):
00178     """Import types tool and content types from XML files.
00179     """
00180     site = context.getSite()
00181     tool = getToolByName(site, 'portal_types')
00182 
00183     importObjects(tool, '', context)
00184 
00185 def exportTypesTool(context):
00186     """Export types tool content types as a set of XML files.
00187     """
00188     site = context.getSite()
00189     tool = getToolByName(site, 'portal_types', None)
00190     if tool is None:
00191         logger = context.getLogger('types')
00192         logger.info('Nothing to export.')
00193         return
00194 
00195     exportObjects(tool, '', context)