|
19 | 19 | #
|
20 | 20 | ##############################################################################
|
21 | 21 |
|
| 22 | +""" |
| 23 | +Evaluate workflow code found in activity actions and transition conditions. |
| 24 | +""" |
| 25 | + |
22 | 26 | import openerp
|
23 | 27 | from openerp.tools.safe_eval import safe_eval as eval
|
24 | 28 |
|
25 | 29 | 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): |
27 | 39 | self.cr = cr
|
28 | 40 | self.uid = uid
|
29 | 41 | self.model = model
|
30 |
| - self.ids = ids |
| 42 | + self.id = id |
| 43 | + self.ids = [id] |
31 | 44 | self.obj = openerp.registry(cr.dbname)[model]
|
32 | 45 | self.columns = self.obj._columns.keys() + self.obj._inherit_fields.keys()
|
33 | 46 |
|
34 | 47 | def __getitem__(self, key):
|
35 | 48 | 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) |
37 | 50 | return res[key]
|
38 | 51 | else:
|
39 | 52 | return super(Env, self).__getitem__(key)
|
40 | 53 |
|
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'): |
45 | 63 | line = line.strip()
|
46 | 64 | if not line:
|
47 | 65 | 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 |
55 | 70 | 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 |
59 | 74 |
|
60 | 75 | 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) |
64 | 83 | return result
|
65 | 84 |
|
66 | 85 | def execute(cr, ident, workitem, activity):
|
| 86 | + """ |
| 87 | + Evaluate the action specified in the activity. |
| 88 | + """ |
67 | 89 | return _eval_expr(cr, ident, workitem, activity['action'])
|
68 | 90 |
|
69 | 91 | 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 | + """ |
70 | 100 | if transition['signal'] and signal != transition['signal']:
|
71 | 101 | return False
|
72 | 102 |
|
73 | 103 | uid = ident[0]
|
74 |
| - if transition['group_id'] and uid != 1: |
| 104 | + if uid != openerp.SUPERUSER_ID and transition['groups_id']: |
75 | 105 | registry = openerp.registry(cr.dbname)
|
76 | 106 | 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: |
78 | 108 | return False
|
79 | 109 |
|
80 | 110 | return _eval_expr(cr, ident, workitem, transition['condition'])
|
|
0 commit comments