Back to index

plone3  3.1.7
PloneControlPanel.py
Go to the documentation of this file.
00001 from Globals import DTMLFile
00002 from Globals import InitializeClass
00003 from AccessControl import ClassSecurityInfo
00004 from OFS.Folder import Folder
00005 from OFS.SimpleItem import SimpleItem
00006 from OFS.PropertyManager import PropertyManager
00007 
00008 from zope.interface import implements
00009 from zope.i18n import translate
00010 
00011 from Products.CMFCore.Expression import Expression, createExprContext
00012 from Products.CMFCore.ActionInformation import ActionInformation
00013 from Products.CMFCore.ActionProviderBase import ActionProviderBase
00014 from Products.CMFCore.permissions import ManagePortal, View
00015 from Products.CMFCore.utils import _checkPermission, getToolByName, UniqueObject
00016 from Products.CMFCore.utils import registerToolInterface
00017 
00018 import ToolNames
00019 from interfaces.PloneControlPanel import IControlPanel as z2IControlPanel
00020 from interfaces import IControlPanel
00021 from Products.CMFPlone.PloneBaseTool import PloneBaseTool
00022 
00023 class PloneConfiglet(ActionInformation):
00024 
00025     def __init__(self, appId, **kwargs):
00026         self.appId=appId
00027         ActionInformation.__init__(self,**kwargs)
00028 
00029     def getAppId(self):
00030         return self.appId
00031 
00032     def getDescription(self):
00033         return self.description
00034 
00035     def clone(self):
00036         return self.__class__(**self.__dict__)
00037 
00038     def getAction(self,ec):
00039         res=ActionInformation.getAction(self,ec)
00040         res['description']=self.getDescription()
00041         return res
00042 
00043 class PloneControlPanel(PloneBaseTool, UniqueObject,
00044                         Folder, ActionProviderBase, PropertyManager):
00045     """Weave together the various sources of "actions" which
00046     are apropos to the current user and context.
00047     """
00048 
00049     __implements__ = (PloneBaseTool.__implements__,
00050                       z2IControlPanel,
00051                       ActionProviderBase.__implements__,
00052                       SimpleItem.__implements__, )
00053 
00054     implements(IControlPanel)
00055 
00056     security = ClassSecurityInfo()
00057 
00058     id = 'portal_controlpanel'
00059     title = 'Control Panel'
00060     toolicon = 'skins/plone_images/site_icon.gif'
00061     meta_type = ToolNames.ControlPanelTool
00062     _actions_form = DTMLFile( 'www/editPloneConfiglets', globals() )
00063 
00064     _properties=(
00065         {'id':'groups','type':'lines'},
00066     )
00067 
00068     manage_options = (ActionProviderBase.manage_options +
00069                       PropertyManager.manage_options)
00070 
00071     # TODO this is still used but should be handled by the GS profile
00072     groups = ['member|Member|My Preferences',
00073               'site|Plone|Plone Configuration',
00074               'site|Products|Add-on Product Configuration']
00075 
00076 
00077     def __init__(self, **kw):
00078         if kw:
00079             self.__dict__.update(**kw)
00080         if not self.__dict__.has_key('groups'):
00081             self.__dict__['groups'] = self.groups
00082 
00083     security.declareProtected( ManagePortal, 'registerConfiglets' )
00084     def registerConfiglets(self,configlets):
00085         """ ATTENTION: must be called AFTER portal_actionicons
00086         is installed
00087         """
00088         for conf in configlets:
00089             self.registerConfiglet(**conf)
00090 
00091     security.declareProtected( ManagePortal, 'getGroupIds' )
00092     def getGroupIds(self, category=''):
00093         return [g.split('|')[1] for g in self.groups
00094                 if category=='' or g.split('|')[0]==category]
00095 
00096     security.declareProtected( View, 'getGroups' )
00097     def getGroups(self,category=''):
00098         return [{'id':g.split('|')[1], 'title':g.split('|')[2]}
00099                 for g in self.groups
00100                 if category=='' or g.split('|')[0]==category]
00101 
00102     security.declarePublic( 'enumConfiglets' )
00103     def enumConfiglets(self, group=None):
00104         portal=getToolByName(self, 'portal_url').getPortalObject()
00105         mtool = getToolByName(self, 'portal_membership')
00106         context=createExprContext(self, portal, self)
00107         res = []
00108         for a in self.listActions():
00109             verified = 0
00110             for permission in a.permissions:
00111                 if _checkPermission(permission, portal):
00112                     verified = 1
00113             if verified and a.category==group and a.testCondition(context):
00114                 res.append(a.getAction(context))
00115         # Translate the title for sorting
00116         if getattr(self, 'REQUEST', None) is not None:
00117             for a in res:
00118                 a['title'] = translate(a['title'],
00119                                        domain='plone',
00120                                        context=self.REQUEST)
00121         def _title(v):
00122             return v['title']
00123         res.sort(key=_title)
00124         return res
00125 
00126     security.declareProtected( ManagePortal, 'unregisterConfiglet' )
00127     def unregisterConfiglet(self,id):
00128         actids= [o.id for o in self.listActions()]
00129         selection=[actids.index(a) for a in actids if a==id]
00130         self.deleteActions(selection)
00131 
00132         actionicons=getToolByName(self,'portal_actionicons')
00133         if actionicons.queryActionInfo('controlpanel', id, None):
00134             actionicons.removeActionIcon('controlpanel', id)
00135 
00136 
00137     security.declareProtected( ManagePortal, 'unregisterApplication' )
00138     def unregisterApplication(self,appId):
00139         acts=list(self.listActions())
00140         selection=[acts.index(a) for a in acts if a.appId==appId]
00141         self.deleteActions(selection)
00142 
00143         actionicons=getToolByName(self,'portal_actionicons')
00144         for a in acts:
00145             if (a.appId == appId and
00146                 actionicons.queryActionInfo('controlpanel', a.id, None)):
00147                 actionicons.removeActionIcon('controlpanel', a.id)
00148 
00149 
00150     def _extractAction( self, properties, index ):
00151         """ Extract an ActionInformation from the funky form properties.
00152         """
00153         id          = str( properties.get( 'id_%d'          % index, '' ) )
00154         name        = str( properties.get( 'name_%d'        % index, '' ) )
00155         action      = str( properties.get( 'action_%d'      % index, '' ) )
00156         condition   = str( properties.get( 'condition_%d'   % index, '' ) )
00157         category    = str( properties.get( 'category_%d'    % index, '' ))
00158         visible     =      properties.get( 'visible_%d'     % index, 0  )
00159         permissions =      properties.get( 'permission_%d'  % index, () )
00160         appId       =      properties.get( 'appId_%d'  % index, '' )
00161         description =      properties.get( 'description_%d'  % index, '' )
00162 
00163         if not name:
00164             raise ValueError('A name is required.')
00165 
00166         if action is not '':
00167             action = Expression( text=action )
00168 
00169         if condition is not '':
00170             condition = Expression( text=condition )
00171 
00172         if category == '':
00173             category = 'object'
00174 
00175         if type( visible ) is not type( 0 ):
00176             try:
00177                 visible = int( visible )
00178             except ValueError:
00179                 visible = 0
00180 
00181         if type( permissions ) is type( '' ):
00182             permissions = ( permissions, )
00183 
00184         return PloneConfiglet( id=id
00185                                 , title=name
00186                                 , action=action
00187                                 , condition=condition
00188                                 , permissions=permissions
00189                                 , category=category
00190                                 , visible=visible
00191                                 , appId = appId
00192                                 , description = description
00193                                 )
00194     security.declareProtected( ManagePortal, 'addAction' )
00195     def addAction( self
00196                  , id
00197                  , name
00198                  , action
00199                  , condition=''
00200                  , permission=''
00201                  , category='Plone'
00202                  , visible=1
00203                  , appId=None
00204                  , imageUrl=None
00205                  , description=''
00206                  , REQUEST=None
00207                  ):
00208         """ Add an action to our list.
00209             attention: must be called AFTER portal_actionicons is installed
00210         """
00211         if not name:
00212             raise ValueError('A name is required.')
00213 
00214         a_expr = action and Expression(text=str(action)) or ''
00215         c_expr = condition and Expression(text=str(condition)) or ''
00216 
00217         if type( permission ) != type( () ):
00218             permission = permission and (str(permission),) or ()
00219 
00220         new_actions = self._cloneActions()
00221 
00222         new_action = PloneConfiglet( id=str(id)
00223                                       , title=str(name)
00224                                       , action=a_expr
00225                                       , condition=c_expr
00226                                       , permissions=permission
00227                                       , category=str(category)
00228                                       , visible=int(visible)
00229                                       , appId=appId
00230                                       , description=description
00231                                       )
00232 
00233         new_actions.append( new_action )
00234         self._actions = tuple( new_actions )
00235 
00236         if imageUrl:
00237             actionicons=getToolByName(self,'portal_actionicons')
00238             actionicons.addActionIcon('controlpanel', new_action.id,
00239                                       imageUrl, new_action.title)
00240 
00241 
00242         if REQUEST is not None:
00243             return self.manage_editActionsForm(
00244                 REQUEST, manage_tabs_message='Added.')
00245 
00246     registerConfiglet=addAction
00247 
00248     security.declareProtected( ManagePortal, 'manage_editActionsForm' )
00249     def manage_editActionsForm( self, REQUEST, manage_tabs_message=None ):
00250         """ Show the 'Actions' management tab.
00251         """
00252         actions = []
00253 
00254         for a in self.listActions():
00255 
00256             a1 = {}
00257             a1['id'] = a.getId()
00258             a1['name'] = a.Title()
00259             p = a.getPermissions()
00260             if p:
00261                 a1['permission'] = p[0]
00262             else:
00263                 a1['permission'] = ''
00264             a1['category'] = a.getCategory() or 'object'
00265             a1['visible'] = a.getVisibility()
00266             a1['action'] = a.getActionExpression()
00267             a1['condition'] = a.getCondition()
00268             a1['appId'] = a.getAppId()
00269             a1['description']=a.getDescription()
00270             actions.append(a1)
00271 
00272         # possible_permissions is in AccessControl.Role.RoleManager.
00273         pp = self.possible_permissions()
00274         return self._actions_form( self
00275                                  , REQUEST
00276                                  , actions=actions
00277                                  , possible_permissions=pp
00278                                  , management_view='Actions'
00279                                  , manage_tabs_message=manage_tabs_message
00280                                  )
00281 
00282 InitializeClass(PloneControlPanel)
00283 registerToolInterface('portal_controlpanel', IControlPanel)