Skip to content

Commit 2397c1a

Browse files
author
Vo Minh Thu
committed
[IMP] workflows: added docstring, some variable renaming (to wkf_expr).
bzr revid: [email protected]
1 parent b506876 commit 2397c1a

File tree

1 file changed

+52
-22
lines changed

1 file changed

+52
-22
lines changed

openerp/workflow/wkf_expr.py

+52-22
Original file line numberDiff line numberDiff line change
@@ -19,62 +19,92 @@
1919
#
2020
##############################################################################
2121

22+
"""
23+
Evaluate workflow code found in activity actions and transition conditions.
24+
"""
25+
2226
import openerp
2327
from openerp.tools.safe_eval import safe_eval as eval
2428

2529
class Env(dict):
26-
def __init__(self, cr, uid, model, ids):
30+
"""
31+
Dictionary class used as an environment to evaluate workflow code (such as
32+
the condition on transitions).
33+
34+
This environment provides sybmols for cr, uid, id, model name, model
35+
instance, column names, and all the record (the one obtained by browsing
36+
the provided ID) attributes.
37+
"""
38+
def __init__(self, cr, uid, model, id):
2739
self.cr = cr
2840
self.uid = uid
2941
self.model = model
30-
self.ids = ids
42+
self.id = id
43+
self.ids = [id]
3144
self.obj = openerp.registry(cr.dbname)[model]
3245
self.columns = self.obj._columns.keys() + self.obj._inherit_fields.keys()
3346

3447
def __getitem__(self, key):
3548
if (key in self.columns) or (key in dir(self.obj)):
36-
res = self.obj.browse(self.cr, self.uid, self.ids[0])
49+
res = self.obj.browse(self.cr, self.uid, self.id)
3750
return res[key]
3851
else:
3952
return super(Env, self).__getitem__(key)
4053

41-
def _eval_expr(cr, ident, workitem, action):
42-
ret=False
43-
assert action, 'You used a NULL action in a workflow, use dummy node instead.'
44-
for line in action.split('\n'):
54+
def _eval_expr(cr, ident, workitem, lines):
55+
"""
56+
Evaluate each line of ``lines`` with the ``Env`` environment, returning
57+
the value of the last line.
58+
"""
59+
assert lines, 'You used a NULL action in a workflow, use dummy node instead.'
60+
uid, model, id = ident
61+
result = False
62+
for line in lines.split('\n'):
4563
line = line.strip()
4664
if not line:
4765
continue
48-
uid=ident[0]
49-
model=ident[1]
50-
ids=[ident[2]]
51-
if line =='True':
52-
ret=True
53-
elif line =='False':
54-
ret=False
66+
if line == 'True':
67+
result = True
68+
elif line == 'False':
69+
result = False
5570
else:
56-
env = Env(cr, uid, model, ids)
57-
ret = eval(line, env, nocopy=True)
58-
return ret
71+
env = Env(cr, uid, model, id)
72+
result = eval(line, env, nocopy=True)
73+
return result
5974

6075
def execute_action(cr, ident, workitem, activity):
61-
obj = openerp.registry(cr.dbname)['ir.actions.server']
62-
ctx = {'active_model':ident[1], 'active_id':ident[2], 'active_ids':[ident[2]]}
63-
result = obj.run(cr, ident[0], [activity['action_id']], ctx)
76+
"""
77+
Evaluate the ir.actions.server action specified in the activity.
78+
"""
79+
uid, model, id = ident
80+
ir_actions_server = openerp.registry(cr.dbname)['ir.actions.server']
81+
context = { 'active_model': model, 'active_id': id, 'active_ids': [id] }
82+
result = ir_actions_server.run(cr, uid, [activity['action_id']], context)
6483
return result
6584

6685
def execute(cr, ident, workitem, activity):
86+
"""
87+
Evaluate the action specified in the activity.
88+
"""
6789
return _eval_expr(cr, ident, workitem, activity['action'])
6890

6991
def check(cr, workitem, ident, transition, signal):
92+
"""
93+
Test if a transition can be taken. The transition can be taken if:
94+
95+
- the signal name matches,
96+
- the uid is SUPERUSER_ID or the user groups contains the transition's
97+
group,
98+
- the condition evaluates to a truish value.
99+
"""
70100
if transition['signal'] and signal != transition['signal']:
71101
return False
72102

73103
uid = ident[0]
74-
if transition['group_id'] and uid != 1:
104+
if uid != openerp.SUPERUSER_ID and transition['groups_id']:
75105
registry = openerp.registry(cr.dbname)
76106
user_groups = registry['res.users'].read(cr, uid, [uid], ['groups_id'])[0]['groups_id']
77-
if not transition['group_id'] in user_groups:
107+
if transition['group_id'] not in user_groups:
78108
return False
79109

80110
return _eval_expr(cr, ident, workitem, transition['condition'])

0 commit comments

Comments
 (0)