Files
tryton/trytond/workflow/expr.py
T

97 lines
3.0 KiB
Python
Raw Normal View History

2009-02-11 14:52:22 +01:00
#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
2007-12-10 01:04:13 +01:00
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
2007-12-10 01:04:13 +01:00
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)
2007-12-10 01:04:13 +01:00
class Env(dict):
def __init__(self, cursor, user, model, obj_id, context=None):
2007-12-10 01:04:13 +01:00
super(Env, self).__init__()
self.cursor = cursor
self.user = user
self.model = model
self.obj_id = obj_id
self.context = context
2009-02-11 14:52:22 +01:00
self.obj = Pool(cursor.dbname).get(model)
self.browse = self.obj.browse(cursor, user, obj_id)
2007-12-10 01:04:13 +01:00
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]
2007-12-10 01:04:13 +01:00
return res
elif key in dir(self.obj):
return EnvCall(self.obj, self.cursor, self.user, key, self.obj_id,
context=self.context)
2007-12-10 01:04:13 +01:00
else:
return super(Env, self).__getitem__(key)
2009-02-11 14:52:22 +01:00
def eval_expr(cursor, user, model, obj_id, action, context=None):
2007-12-10 01:04:13 +01:00
res = False
env = Env(cursor, user, model, obj_id, context=context)
2007-12-10 01:04:13 +01:00
for line in action.split('\n'):
if line == 'True':
res = True
elif line =='False':
res = False
else:
res = eval(line, env)
return res
2009-02-11 14:52:22 +01:00
def execute(cursor, user, model, obj_id, activity, context=None):
'''
Execute
2007-12-10 01:04:13 +01:00
2009-02-11 14:52:22 +01:00
: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)
2009-02-11 14:52:22 +01:00
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
'''
res = True
if transition.signal:
res = (signal == transition.signal)
if transition.group and user != 0:
user_obj = Pool(cursor.dbname).get('res.user')
user_groups = user_obj.get_groups(cursor, user, context=context)
res = res and transition.group.id in user_groups
res = res and eval_expr(cursor, user, model, obj_id,
transition.condition, context=context)
2007-12-10 01:04:13 +01:00
return res