Back to index

plone3  3.1.7
controlpanel.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 """ Plone control panel tool node adapters.
00014 
00015 $Id: controlpanel.py 14449 2007-04-27 18:48:57Z wichert $
00016 """
00017 
00018 from Products.GenericSetup.utils import exportObjects
00019 from Products.GenericSetup.utils import importObjects
00020 from Products.GenericSetup.utils import XMLAdapterBase
00021 
00022 from Products.CMFCore.interfaces import IActionProvider
00023 from Products.CMFCore.interfaces.portal_actions \
00024         import ActionProvider as z2IActionProvider
00025 from Products.CMFCore.utils import getToolByName
00026 
00027 from Products.CMFPlone.interfaces import IControlPanel
00028 
00029 class ControlPanelXMLAdapter(XMLAdapterBase):
00030 
00031     """
00032     XML im- and exporter for Plone control panel.  Most of this
00033     code is taken from the actions handler in CMFCore.
00034     """
00035 
00036     __used_for__ = IControlPanel
00037 
00038     _LOGGER_ID = 'controlpanel'
00039 
00040     name = 'controlpanel'
00041 
00042     def _exportNode(self):
00043         """
00044         Export the object as a DOM node.
00045         """
00046         node = self._getObjectNode('object')
00047         node.appendChild(self._extractConfiglets())
00048         self._logger.info('Control panel exported.')
00049         return node
00050 
00051     def _importNode(self, node):
00052         """
00053         Import the object from the DOM node.
00054         """
00055         self._initProvider(node)
00056         self._logger.info('Control panel imported.')
00057 
00058     def _initProvider(self, node):
00059         if self.environ.shouldPurge():
00060             actions = self.context.listActions()
00061             for action in actions:
00062                 self.context.unregisterConfiglet(action.getId())
00063 
00064         self._initConfiglets(node)
00065 
00066     def _extractConfiglets(self):
00067         fragment = self._doc.createDocumentFragment()
00068 
00069         provider = self.context
00070         if not (IActionProvider.providedBy(provider) or
00071                 z2IActionProvider.isImplementedBy(provider)):
00072             return fragment
00073 
00074         actions = provider.listActions()
00075 
00076         if actions and isinstance(actions[0], dict):
00077             return fragment
00078 
00079         for ai in actions:
00080             mapping = ai.getMapping()
00081             child = self._doc.createElement('configlet')
00082             child.setAttribute('action_id', mapping['id'])
00083             child.setAttribute('category', mapping['category'])
00084             child.setAttribute('condition_expr', mapping['condition'])
00085             child.setAttribute('title', mapping['title'])
00086             child.setAttribute('url_expr', mapping['action'])
00087             child.setAttribute('visible', str(mapping['visible']))
00088             child.setAttribute('appId', ai.getAppId())
00089             for permission in mapping['permissions']:
00090                 sub = self._doc.createElement('permission')
00091                 sub.appendChild(self._doc.createTextNode(permission))
00092                 child.appendChild(sub)
00093             fragment.appendChild(child)
00094         return fragment
00095 
00096 
00097     def _initConfiglets(self, node):
00098         controlpanel = self.context
00099         for child in node.childNodes:
00100             if child.nodeName != 'configlet':
00101                 continue
00102 
00103             action_id = str(child.getAttribute('action_id'))
00104             title = str(child.getAttribute('title'))
00105             url_expr = str(child.getAttribute('url_expr'))
00106             condition_expr = str(child.getAttribute('condition_expr'))
00107             category = str(child.getAttribute('category'))
00108             visible = str(child.getAttribute('visible'))
00109             appId = str(child.getAttribute('appId'))
00110             if visible.lower() == 'true':
00111                 visible = 1
00112             else:
00113                 visible = 0
00114 
00115             permission = ''
00116             for permNode in child.childNodes:
00117                 if permNode.nodeName == 'permission':
00118                     for textNode in permNode.childNodes:
00119                         if textNode.nodeName != '#text' or \
00120                                not textNode.nodeValue.strip():
00121                             continue
00122                         permission = str(textNode.nodeValue)
00123                         break  # only one permission is allowed
00124                     if permission:
00125                         break
00126 
00127             # Remove previous action with same id and category.
00128             controlpanel.unregisterConfiglet(action_id)
00129 
00130             controlpanel.registerConfiglet(id=action_id,
00131                                            name=title,
00132                                            action=url_expr,
00133                                            appId=appId,
00134                                            condition=condition_expr,
00135                                            category=category,
00136                                            permission=permission,
00137                                            visible=visible)
00138 
00139 
00140 def importControlPanel(context):
00141     """Import Plone control panel.
00142     """
00143     site = context.getSite()
00144     tool = getToolByName(site, 'portal_controlpanel')
00145 
00146     importObjects(tool, '', context)
00147 
00148 def exportControlPanel(context):
00149     """Export actions tool.
00150     """
00151     site = context.getSite()
00152     tool = getToolByName(site, 'portal_controlpanel', None)
00153     if tool is None:
00154         logger = context.getLogger('controlpanel')
00155         logger.info('Nothing to export.')
00156         return
00157 
00158     exportObjects(tool, '', context)