[IMP] tools: add "unquote" class for magic bare variables in context/domains

This is used to allow bare active_id variables in context
or domains of act_window tags. 
Note that previously this was done by replacing the active_id with
a active_id string, which required further trick on the client
side to perform the reverse operation.

bzr revid: odo@openerp.com-20110328075920-ljadcubezs9swli8
This commit is contained in:
Olivier Dony 2011-03-28 09:59:20 +02:00
parent 0fb1c7a799
commit 9effe4fa6d
2 changed files with 37 additions and 2 deletions

View File

@ -50,6 +50,8 @@ from yaml_import import convert_yaml_import
# List of etree._Element subclasses that we choose to ignore when parsing XML.
from misc import SKIPPED_ELEMENT_TYPES
from misc import unquote
# Import of XML records requires the unsafe eval as well,
# almost everywhere, which is ok because it supposedly comes
# from trusted data, but at least we make it obvious now.
@ -439,7 +441,21 @@ form: module.record_id""" % (xml_id,)
limit = rec.get('limit','').encode('utf-8')
auto_refresh = rec.get('auto_refresh','').encode('utf-8')
uid = self.uid
active_id = str("active_id") # for further reference in client/bin/tools/__init__.py
# Act_window's 'domain' and 'context' contain mostly literals
# but they can also refer to the variables provided below
# in eval_context, so we need to eval() them before storing.
# Among the context variables, 'active_id' refers to
# the currently selected items in a list view, and only
# takes meaning at runtime on the client side. For this
# reason it must remain a bare variable in domain and context,
# even after eval() at server-side. We use the special 'unquote'
# class to achieve this effect: a string which has itself, unquoted,
# as representation.
active_id = unquote("active_id")
active_ids = unquote("active_ids")
active_model = unquote("active_model")
def ref(str_id):
return self.id_get(cr, str_id)
@ -459,6 +475,8 @@ form: module.record_id""" % (xml_id,)
'auto_refresh': auto_refresh,
'uid' : uid,
'active_id': active_id,
'active_ids': active_ids,
'active_model': active_model,
'ref' : ref,
}
context = self.get_context(data_node, rec, eval_context)

View File

@ -1439,6 +1439,23 @@ def attrgetter(*items):
return tuple(resolve_attr(obj, attr) for attr in items)
return g
class unquote(str):
"""A subclass of str that implements repr() without enclosing quotation marks
or escaping, keeping the original string untouched. The name come from Lisp's unquote.
One of the uses for this is to preserve or insert bare variable names within dicts during eval()
of a dict's repr(). Use with care.
Some examples:
>>> unquote('active_id')
active_id
>>> repr(unquote('active_id'))
active_id
>>> d = {'test': unquote('active_id')}
>>> d
{'test': active_id}
>>> repr(d)
"{'test': active_id}"
"""
def __repr__(self):
return self
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: