Back to index

plone3  3.1.7
WorkflowUIMixin.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 """ Web-configurable workflow UI.
00014 
00015 $Id: WorkflowUIMixin.py 77182 2007-06-28 17:25:27Z yuppie $
00016 """
00017 
00018 import os
00019 
00020 from Globals import DTMLFile
00021 from Globals import InitializeClass
00022 from AccessControl import ClassSecurityInfo
00023 from AccessControl.requestmethod import postonly
00024 from Acquisition import aq_get
00025 
00026 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00027 
00028 from permissions import ManagePortal
00029 from Guard import Guard
00030 from utils import _dtmldir
00031 
00032 try:
00033     #
00034     #   XXX: 2004/04/28  This factoring *has* to go;  if necessary,
00035     #         this module could have a hook function, which the dependent
00036     #         module could replace.
00037     #
00038 
00039     # If base_cms exists, include the roles it defines.
00040     from Products.base_cms.permissions import getDefaultRolePermissionMap
00041 except ImportError:
00042     def getDefaultRolePermissionMap():
00043         return {}
00044 
00045 
00046 class WorkflowUIMixin:
00047     '''
00048     '''
00049 
00050     security = ClassSecurityInfo()
00051 
00052     security.declareProtected(ManagePortal, 'manage_properties')
00053     manage_properties = DTMLFile('workflow_properties', _dtmldir)
00054     manage_groups = PageTemplateFile('workflow_groups.pt', _dtmldir)
00055 
00056     security.declareProtected(ManagePortal, 'setProperties')
00057     @postonly
00058     def setProperties(self, title, manager_bypass=0, props=None, 
00059                       REQUEST=None, description=''):
00060         """Sets basic properties.
00061         """
00062         self.title = str(title)
00063         self.description = str(description)
00064         self.manager_bypass = manager_bypass and 1 or 0
00065         g = Guard()
00066         if g.changeFromProperties(props or REQUEST):
00067             self.creation_guard = g
00068         else:
00069             self.creation_guard = None
00070         if REQUEST is not None:
00071             return self.manage_properties(
00072                 REQUEST, manage_tabs_message='Properties changed.')
00073 
00074     _permissions_form = DTMLFile('workflow_permissions', _dtmldir)
00075 
00076     security.declareProtected(ManagePortal, 'manage_permissions')
00077     def manage_permissions(self, REQUEST, manage_tabs_message=None):
00078         """Displays the form for choosing which permissions to manage.
00079         """
00080         return self._permissions_form(REQUEST,
00081                                       management_view='Permissions',
00082                                       manage_tabs_message=manage_tabs_message,
00083                                       )
00084 
00085     security.declareProtected(ManagePortal, 'addManagedPermission')
00086     @postonly
00087     def addManagedPermission(self, p, REQUEST=None):
00088         """Adds to the list of permissions to manage.
00089         """
00090         if p in self.permissions:
00091             raise ValueError, 'Already a managed permission: ' + p
00092         if REQUEST is not None and p not in self.getPossiblePermissions():
00093             raise ValueError, 'Not a valid permission name:' + p
00094         self.permissions = self.permissions + (p,)
00095         if REQUEST is not None:
00096             return self.manage_permissions(
00097                 REQUEST, manage_tabs_message='Permission added.')
00098 
00099     security.declareProtected(ManagePortal, 'delManagedPermissions')
00100     @postonly
00101     def delManagedPermissions(self, ps, REQUEST=None):
00102         """Removes from the list of permissions to manage.
00103         """
00104         if ps:
00105             l = list(self.permissions)
00106             for p in ps:
00107                 l.remove(p)
00108             self.permissions = tuple(l)
00109         if REQUEST is not None:
00110             return self.manage_permissions(
00111                 REQUEST, manage_tabs_message='Permission(s) removed.')
00112 
00113     security.declareProtected(ManagePortal, 'getPossiblePermissions')
00114     def getPossiblePermissions(self):
00115         """Returns the list of all permissions that can be managed.
00116         """
00117         # possible_permissions is in AccessControl.Role.RoleManager.
00118         return list(self.possible_permissions())
00119 
00120     security.declareProtected(ManagePortal, 'getGroups')
00121     def getGroups(self):
00122         """Returns the names of groups managed by this workflow.
00123         """
00124         return tuple(self.groups)
00125 
00126     security.declareProtected(ManagePortal, 'getAvailableGroups')
00127     def getAvailableGroups(self):
00128         """Returns a list of available group names.
00129         """
00130         gf = aq_get( self, '__allow_groups__', None, 1 )
00131         if gf is None:
00132             return ()
00133         try:
00134             groups = gf.searchGroups()
00135         except AttributeError:
00136             return ()
00137         else:
00138             return [g['id'] for g in groups]
00139 
00140     security.declareProtected(ManagePortal, 'addGroup')
00141     @postonly
00142     def addGroup(self, group, RESPONSE=None, REQUEST=None):
00143         """Adds a group by name.
00144         """
00145         if group not in self.getAvailableGroups():
00146             raise ValueError(group)
00147         self.groups = self.groups + (group,)
00148         if RESPONSE is not None:
00149             RESPONSE.redirect(
00150                 "%s/manage_groups?manage_tabs_message=Added+group."
00151                 % self.absolute_url())
00152 
00153     security.declareProtected(ManagePortal, 'delGroups')
00154     @postonly
00155     def delGroups(self, groups, RESPONSE=None, REQUEST=None):
00156         """Removes groups by name.
00157         """
00158         self.groups = tuple([g for g in self.groups if g not in groups])
00159         if RESPONSE is not None:
00160             RESPONSE.redirect(
00161                 "%s/manage_groups?manage_tabs_message=Groups+removed."
00162                 % self.absolute_url())
00163 
00164     security.declareProtected(ManagePortal, 'getAvailableRoles')
00165     def getAvailableRoles(self):
00166         """Returns the acquired roles mixed with base_cms roles.
00167         """
00168         roles = list(self.valid_roles())
00169         for role in getDefaultRolePermissionMap().keys():
00170             if role not in roles:
00171                 roles.append(role)
00172         roles.sort()
00173         return roles
00174 
00175     security.declareProtected(ManagePortal, 'getRoles')
00176     def getRoles(self):
00177         """Returns the list of roles managed by this workflow.
00178         """
00179         roles = self.roles
00180         if roles is not None:
00181             return roles
00182         roles = getDefaultRolePermissionMap().keys()
00183         if roles:
00184             # Map the base_cms roles by default.
00185             roles.sort()
00186             return roles
00187         return self.valid_roles()
00188 
00189     security.declareProtected(ManagePortal, 'setRoles')
00190     @postonly
00191     def setRoles(self, roles, RESPONSE=None, REQUEST=None):
00192         """Changes the list of roles mapped to groups by this workflow.
00193         """
00194         avail = self.getAvailableRoles()
00195         for role in roles:
00196             if role not in avail:
00197                 raise ValueError(role)
00198         self.roles = tuple(roles)
00199         if RESPONSE is not None:
00200             RESPONSE.redirect(
00201                 "%s/manage_groups?manage_tabs_message=Roles+changed."
00202                 % self.absolute_url())
00203 
00204     security.declareProtected(ManagePortal, 'getGuard')
00205     def getGuard(self):
00206         """Returns the initiation guard.
00207 
00208         If no init guard has been created, returns a temporary object.
00209         """
00210         if self.creation_guard is not None:
00211             return self.creation_guard
00212         else:
00213             return Guard().__of__(self)  # Create a temporary guard.
00214 
00215     security.declarePublic('guardExprDocs')
00216     def guardExprDocs(self):
00217         """Returns documentation on guard expressions.
00218         """
00219         here = os.path.dirname(__file__)
00220         fn = os.path.join(here, 'doc', 'expressions.stx')
00221         f = open(fn, 'rt')
00222         try:
00223             text = f.read()
00224         finally:
00225             f.close()
00226         from DocumentTemplate.DT_Var import structured_text
00227         return structured_text(text)
00228 
00229 InitializeClass(WorkflowUIMixin)