File: DefaultWorkflowPolicy.py

package info (click to toggle)
zope-cmfplacefulworkflow 1.0.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 348 kB
  • ctags: 195
  • sloc: python: 1,139; makefile: 31; sh: 2
file content (303 lines) | stat: -rw-r--r-- 10,519 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# -*- coding: utf-8 -*-
## CMFPlacefulWorkflow
## Copyright (C)2005 Ingeniweb

## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.

## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.

## You should have received a copy of the GNU General Public License
## along with this program; see the file COPYING. If not, write to the
## Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
"""
A simple workflow policy.
"""
__version__ = "$Revision: 25624 $"
# $Source: /cvsroot/ingeniweb/CMFPlacefulWorkflow/DefaultWorkflowPolicy.py,v $
# $Id: DefaultWorkflowPolicy.py 25624 2006-06-30 21:07:33Z encolpe $
__docformat__ = 'restructuredtext'

from AccessControl import ClassSecurityInfo
from Globals import InitializeClass, PersistentMapping, DTMLFile
from Acquisition import aq_base

from Products.CMFCore.utils import SimpleItemWithProperties
from Products.CMFPlacefulWorkflow.PlacefulWorkflowTool import addWorkflowPolicyFactory

from Products.CMFCore.permissions import ManagePortal

from Products.CMFPlacefulWorkflow.interfaces.portal_placeful_workflow \
        import WorkflowPolicyDefinition as IWorkflowPolicyDefinition

from Globals import package_home
from os import path as os_path
_dtmldir = os_path.join( package_home( globals() ), 'dtml' )

from Products.CMFCore.utils import getToolByName

DEFAULT_CHAIN = '(Default)'
MARKER = '_MARKER'

