File: expr.py

package info (click to toggle)
tryton-server 1.6.1-2%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 2,440 kB
  • ctags: 3,017
  • sloc: python: 24,380; xml: 3,771; sql: 506; sh: 126; makefile: 74
file content (97 lines) | stat: -rw-r--r-- 3,111 bytes parent folder | download | duplicates (2)
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
#This file is part of Tryton.  The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
from trytond.pool import Pool
from trytond.tools import safe_eval


class EnvCall(object):

    def __init__(self, obj, cursor, user, method, obj_id, context=None):
        self.obj = obj
        self.cursor = cursor
        self.user = user
        self.method = method
        self.obj_id = obj_id
        self.context = context

    def __call__(self, *args, **kargs):
        kargs = kargs.copy()
        kargs['context'] = self.context
        return getattr(self.obj, self.method)(self.cursor, self.user,
                self.obj_id, *args, **kargs)


class Env(dict):

    def __init__(self, cursor, user, model, obj_id, context=None):
        super(Env, self).__init__()
        self.cursor = cursor
        self.user = user
        self.model = model
        self.obj_id = obj_id
        self.context = context
        self.obj = Pool(cursor.dbname).get(model)
        self.browse = self.obj.browse(cursor, user, obj_id)
        self.columns = self.obj._columns.keys() + \
                self.obj._inherit_fields.keys()

    def __getitem__(self, key):
        if key == 'context':
            return self.context
        elif key in self.columns:
            res = self.browse[key]
            return res
        elif key in dir(self.obj):
            return EnvCall(self.obj, self.cursor, self.user, key, self.obj_id,
                    context=self.context)
        else:
            return super(Env, self).__getitem__(key)

def eval_expr(cursor, user, model, obj_id, action, context=None):
    res = False
    env = Env(cursor, user, model, obj_id, context=context)
    for line in action.split('\n'):
        if line == 'True':
            res = True
        elif line =='False':
            res = False
        else:
            res = safe_eval(line, env)
    return res

def execute(cursor, user, model, obj_id, activity, context=None):
    '''
    Execute

    :param cursor: the database cursor
    :param user: the user id
    :param model: the model name
    :param obj_id: the record id
    :param activity: a BrowseRecord of workflow.activity
    :param context: the context
    '''
    return eval_expr(cursor, user, model, obj_id, activity.action,
            context=context)

def check(cursor, user, model, obj_id, transition, signal, context=None):
    '''
    Check

    :param cursor: the database cursor
    :param user: the user id
    :param model: the model name
    :param obj_id: the record id
    :param transition: a BrowseRecord of workflow.transition
    :param context: the context
    '''
    if transition.signal:
        if signal != transition.signal:
            return False

    if transition.group and user != 0:
        user_obj = Pool(cursor.dbname).get('res.user')
        user_groups = user_obj.get_groups(cursor, user, context=context)
        if transition.group.id not in user_groups:
            return False
    return eval_expr(cursor, user, model, obj_id, transition.condition,
            context=context)