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
|
2009-09-28 11:21:11 +02:00
|
|
|
from trytond.tools import safe_eval
|
2010-08-19 22:01:12 +02:00
|
|
|
from trytond.transaction import Transaction
|
2007-12-10 01:04:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class EnvCall(object):
|
|
|
|
|
|
2010-08-19 22:01:12 +02:00
|
|
|
def __init__(self, obj, method, obj_id):
|
2008-04-08 15:33:23 +02:00
|
|
|
self.obj = obj
|
|
|
|
|
self.method = method
|
|
|
|
|
self.obj_id = obj_id
|
2007-12-10 01:04:13 +01:00
|
|
|
|
2008-04-08 15:33:23 +02:00
|
|
|
def __call__(self, *args, **kargs):
|
2010-08-19 22:01:12 +02:00
|
|
|
return getattr(self.obj, self.method)(self.obj_id, *args, **kargs)
|
2007-12-10 01:04:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Env(dict):
|
|
|
|
|
|
2010-08-19 22:01:12 +02:00
|
|
|
def __init__(self, model, obj_id):
|
2007-12-10 01:04:13 +01:00
|
|
|
super(Env, self).__init__()
|
|
|
|
|
self.model = model
|
2008-04-08 15:33:23 +02:00
|
|
|
self.obj_id = obj_id
|
2010-08-19 22:01:12 +02:00
|
|
|
self.obj = Pool(Transaction().cursor.dbname).get(model)
|
|
|
|
|
self.browse = self.obj.browse(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):
|
2008-10-27 16:39:37 +01:00
|
|
|
if key == 'context':
|
2010-08-19 22:01:12 +02:00
|
|
|
return Transaction().context
|
2008-10-27 16:39:37 +01:00
|
|
|
elif key in self.columns:
|
2008-05-13 13:57:30 +02:00
|
|
|
res = self.browse[key]
|
2007-12-10 01:04:13 +01:00
|
|
|
return res
|
|
|
|
|
elif key in dir(self.obj):
|
2010-08-19 22:01:12 +02:00
|
|
|
return EnvCall(self.obj, key, self.obj_id)
|
2007-12-10 01:04:13 +01:00
|
|
|
else:
|
|
|
|
|
return super(Env, self).__getitem__(key)
|
|
|
|
|
|
2010-08-19 22:01:12 +02:00
|
|
|
def eval_expr(model, obj_id, action):
|
2007-12-10 01:04:13 +01:00
|
|
|
res = False
|
2010-08-19 22:01:12 +02:00
|
|
|
env = Env(model, obj_id)
|
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:
|
2009-09-28 11:21:11 +02:00
|
|
|
res = safe_eval(line, env)
|
2007-12-10 01:04:13 +01:00
|
|
|
return res
|
|
|
|
|
|
2010-08-19 22:01:12 +02:00
|
|
|
def execute(model, obj_id, activity):
|
2009-02-11 14:52:22 +01:00
|
|
|
'''
|
|
|
|
|
Execute
|
2007-12-10 01:04:13 +01:00
|
|
|
|
2009-02-11 14:52:22 +01:00
|
|
|
:param model: the model name
|
|
|
|
|
:param obj_id: the record id
|
|
|
|
|
:param activity: a BrowseRecord of workflow.activity
|
|
|
|
|
'''
|
2010-08-19 22:01:12 +02:00
|
|
|
return eval_expr(model, obj_id, activity.action)
|
2009-02-11 14:52:22 +01:00
|
|
|
|
2010-08-19 22:01:12 +02:00
|
|
|
def check(model, obj_id, transition, signal):
|
2009-02-11 14:52:22 +01:00
|
|
|
'''
|
|
|
|
|
Check
|
|
|
|
|
|
|
|
|
|
:param model: the model name
|
|
|
|
|
:param obj_id: the record id
|
|
|
|
|
:param transition: a BrowseRecord of workflow.transition
|
|
|
|
|
'''
|
|
|
|
|
if transition.signal:
|
2009-03-19 19:16:39 +01:00
|
|
|
if signal != transition.signal:
|
|
|
|
|
return False
|
2009-02-11 14:52:22 +01:00
|
|
|
|
2010-08-19 22:01:12 +02:00
|
|
|
if transition.group and Transaction().user != 0:
|
|
|
|
|
user_obj = Pool(Transaction().cursor.dbname).get('res.user')
|
|
|
|
|
user_groups = user_obj.get_groups()
|
2009-03-19 19:16:39 +01:00
|
|
|
if transition.group.id not in user_groups:
|
|
|
|
|
return False
|
2010-08-19 22:01:12 +02:00
|
|
|
return eval_expr(model, obj_id, transition.condition)
|