Back to index

plone3  3.1.7
actions.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 """Actions tool node adapters.
00014 
00015 $Id: actions.py 77186 2007-06-28 19:06:19Z yuppie $
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 NodeAdapterBase
00025 from Products.GenericSetup.utils import ObjectManagerHelpers
00026 from Products.GenericSetup.utils import PropertyManagerHelpers
00027 from Products.GenericSetup.utils import XMLAdapterBase
00028 
00029 from Products.CMFCore.interfaces import IAction
00030 from Products.CMFCore.interfaces import IActionCategory
00031 from Products.CMFCore.interfaces import IActionProvider
00032 from Products.CMFCore.interfaces import IActionsTool
00033 from Products.CMFCore.interfaces.portal_actions \
00034         import ActionProvider as z2IActionProvider
00035 from Products.CMFCore.utils import getToolByName
00036 
00037 _SPECIAL_PROVIDERS = ('portal_actions', 'portal_types', 'portal_workflow')
00038 
00039 
00040 class ActionCategoryNodeAdapter(NodeAdapterBase, ObjectManagerHelpers,
00041                                 PropertyManagerHelpers):
00042 
00043     """Node im- and exporter for ActionCategory.
00044     """
00045 
00046     adapts(IActionCategory, ISetupEnviron)
00047 
00048     def _exportNode(self):
00049         """Export the object as a DOM node.
00050         """
00051         node = self._getObjectNode('object')
00052         node.appendChild(self._extractProperties())
00053         node.appendChild(self._extractObjects())
00054         return node
00055 
00056     def _importNode(self, node):
00057         """Import the object from the DOM node.
00058         """
00059         purge = self.environ.shouldPurge()
00060         if node.getAttribute('purge'):
00061             purge = self._convertToBoolean(node.getAttribute('purge'))
00062         if purge:
00063             self._purgeProperties()
00064             self._purgeObjects()
00065 
00066         self._initProperties(node)
00067         self._initObjects(node)
00068 
00069     node = property(_exportNode, _importNode)
00070 
00071 
00072 class ActionNodeAdapter(NodeAdapterBase, PropertyManagerHelpers):
00073 
00074     """Node im- and exporter for Action.
00075     """
00076 
00077     adapts(IAction, ISetupEnviron)
00078 
00079     def _exportNode(self):
00080         """Export the object as a DOM node.
00081         """
00082         node = self._getObjectNode('object')
00083         node.appendChild(self._extractProperties())
00084         return node
00085 
00086     def _importNode(self, node):
00087         """Import the object from the DOM node.
00088         """
00089         purge = self.environ.shouldPurge()
00090         if node.getAttribute('purge'):
00091             purge = self._convertToBoolean(node.getAttribute('purge'))
00092         if purge:
00093             self._purgeProperties()
00094 
00095         self._initProperties(node)
00096 
00097     node = property(_exportNode, _importNode)
00098 
00099 
00100 class ActionsToolXMLAdapter(XMLAdapterBase, ObjectManagerHelpers):
00101 
00102     """XML im- and exporter for ActionsTool.
00103     """
00104 
00105     adapts(IActionsTool, ISetupEnviron)
00106 
00107     _LOGGER_ID = 'actions'
00108 
00109     name = 'actions'
00110 
00111     def _exportNode(self):
00112         """Export the object as a DOM node.
00113         """
00114         node = self._getObjectNode('object')
00115         node.setAttribute('xmlns:i18n', I18NURI)
00116         node.appendChild(self._extractProviders())
00117         node.appendChild(self._extractObjects())
00118 
00119         self._logger.info('Actions tool exported.')
00120         return node
00121 
00122     def _importNode(self, node):
00123         """Import the object from the DOM node.
00124         """
00125         if self.environ.shouldPurge():
00126             self._purgeProviders()
00127             self._purgeObjects()
00128 
00129         self._initObjects(node)
00130         self._initProviders(node)
00131 
00132         self._logger.info('Actions tool imported.')
00133 
00134     def _extractProviders(self):
00135         fragment = self._doc.createDocumentFragment()
00136         for provider_id in self.context.listActionProviders():
00137             child = self._doc.createElement('action-provider')
00138             child.setAttribute('name', provider_id)
00139             # BBB: for CMF 1.6 profiles
00140             sub = self._extractOldstyleActions(provider_id)
00141             child.appendChild(sub)
00142             fragment.appendChild(child)
00143         return fragment
00144 
00145     def _extractOldstyleActions(self, provider_id):
00146         # BBB: for CMF 1.6 profiles
00147         fragment = self._doc.createDocumentFragment()
00148 
00149         provider = getToolByName(self.context, provider_id)
00150         if not (IActionProvider.providedBy(provider) or
00151                 z2IActionProvider.isImplementedBy(provider)):
00152             return fragment
00153 
00154         if provider_id == 'portal_actions':
00155             actions = provider._actions
00156         else:
00157             actions = provider.listActions()
00158 
00159         if actions and isinstance(actions[0], dict):
00160             return fragment
00161 
00162         for ai in actions:
00163             mapping = ai.getMapping()
00164             child = self._doc.createElement('action')
00165             child.setAttribute('action_id', mapping['id'])
00166             child.setAttribute('category', mapping['category'])
00167             child.setAttribute('condition_expr', mapping['condition'])
00168             child.setAttribute('title', mapping['title'])
00169             child.setAttribute('url_expr', mapping['action'])
00170             child.setAttribute('visible', str(mapping['visible']))
00171             for permission in mapping['permissions']:
00172                 sub = self._doc.createElement('permission')
00173                 sub.appendChild(self._doc.createTextNode(permission))
00174                 child.appendChild(sub)
00175             fragment.appendChild(child)
00176         return fragment
00177 
00178     def _purgeProviders(self):
00179         for provider_id in self.context.listActionProviders():
00180             self.context.deleteActionProvider(provider_id)
00181 
00182     def _initProviders(self, node):
00183         for child in node.childNodes:
00184             if child.nodeName != 'action-provider':
00185                 continue
00186 
00187             provider_id = str(child.getAttribute('name'))
00188             if child.hasAttribute('remove'):
00189                 if provider_id in self.context.listActionProviders():
00190                     self.context.deleteActionProvider(provider_id)
00191                 continue
00192 
00193             if provider_id in _SPECIAL_PROVIDERS and \
00194                     provider_id not in self.context.listActionProviders():
00195                 self.context.addActionProvider(provider_id)
00196 
00197             # BBB: for CMF 1.6 profiles
00198             self._initOldstyleActions(child)
00199 
00200     def _initOldstyleActions(self, node):
00201         # BBB: for CMF 1.6 profiles
00202         doc = node.ownerDocument
00203         fragment = doc.createDocumentFragment()
00204         for child in node.childNodes:
00205             if child.nodeName != 'action':
00206                 continue
00207 
00208             parent = fragment
00209             for category_id in child.getAttribute('category').split('/'):
00210                 newnode = doc.createElement('object')
00211                 newnode.setAttribute('name', str(category_id))
00212                 newnode.setAttribute('meta_type', 'CMF Action Category')
00213                 newnode.setAttribute('purge', 'False')
00214                 parent.appendChild(newnode)
00215                 parent = newnode
00216             newnode = doc.createElement('object')
00217             newnode.setAttribute('name', str(child.getAttribute('action_id')))
00218             newnode.setAttribute('meta_type', 'CMF Action')
00219             newnode.setAttribute('purge', 'False')
00220 
00221             mapping = {'title': 'title',
00222                        'url_expr': 'url_expr',
00223                        'condition_expr': 'available_expr',
00224                        'visible': 'visible'}
00225             for old, new in mapping.iteritems():
00226                 newchild = doc.createElement('property')
00227                 newchild.setAttribute('name', new)
00228                 newsub = doc.createTextNode(child.getAttribute(old))
00229                 newchild.appendChild(newsub)
00230                 newnode.appendChild(newchild)
00231 
00232             newchild = doc.createElement('property')
00233             newchild.setAttribute('name', 'permissions')
00234             for sub in child.childNodes:
00235                 if sub.nodeName == 'permission':
00236                     newsub = doc.createElement('element')
00237                     newsub.setAttribute('value', self._getNodeText(sub))
00238                     newchild.appendChild(newsub)
00239             newnode.appendChild(newchild)
00240 
00241             parent.appendChild(newnode)
00242 
00243         self._initObjects(fragment)
00244 
00245 
00246 def importActionProviders(context):
00247     """Import actions tool.
00248     """
00249     site = context.getSite()
00250     tool = getToolByName(site, 'portal_actions')
00251 
00252     importObjects(tool, '', context)
00253 
00254 def exportActionProviders(context):
00255     """Export actions tool.
00256     """
00257     site = context.getSite()
00258     tool = getToolByName(site, 'portal_actions', None)
00259     if tool is None:
00260         logger = context.getLogger('actions')
00261         logger.info('Nothing to export.')
00262         return
00263 
00264     exportObjects(tool, '', context)