Back to index

plone3  3.1.7
ActionsTool.py
Go to the documentation of this file.
00001 from AccessControl import ClassSecurityInfo
00002 from Globals import InitializeClass
00003 from Products.CMFCore.ActionInformation import ActionInfo
00004 from Products.CMFCore.ActionsTool import ActionsTool as BaseTool
00005 from Products.CMFCore.interfaces import IActionProvider
00006 from Products.CMFPlone import ToolNames
00007 from Products.CMFPlone.PloneBaseTool import PloneBaseTool
00008 
00009 
00010 class ActionsTool(PloneBaseTool, BaseTool):
00011 
00012     meta_type = ToolNames.ActionsTool
00013     toolicon = 'skins/plone_images/confirm_icon.gif'
00014 
00015     __implements__ = (PloneBaseTool.__implements__, BaseTool.__implements__, )
00016 
00017     security = ClassSecurityInfo()
00018 
00019     #
00020     #   ActionProvider interface
00021     #
00022     security.declarePrivate('listActions')
00023     def listActions(self, info=None, object=None,
00024                     categories=None, ignore_categories=None):
00025         """ List all the actions defined by a provider.
00026         """
00027         actions = list(self._actions)
00028 
00029         if ignore_categories is None:
00030             ignore_categories = ()
00031 
00032         if categories is None:
00033             categories = [cat for cat in self.objectIds()
00034                               if cat not in ignore_categories]
00035         else:
00036             categories = [cat for cat in self.objectIds()
00037                               if cat in categories]
00038 
00039         for category in categories:
00040             actions.extend(self[category].listActions())
00041         return tuple(actions)
00042 
00043     security.declarePublic('listActionInfos')
00044     def listActionInfos(self, action_chain=None, object=None,
00045                         check_visibility=1, check_permissions=1,
00046                         check_condition=1, max=-1,
00047                         categories=None, ignore_categories=None):
00048         # List ActionInfo objects.
00049         # (method is without docstring to disable publishing)
00050         #
00051         ec = self._getExprContext(object)
00052         actions = self.listActions(object=object,
00053                                    categories=categories,
00054                                    ignore_categories=ignore_categories)
00055         actions = [ ActionInfo(action, ec) for action in actions ]
00056 
00057         if action_chain:
00058             filtered_actions = []
00059             if isinstance(action_chain, basestring):
00060                 action_chain = (action_chain,)
00061             for action_ident in action_chain:
00062                 sep = action_ident.rfind('/')
00063                 category, id = action_ident[:sep], action_ident[sep+1:]
00064                 for ai in actions:
00065                     if id == ai['id'] and category == ai['category']:
00066                         filtered_actions.append(ai)
00067             actions = filtered_actions
00068 
00069         if categories is not None:
00070             actions = [ai for ai in actions
00071                           if ai['category'] in categories]
00072 
00073         if ignore_categories is not None:
00074             actions = [ai for ai in actions
00075                           if ai['category'] not in ignore_categories]
00076 
00077         action_infos = []
00078         for ai in actions:
00079             if check_visibility and not ai['visible']:
00080                 continue
00081             if check_permissions and not ai['allowed']:
00082                 continue
00083             if check_condition and not ai['available']:
00084                 continue
00085             action_infos.append(ai)
00086             if max + 1 and len(action_infos) >= max:
00087                 break
00088         return action_infos
00089 
00090     #
00091     #   'portal_actions' interface methods
00092     #
00093     security.declarePublic('listFilteredActionsFor')
00094     def listFilteredActionsFor(self, object=None,
00095                                ignore_providers=(),
00096                                ignore_categories=None):
00097         """ List all actions available to the user.
00098         """
00099         actions = []
00100 
00101         providers = [name for name in self.listActionProviders()
00102                           if name not in ignore_providers]
00103 
00104         # Include actions from specific tools.
00105         for provider_name in providers:
00106             provider = getattr(self, provider_name, None)
00107             # Skip missing action providers.
00108             if provider is None:
00109                 continue
00110             if IActionProvider.providedBy(provider):
00111                 if provider_name == 'portal_actions':
00112                     actions.extend(provider.listActionInfos(
00113                                    object=object,
00114                                    ignore_categories=ignore_categories
00115                                    ))
00116                 else:
00117                     actions.extend(provider.listActionInfos(object=object))
00118 
00119         # Include actions from object.
00120         if object is not None:
00121             if IActionProvider.providedBy(object):
00122                 actions.extend(object.listActionInfos(object=object))
00123 
00124         # Reorganize the actions by category.
00125         filtered_actions={'user':[],
00126                           'folder':[],
00127                           'object':[],
00128                           'global':[],
00129                           'workflow':[],
00130                           }
00131 
00132         for action in actions:
00133             catlist = filtered_actions.setdefault(action['category'], [])
00134             catlist.append(action)
00135 
00136         return filtered_actions
00137 
00138 InitializeClass(ActionsTool)