class DefaultWorkflowPolicyDefinition (SimpleItemWithProperties):

    __implements__ = IWorkflowPolicyDefinition

    meta_type = 'WorkflowPolicy'
    id = 'default_workflow_policy'
    _isAWorkflowPolicy = 1

    _chains_by_type = None  # PersistentMapping
    _default_chain = None # Fallback to wf tool

    security = ClassSecurityInfo()

    manage_options = ( { 'label' : 'Workflows'
                       , 'action' : 'manage_selectWorkflows'
                       }
                     , { 'label' : 'Overview', 'action' : 'manage_overview' }
                     )

    #
    #   ZMI methods
    #
    security.declareProtected( ManagePortal, 'manage_overview' )
    manage_overview = DTMLFile( 'explainWorkflowPolicy', _dtmldir )

    def __init__(self, id):
        self.id = id
        self.title = ''
        self.description = ''

    _manage_defineLocalWorkflowPolicy = DTMLFile('defineLocalWorkflowPolicy', _dtmldir)

    security.declareProtected( ManagePortal, 'getId')
    def getId(self):
        """ Return the id
        """
        return self.id

    security.declareProtected( ManagePortal, 'getTitle')
    def getTitle(self):
        """ Return the title
        """
        title = getattr(self, 'title', '')
        return title

    security.declareProtected( ManagePortal, 'getDescription')
    def getDescription(self):
        """ Return the description
        """
        description = getattr(self, 'description', '')
        return description

    security.declareProtected( ManagePortal, 'setTitle')
    def setTitle(self, title):
        """ Set the title
        """
        self.title=title

    security.declareProtected( ManagePortal, 'setDescription')
    def setDescription(self, description):
        """ Set the description
        """
        self.description = description

    security.declareProtected( ManagePortal, 'manage_selectWorkflows')
    def manage_selectWorkflows(self, REQUEST, manage_tabs_message=None):
        """ Show a management screen for changing type to workflow connections.
        """
        cbt = self._chains_by_type
        ti = self._listTypeInfo()
        types_info = []
        for t in ti:
            id = t.getId()
            title = t.Title()
            if title == id:
                title = None
            if cbt is not None and cbt.has_key(id):
                chain = ', '.join(cbt[id])
            else:
                chain = DEFAULT_CHAIN
            types_info.append({'id': id,
                               'title': title,
                               'chain': chain})
        return self._manage_defineLocalWorkflowPolicy(
            REQUEST,
            default_chain=', '.join(self._default_chain or ()),
            types_info=types_info,
            management_view='Workflows',
            manage_tabs_message=manage_tabs_message)


    security.declareProtected( ManagePortal, 'manage_changeWorkflows')
    def manage_changeWorkflows(self, title, description, default_chain, props=None, REQUEST=None):
        """ Changes which workflows apply to objects of which type.
        """
        self.title = title
        self.description = description

        wf_tool = getToolByName(self, 'portal_workflow')

        if props is None:
            props = REQUEST
        cbt = self._chains_by_type
        if cbt is None:
            self._chains_by_type = cbt = PersistentMapping()
        ti = self._listTypeInfo()
        # Set up the chains by type.
        for t in ti:
            id = t.getId()
            field_name = 'chain_%s' % id
            chain = props.get(field_name, DEFAULT_CHAIN).strip()
            if chain == DEFAULT_CHAIN:
                # Remove from cbt.
                if cbt.has_key(id):
                    del cbt[id]
            else:
                chain = chain.replace(',', ' ')
                ids = []
                for wf_id in chain.split(' '):
                    if wf_id:
                        if not wf_tool.getWorkflowById(wf_id):
                            raise ValueError, (
                                '"%s" is not a workflow ID.' % wf_id)
                        ids.append(wf_id)
                cbt[id] = tuple(ids)
        # Set up the default chain.
        default_chain = default_chain.replace(',', ' ')
        ids = []
        for wf_id in default_chain.split(' '):
            if wf_id:
                if not wf_tool.getWorkflowById(wf_id):
                    raise ValueError, (
                        '"%s" is not a workflow ID.' % wf_id)
                ids.append(wf_id)
        self._default_chain = tuple(ids)
        if REQUEST is not None:
            return self.manage_selectWorkflows(REQUEST,
                            manage_tabs_message='Changed.')

    security.declareProtected( ManagePortal, 'setChainForPortalTypes')
    def setChainForPortalTypes(self, pt_names, chain):
        """ Set a chain for portal types.
        """
        for portal_type in pt_names:
            self.setChain(portal_type, chain)

    security.declareProtected( ManagePortal, 'getChainFor')
    def getChainFor(self, ob, managescreen=False):
        """Returns the chain that applies to the object.

        If chain doesn't exist we return None to get a fallback from portal_workflow.
        We never return emtpy tuple that is good value for a chain.
        """

        cbt = self._chains_by_type
        if type(ob) == type(''):
            pt = ob
        elif hasattr(aq_base(ob), '_getPortalTypeName'):
            pt = ob._getPortalTypeName()
        else:
            pt = None

        if pt is None:
            return None

        chain = None
        if cbt is not None:
            chain = cbt.get(pt, MARKER)

        if chain is MARKER or chain is None:
            return None
        elif len(chain) == 1 and chain[0] == DEFAULT_CHAIN:
            default = self.getDefaultChain(ob)
            if default:
                if managescreen:
                    return chain[0]
                else:
                    return default
            else:
                return None

        return chain

    security.declareProtected( ManagePortal, 'setDefaultChain')
    def setDefaultChain(self, default_chain):

        """ Sets the default chain for this tool. """
        wftool = getToolByName(self, 'portal_workflow')
        ids = []
        for wf_id in default_chain:
            if wf_id:
                if not wftool.getWorkflowById(wf_id):
                    raise ValueError, ( "'%s' is not a workflow ID." % wf_id)
                ids.append(wf_id)

        self._default_chain = tuple(ids)

    security.declareProtected( ManagePortal, 'getDefaultChain')
    def getDefaultChain(self, ob):
        """ Returns the default chain."""
        if self._default_chain is None:
            wf_tool = getToolByName(self, 'portal_workflow')
            return wf_tool.getDefaultChainFor(ob)
        else:
            return self._default_chain

    security.declareProtected( ManagePortal, 'setChain')
    def setChain(self, portal_type, chain):
        """Set the chain for a portal type."""
        # Verify input data
        if portal_type not in [pt.id for pt in self._listTypeInfo()]:
            raise ValueError, ("'%s' is not a valid portal type." % portal_type)

        if type(chain) is type(''):
            chain = map( lambda x: x.strip(), chain.split(',') )

        wftool = getToolByName(self, 'portal_workflow')
        cbt = self._chains_by_type
        if cbt is None:
            self._chains_by_type = cbt = PersistentMapping()

        # if chain is None or default, we remove the entry
        if chain is None and cbt.has_key(portal_type):
            del cbt[portal_type]
        elif len(chain) == 1 and chain[0] == DEFAULT_CHAIN:
            cbt[portal_type] = chain
        else:
            for wf_id in chain:
                if wf_id != '' and not wftool.getWorkflowById(wf_id):
                    raise ValueError, ("'%s' is not a workflow ID." % wf_id)
            cbt[portal_type] = tuple(chain)

    security.declareProtected( ManagePortal, 'delChain')
    def delChain(self, portal_type):
        """Delete the chain for a portal type."""
        if self._chains_by_type.has_key(portal_type):
            del self._chains_by_type[portal_type]

    #
    #   Helper methods
    #
    security.declarePrivate( '_listTypeInfo' )
    def _listTypeInfo(self):

        """ List the portal types which are available.
        """
        pt = getToolByName(self, 'portal_types', None)
        if pt is None:
            return ()
        else:
            return pt.listTypeInfo()


InitializeClass(DefaultWorkflowPolicyDefinition)

addWorkflowPolicyFactory(DefaultWorkflowPolicyDefinition, title='Simple Policy')