Back to index

plone3  3.1.7
ActionsTool.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 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 """ Basic action list tool.
00014 
00015 $Id: ActionsTool.py 76996 2007-06-24 00:18:49Z hannosch $
00016 """
00017 
00018 from AccessControl import ClassSecurityInfo
00019 from Globals import DTMLFile
00020 from Globals import InitializeClass
00021 from OFS.ObjectManager import IFAwareObjectManager
00022 from OFS.OrderedFolder import OrderedFolder
00023 from zope.interface import implements
00024 
00025 from ActionProviderBase import ActionProviderBase
00026 from interfaces import IActionCategory
00027 from interfaces import IActionProvider
00028 from interfaces import IActionsTool
00029 from interfaces.portal_actions import ActionProvider as z2IActionProvider
00030 from interfaces.portal_actions import portal_actions as z2IActionsTool
00031 from permissions import ManagePortal
00032 from utils import _dtmldir
00033 from utils import UniqueObject
00034 
00035 
00036 class ActionsTool(UniqueObject, IFAwareObjectManager, OrderedFolder,
00037                   ActionProviderBase):
00038 
00039     """
00040         Weave together the various sources of "actions" which are apropos
00041         to the current user and context.
00042     """
00043     # XXX: this class violates the rules for tools/utilities:
00044     # ActionProviderBase depends implicitly on REQUEST
00045 
00046     implements(IActionsTool)
00047     __implements__ = (z2IActionsTool, OrderedFolder.__implements__,
00048                       ActionProviderBase.__implements__)
00049 
00050     id = 'portal_actions'
00051     meta_type = 'CMF Actions Tool'
00052     _product_interfaces = (IActionCategory,)
00053     action_providers = ('portal_types', 'portal_workflow', 'portal_actions')
00054 
00055     security = ClassSecurityInfo()
00056 
00057     manage_options = ( ( OrderedFolder.manage_options[0],
00058                          ActionProviderBase.manage_options[0],
00059                          {'label': 'Action Providers',
00060                           'action': 'manage_actionProviders'},
00061                          {'label': 'Overview',
00062                           'action': 'manage_overview'} ) +
00063                        OrderedFolder.manage_options[2:] )
00064 
00065     #
00066     #   ZMI methods
00067     #
00068     security.declareProtected(ManagePortal, 'manage_overview')
00069     manage_overview = DTMLFile( 'explainActionsTool', _dtmldir )
00070     manage_actionProviders = DTMLFile('manageActionProviders', _dtmldir)
00071 
00072     security.declareProtected(ManagePortal, 'manage_aproviders')
00073     def manage_aproviders(self
00074                         , apname=''
00075                         , chosen=()
00076                         , add_provider=0
00077                         , del_provider=0
00078                         , REQUEST=None):
00079         """
00080         Manage action providers through-the-web.
00081         """
00082         providers = list(self.listActionProviders())
00083         new_providers = []
00084         if add_provider:
00085             providers.append(apname)
00086         elif del_provider:
00087             for item in providers:
00088                 if item not in chosen:
00089                     new_providers.append(item)
00090             providers = new_providers
00091         self.action_providers = tuple(providers)
00092         if REQUEST is not None:
00093             return self.manage_actionProviders(self , REQUEST
00094                           , manage_tabs_message='Providers changed.')
00095 
00096     security.declareProtected( ManagePortal, 'manage_editActionsForm' )
00097     def manage_editActionsForm( self, REQUEST, manage_tabs_message=None ):
00098         """ Show the 'Actions' management tab.
00099         """
00100         actions = [ ai.getMapping() for ai in self._actions ]
00101 
00102         # possible_permissions is in AccessControl.Role.RoleManager.
00103         pp = self.possible_permissions()
00104         return self._actions_form( self
00105                                  , REQUEST
00106                                  , actions=actions
00107                                  , possible_permissions=pp
00108                                  , management_view='Actions'
00109                                  , manage_tabs_message=manage_tabs_message
00110                                  )
00111 
00112     #
00113     #   ActionProvider interface
00114     #
00115     security.declarePrivate('listActions')
00116     def listActions(self, info=None, object=None):
00117         """ List all the actions defined by a provider.
00118         """
00119         actions = list(self._actions)
00120         for category in self.objectValues():
00121             actions.extend( category.listActions() )
00122         return tuple(actions)
00123 
00124     #
00125     #   Programmatically manipulate the list of action providers
00126     #
00127     security.declareProtected(ManagePortal, 'listActionProviders')
00128     def listActionProviders(self):
00129         """ List the ids of all Action Providers queried by this tool.
00130         """
00131         return self.action_providers
00132 
00133     security.declareProtected(ManagePortal, 'addActionProvider')
00134     def addActionProvider( self, provider_name ):
00135         """ Add an Action Provider id to the providers queried by this tool.
00136         """
00137         ap = list( self.action_providers )
00138         if hasattr( self, provider_name ) and provider_name not in ap:
00139             ap.append( provider_name )
00140             self.action_providers = tuple( ap )
00141 
00142     security.declareProtected(ManagePortal, 'deleteActionProvider')
00143     def deleteActionProvider( self, provider_name ):
00144         """ Delete an Action Provider id from providers queried by this tool.
00145         """
00146         ap = list( self.action_providers )
00147         if provider_name in ap:
00148             ap.remove( provider_name )
00149             self.action_providers = tuple( ap )
00150 
00151     #
00152     #   'portal_actions' interface methods
00153     #
00154     security.declarePublic('listFilteredActionsFor')
00155     def listFilteredActionsFor(self, object=None):
00156         """ List all actions available to the user.
00157         """
00158         actions = []
00159 
00160         # Include actions from specific tools.
00161         for provider_name in self.listActionProviders():
00162             provider = getattr(self, provider_name)
00163             if IActionProvider.providedBy(provider) or \
00164                     z2IActionProvider.isImplementedBy(provider):
00165                 actions.extend( provider.listActionInfos(object=object) )
00166 
00167         # Include actions from object.
00168         if object is not None:
00169             if IActionProvider.providedBy(object) or \
00170                     z2IActionProvider.isImplementedBy(object):
00171                 actions.extend( object.listActionInfos(object=object) )
00172 
00173         # Reorganize the actions by category.
00174         filtered_actions={'user':[],
00175                           'folder':[],
00176                           'object':[],
00177                           'global':[],
00178                           'workflow':[],
00179                           }
00180 
00181         for action in actions:
00182             catlist = filtered_actions.setdefault(action['category'], [])
00183             catlist.append(action)
00184 
00185         return filtered_actions
00186 
00187 InitializeClass(ActionsTool)