diff --git a/bin/tools/convert.py b/bin/tools/convert.py index 973e762cd2e..d9b663ae7a2 100644 --- a/bin/tools/convert.py +++ b/bin/tools/convert.py @@ -102,13 +102,12 @@ def _eval_xml(self, node, pool, cr, uid, idref, context=None): pytz=pytz) if len(f_model): idref2['obj'] = _obj(self.pool, cr, uid, f_model, context=context) - try: - return unsafe_eval(a_eval, idref2) + return unsafe_eval(a_eval, idref2) except Exception: - logger = logging.getLogger('init') - logger.warning('could not eval(%s) for %s in %s' % (a_eval, node.get('name'), context), exc_info=True) - return "" + logger = logging.getLogger('init') + logger.warning('could not eval(%s) for %s in %s' % (a_eval, node.get('name'), context), exc_info=True) + return "" if t == 'xml': def _process(s, idref): m = re.findall('[^%]%\((.*?)\)[ds]', s) @@ -212,15 +211,24 @@ class xml_import(object): def get_context(self, data_node, node, eval_dict): data_node_context = (len(data_node) and data_node.get('context','').encode('utf8')) - if data_node_context: - context = unsafe_eval(data_node_context, eval_dict) - else: - context = {} - node_context = node.get("context",'').encode('utf8') - if node_context: - context.update(unsafe_eval(node_context, eval_dict)) - + context = {} + for ctx in (data_node_context, node_context): + if ctx: + try: + ctx_res = unsafe_eval(ctx, eval_dict) + if isinstance(context, dict): + context.update(ctx_res) + else: + context = ctx_res + except NameError: + # Some contexts contain references that are only valid at runtime at + # client-side, so in that case we keep the original context string + # as it is. We also log it, just in case. + context = ctx + logging.getLogger("init").debug('Context value (%s) for element with id "%s" or its data node does not parse '\ + 'at server-side, keeping original string, in case it\'s meant for client side only', + ctx, node.get('id','n/a'), exc_info=True) return context def get_uid(self, cr, uid, data_node, node): @@ -390,26 +398,47 @@ form: module.record_id""" % (xml_id,) if rec.get('view'): view_id = self.id_get(cr, 'ir.actions.act_window', rec.get('view','').encode('utf-8')) domain = rec.get('domain','').encode('utf-8') or '{}' - context = rec.get('context','').encode('utf-8') or '{}' res_model = rec.get('res_model','').encode('utf-8') src_model = rec.get('src_model','').encode('utf-8') view_type = rec.get('view_type','').encode('utf-8') or 'form' view_mode = rec.get('view_mode','').encode('utf-8') or 'tree,form' - usage = rec.get('usage','').encode('utf-8') limit = rec.get('limit','').encode('utf-8') auto_refresh = rec.get('auto_refresh','').encode('utf-8') - - # WARNING: don't remove the unused variables and functions below as they are - # used by the unsafe_eval() call. - # TODO: change this into an eval call with locals and globals parameters uid = self.uid active_id = str("active_id") # for further reference in client/bin/tools/__init__.py def ref(str_id): return self.id_get(cr, None, str_id) - context = unsafe_eval(context) -# domain = eval(domain) # XXX need to test this line -> uid, active_id, active_ids, ... + # Include all locals() in eval_context, for backwards compatibility + eval_context = { + 'name': name, + 'xml_id': xml_id, + 'type': type, + 'view_id': view_id, + 'domain': domain, + 'res_model': res_model, + 'src_model': src_model, + 'view_type': view_type, + 'view_mode': view_mode, + 'usage': usage, + 'limit': limit, + 'auto_refresh': auto_refresh, + 'uid' : uid, + 'active_id': active_id, + 'ref' : ref, + } + context = self.get_context(data_node, rec, eval_context) + + try: + domain = unsafe_eval(domain, eval_context) + except NameError: + # Some domains contain references that are only valid at runtime at + # client-side, so in that case we keep the original domain string + # as it is. We also log it, just in case. + logging.getLogger("init").debug('Domain value (%s) for element with id "%s" does not parse '\ + 'at server-side, keeping original string, in case it\'s meant for client side only', + domain, xml_id or 'n/a', exc_info=True) res = { 'name': name, 'type': type, @@ -713,12 +742,12 @@ form: module.record_id""" % (xml_id,) for field in rec.findall('./field'): #TODO: most of this code is duplicated above (in _eval_xml)... f_name = field.get("name",'').encode('utf-8') - f_ref = field.get("ref",'').encode('ascii') + f_ref = field.get("ref",'').encode('utf-8') f_search = field.get("search",'').encode('utf-8') - f_model = field.get("model",'').encode('ascii') + f_model = field.get("model",'').encode('utf-8') if not f_model and model._columns.get(f_name,False): f_model = model._columns[f_name]._obj - f_use = field.get("use",'').encode('ascii') or 'id' + f_use = field.get("use",'').encode('utf-8') or 'id' f_val = False if f_search: