2013-10-04 11:41:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-10-07 09:15:56 +00:00
|
|
|
import cStringIO
|
|
|
|
import json
|
2013-10-14 08:52:15 +00:00
|
|
|
import datetime
|
2013-10-18 14:36:59 +00:00
|
|
|
import dateutil.relativedelta
|
2013-06-27 15:08:07 +00:00
|
|
|
import logging
|
2013-08-08 18:11:38 +00:00
|
|
|
import re
|
2013-10-14 08:52:15 +00:00
|
|
|
import urllib
|
|
|
|
import xml # FIXME use lxml and etree
|
2013-06-27 14:20:22 +00:00
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
import Image
|
2013-10-21 12:57:23 +00:00
|
|
|
import math
|
2013-09-26 14:51:26 +00:00
|
|
|
import werkzeug.utils
|
2013-06-27 15:08:07 +00:00
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
from openerp.osv import osv, orm, fields
|
2013-10-21 15:26:39 +00:00
|
|
|
import openerp.tools
|
2013-06-27 15:08:07 +00:00
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
2013-06-27 09:13:29 +00:00
|
|
|
|
2013-08-13 14:08:09 +00:00
|
|
|
BUILTINS = {
|
|
|
|
'False': False,
|
|
|
|
'None': None,
|
|
|
|
'True': True,
|
|
|
|
'abs': abs,
|
|
|
|
'bool': bool,
|
|
|
|
'dict': dict,
|
|
|
|
'filter': filter,
|
|
|
|
'len': len,
|
|
|
|
'list': list,
|
|
|
|
'map': map,
|
|
|
|
'max': max,
|
|
|
|
'min': min,
|
|
|
|
'reduce': reduce,
|
|
|
|
'repr': repr,
|
|
|
|
'round': round,
|
|
|
|
'set': set,
|
|
|
|
'str': str,
|
|
|
|
'tuple': tuple,
|
2013-10-14 08:52:15 +00:00
|
|
|
'quote': urllib.quote,
|
|
|
|
'urlencode': urllib.urlencode,
|
|
|
|
'datetime': datetime,
|
|
|
|
# dateutil.relativedelta is an old-style class and cannot be directly
|
|
|
|
# instanciated wihtin a jinja2 expression, so a lambda "proxy" is
|
|
|
|
# is needed, apparently.
|
|
|
|
'relativedelta': lambda *a, **kw : dateutil.relativedelta.relativedelta(*a, **kw),
|
2013-08-13 14:08:09 +00:00
|
|
|
}
|
|
|
|
|
2013-10-18 09:59:13 +00:00
|
|
|
class QWebException(Exception):
|
2013-10-18 11:39:27 +00:00
|
|
|
def __init__(self, message, template=None, node=None, attribute=None):
|
|
|
|
self.message = message
|
|
|
|
self.template = template
|
|
|
|
self.node = node
|
|
|
|
self.attribute = attribute
|
2013-10-18 09:59:13 +00:00
|
|
|
|
2013-10-14 08:52:15 +00:00
|
|
|
## We use a jinja2 sandboxed environment to render qWeb templates.
|
|
|
|
#from openerp.tools.safe_eval import safe_eval as eval
|
|
|
|
#from jinja2.sandbox import SandboxedEnvironment
|
|
|
|
#from jinja2.exceptions import SecurityError, UndefinedError
|
|
|
|
#UNSAFE = ["browse", "search", "read", "unlink", "read_group"]
|
|
|
|
#SAFE = ["_name"]
|
|
|
|
|
2013-08-08 18:11:38 +00:00
|
|
|
class QWebContext(dict):
|
2013-10-18 14:45:46 +00:00
|
|
|
def __init__(self, cr, uid, data, undefined_handler=None, loader=None,
|
|
|
|
templates=None, context=None):
|
|
|
|
self.cr = cr
|
|
|
|
self.uid = uid
|
2013-10-04 11:41:55 +00:00
|
|
|
self.loader = loader
|
2013-08-08 18:11:38 +00:00
|
|
|
self.undefined_handler = undefined_handler
|
2013-10-15 15:28:58 +00:00
|
|
|
self.templates = templates or {}
|
2013-10-18 14:45:46 +00:00
|
|
|
self.context = context
|
2013-08-13 14:08:09 +00:00
|
|
|
dic = BUILTINS.copy()
|
|
|
|
dic.update(data)
|
2013-09-25 12:10:28 +00:00
|
|
|
super(QWebContext, self).__init__(dic)
|
2013-08-08 18:11:38 +00:00
|
|
|
self['defined'] = lambda key: key in self
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
if key in self:
|
|
|
|
return self.get(key)
|
2013-08-08 18:22:18 +00:00
|
|
|
elif not self.undefined_handler:
|
|
|
|
raise NameError("QWeb: name %r is not defined while rendering template %r" % (key, self.get('__template__')))
|
2013-06-27 09:13:29 +00:00
|
|
|
else:
|
2013-08-08 18:22:18 +00:00
|
|
|
return self.get(key, self.undefined_handler(key, self))
|
2013-06-27 09:13:29 +00:00
|
|
|
|
2013-10-14 08:52:15 +00:00
|
|
|
def safe_eval(self, expr):
|
|
|
|
# This is too slow, we should cached compiled expressions attribute of
|
|
|
|
# qweb to will be changed into a model object ir.qweb.
|
|
|
|
#
|
|
|
|
# The cache should be on qweb, and qweb context contructor take qweb as
|
|
|
|
# argument to store the cache.
|
|
|
|
#
|
|
|
|
#class QWebSandboxedEnvironment(SandboxedEnvironment):
|
|
|
|
# def is_safe_attribute(self, obj, attr, value):
|
|
|
|
# if str(attr) in SAFE:
|
|
|
|
# res = True
|
|
|
|
# else:
|
|
|
|
# res = super(QWebSandboxedEnvironment, self).is_safe_attribute(obj, attr, value)
|
|
|
|
# if str(attr) in UNSAFE or not res:
|
|
|
|
# raise SecurityError("access to attribute '%s' of '%s' object is unsafe." % (attr,obj))
|
|
|
|
# return res
|
|
|
|
#env = qWebSandboxedEnvironment(variable_start_string="${", variable_end_string="}")
|
|
|
|
#env.globals.update(context)
|
|
|
|
#env.compile_expression(expr)()
|
|
|
|
return eval(expr, None, self)
|
|
|
|
|
2013-10-04 11:41:55 +00:00
|
|
|
def copy(self):
|
2013-10-18 14:45:46 +00:00
|
|
|
return QWebContext(self.cr, self.uid, dict.copy(self),
|
2013-10-04 11:41:55 +00:00
|
|
|
undefined_handler=self.undefined_handler,
|
2013-10-15 15:28:58 +00:00
|
|
|
loader=self.loader,
|
2013-10-18 14:45:46 +00:00
|
|
|
templates=self.templates,
|
|
|
|
context=self.context)
|
2013-10-04 11:41:55 +00:00
|
|
|
|
|
|
|
def __copy__(self):
|
|
|
|
return self.copy()
|
|
|
|
|
|
|
|
class QWeb(orm.AbstractModel):
|
2013-06-27 09:13:29 +00:00
|
|
|
"""QWeb Xml templating engine
|
2013-06-27 15:08:07 +00:00
|
|
|
|
2013-06-27 09:13:29 +00:00
|
|
|
The templating engine use a very simple syntax, "magic" xml attributes, to
|
|
|
|
produce any kind of texutal output (even non-xml).
|
2013-06-27 15:08:07 +00:00
|
|
|
|
2013-06-27 09:13:29 +00:00
|
|
|
QWebXml:
|
|
|
|
the template engine core implements the basic magic attributes:
|
2013-06-27 15:08:07 +00:00
|
|
|
|
2013-06-27 09:13:29 +00:00
|
|
|
t-att t-raw t-esc t-if t-foreach t-set t-call t-trim
|
2013-06-27 15:08:07 +00:00
|
|
|
|
2013-06-27 09:13:29 +00:00
|
|
|
|
|
|
|
- loader: function that return a template
|
|
|
|
|
2013-10-08 07:07:42 +00:00
|
|
|
QWeb rendering can be used for many tasks. As a result, customizations
|
|
|
|
made by one task context (to either the main qweb rendering or to specific
|
|
|
|
fields rendering) could break other tasks.
|
|
|
|
|
|
|
|
To avoid that, ``ir.qweb`` was consciously made inheritable and the "root"
|
|
|
|
of an object hierarchy. If you need extensions or alterations which could
|
|
|
|
be incompatible with other subsystems, you should create a local object
|
|
|
|
inheriting from ``ir.qweb`` and customize that.
|
|
|
|
|
|
|
|
If you need to customize t-fields rendering, subclass the ir.qweb.field
|
|
|
|
model (and its sub-models) then override :meth:`~.get_converter_for` to
|
|
|
|
fetch the right field converters for your qweb model.
|
2013-06-27 09:13:29 +00:00
|
|
|
"""
|
2013-10-04 11:41:55 +00:00
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
_name = 'ir.qweb'
|
2013-10-04 11:41:55 +00:00
|
|
|
|
|
|
|
node = xml.dom.Node
|
|
|
|
_void_elements = frozenset([
|
|
|
|
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen',
|
|
|
|
'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr'])
|
2013-10-04 12:52:04 +00:00
|
|
|
_format_regex = re.compile(
|
|
|
|
'(?:'
|
|
|
|
# ruby-style pattern
|
|
|
|
'#\{(.+?)\}'
|
|
|
|
')|(?:'
|
|
|
|
# jinja-style pattern
|
|
|
|
'\{\{(.+?)\}\}'
|
|
|
|
')')
|
2013-10-04 11:41:55 +00:00
|
|
|
|
|
|
|
def __init__(self, pool, cr):
|
|
|
|
super(QWeb, self).__init__(pool, cr)
|
|
|
|
|
|
|
|
self._render_tag = self.prefixed_methods('render_tag_')
|
|
|
|
self._render_att = self.prefixed_methods('render_att_')
|
|
|
|
|
|
|
|
def prefixed_methods(self, prefix):
|
|
|
|
""" Extracts all methods prefixed by ``prefix``, and returns a mapping
|
|
|
|
of (t-name, method) where the t-name is the method name with prefix
|
|
|
|
removed and underscore converted to dashes
|
|
|
|
|
|
|
|
:param str prefix:
|
|
|
|
:return: dict
|
|
|
|
"""
|
|
|
|
return dict(
|
|
|
|
(name[len(prefix):].replace('_', '-'), getattr(type(self), name))
|
|
|
|
for name in dir(self)
|
|
|
|
if name.startswith(prefix))
|
2013-06-27 15:08:07 +00:00
|
|
|
|
|
|
|
def register_tag(self, tag, func):
|
|
|
|
self._render_tag[tag] = func
|
|
|
|
|
2013-10-15 15:28:58 +00:00
|
|
|
def load_document(self, x, into):
|
2013-10-04 11:41:55 +00:00
|
|
|
"""
|
|
|
|
Loads an XML document and installs any contained template in the engine
|
|
|
|
"""
|
2013-06-27 15:08:07 +00:00
|
|
|
if hasattr(x, 'documentElement'):
|
|
|
|
dom = x
|
2013-06-27 09:13:29 +00:00
|
|
|
elif x.startswith("<?xml"):
|
2013-06-27 15:08:07 +00:00
|
|
|
dom = xml.dom.minidom.parseString(x)
|
2013-06-27 09:13:29 +00:00
|
|
|
else:
|
2013-06-27 15:08:07 +00:00
|
|
|
dom = xml.dom.minidom.parse(x)
|
2013-10-04 11:41:55 +00:00
|
|
|
|
2013-06-27 09:13:29 +00:00
|
|
|
for n in dom.documentElement.childNodes:
|
2013-10-04 11:41:55 +00:00
|
|
|
if n.nodeType == self.node.ELEMENT_NODE and n.getAttribute('t-name'):
|
2013-10-15 15:28:58 +00:00
|
|
|
into[str(n.getAttribute("t-name"))] = n
|
2013-06-27 14:20:22 +00:00
|
|
|
|
2013-10-04 11:41:55 +00:00
|
|
|
def get_template(self, name, context):
|
2013-10-15 15:28:58 +00:00
|
|
|
if context.loader and name not in context.templates:
|
2013-10-04 11:41:55 +00:00
|
|
|
xml_doc = context.loader(name)
|
2013-10-15 15:28:58 +00:00
|
|
|
self.load_document(xml_doc, into=context.templates)
|
2013-10-04 11:41:55 +00:00
|
|
|
|
2013-10-15 15:28:58 +00:00
|
|
|
if name in context.templates:
|
|
|
|
return context.templates[name]
|
2013-10-04 11:41:55 +00:00
|
|
|
|
2013-06-27 14:20:22 +00:00
|
|
|
raise KeyError('qweb: template "%s" not found' % name)
|
2013-06-27 09:13:29 +00:00
|
|
|
|
2013-08-08 18:11:38 +00:00
|
|
|
def eval(self, expr, v):
|
|
|
|
try:
|
2013-10-14 08:52:15 +00:00
|
|
|
return v.safe_eval(expr)
|
2013-11-02 16:55:15 +00:00
|
|
|
except Exception, e:
|
|
|
|
# add qweb metdata on exception
|
|
|
|
setattr(e, 'qweb_eval', expr)
|
|
|
|
setattr(e, 'qweb_template', v.get('__template__'))
|
|
|
|
raise
|
2013-08-08 18:11:38 +00:00
|
|
|
|
2013-06-27 15:08:07 +00:00
|
|
|
def eval_object(self, expr, v):
|
2013-08-08 18:11:38 +00:00
|
|
|
return self.eval(expr, v)
|
2013-06-27 15:08:07 +00:00
|
|
|
|
|
|
|
def eval_str(self, expr, v):
|
2013-08-08 18:11:38 +00:00
|
|
|
if expr == "0":
|
|
|
|
return v.get(0, '')
|
|
|
|
val = self.eval(expr, v)
|
|
|
|
if isinstance(val, unicode):
|
|
|
|
return val.encode("utf8")
|
|
|
|
return str(val)
|
2013-06-27 15:08:07 +00:00
|
|
|
|
|
|
|
def eval_format(self, expr, v):
|
2013-10-04 12:52:04 +00:00
|
|
|
expr, replacements = self._format_regex.subn(
|
|
|
|
lambda m: self.eval_str(m.group(1) or m.group(2), v),
|
|
|
|
expr)
|
2013-09-24 08:46:20 +00:00
|
|
|
|
2013-10-04 12:52:04 +00:00
|
|
|
if replacements:
|
2013-08-08 18:11:38 +00:00
|
|
|
return expr
|
2013-10-04 12:52:04 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
return str(expr % v)
|
|
|
|
except:
|
|
|
|
raise Exception("QWeb: format error '%s' " % expr)
|
2013-06-27 15:08:07 +00:00
|
|
|
|
|
|
|
def eval_bool(self, expr, v):
|
2013-08-08 18:11:38 +00:00
|
|
|
val = self.eval(expr, v)
|
|
|
|
if val:
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return 0
|
2013-06-27 09:13:29 +00:00
|
|
|
|
2013-10-18 14:45:46 +00:00
|
|
|
def render(self, cr, uid, tname, v=None, loader=None,
|
|
|
|
undefined_handler=None, context=None):
|
2013-06-30 21:35:15 +00:00
|
|
|
if v is None:
|
|
|
|
v = {}
|
2013-10-04 11:41:55 +00:00
|
|
|
if not isinstance(v, QWebContext):
|
2013-10-18 14:45:46 +00:00
|
|
|
v = QWebContext(cr, uid, v, undefined_handler=undefined_handler,
|
|
|
|
loader=loader, context=context)
|
2013-08-08 18:11:38 +00:00
|
|
|
v['__template__'] = tname
|
2013-08-12 10:52:11 +00:00
|
|
|
stack = v.get('__stack__', [])
|
|
|
|
if stack:
|
|
|
|
v['__caller__'] = stack[-1]
|
|
|
|
stack.append(tname)
|
|
|
|
v['__stack__'] = stack
|
2013-10-14 10:07:53 +00:00
|
|
|
v['xmlid'] = str(stack[0]) # Temporary fix
|
2013-10-04 11:41:55 +00:00
|
|
|
return self.render_node(self.get_template(tname, v), v)
|
2013-06-27 15:08:07 +00:00
|
|
|
|
|
|
|
def render_node(self, e, v):
|
|
|
|
r = ""
|
|
|
|
if e.nodeType == self.node.TEXT_NODE or e.nodeType == self.node.CDATA_SECTION_NODE:
|
|
|
|
r = e.data.encode("utf8")
|
|
|
|
elif e.nodeType == self.node.ELEMENT_NODE:
|
|
|
|
g_att = ""
|
|
|
|
t_render = None
|
|
|
|
t_att = {}
|
|
|
|
for (an, av) in e.attributes.items():
|
|
|
|
an = str(an)
|
2013-09-10 13:44:02 +00:00
|
|
|
if isinstance(av, unicode):
|
2013-06-27 15:08:07 +00:00
|
|
|
av = av.encode("utf8")
|
2013-06-27 09:13:29 +00:00
|
|
|
else:
|
2013-06-27 15:08:07 +00:00
|
|
|
av = av.nodeValue.encode("utf8")
|
2013-06-27 09:13:29 +00:00
|
|
|
if an.startswith("t-"):
|
|
|
|
for i in self._render_att:
|
|
|
|
if an[2:].startswith(i):
|
2013-06-27 15:08:07 +00:00
|
|
|
g_att += self._render_att[i](self, e, an, av, v)
|
2013-06-27 09:13:29 +00:00
|
|
|
break
|
|
|
|
else:
|
2013-06-27 15:08:07 +00:00
|
|
|
if an[2:] in self._render_tag:
|
|
|
|
t_render = an[2:]
|
|
|
|
t_att[an[2:]] = av
|
2013-06-27 09:13:29 +00:00
|
|
|
else:
|
2013-09-26 14:51:26 +00:00
|
|
|
g_att += ' %s="%s"' % (an, werkzeug.utils.escape(av))
|
2013-08-21 14:09:10 +00:00
|
|
|
|
|
|
|
if 'debug' in t_att:
|
|
|
|
debugger = t_att.get('debug', 'pdb')
|
|
|
|
__import__(debugger).set_trace() # pdb, ipdb, pudb, ...
|
2013-06-27 09:13:29 +00:00
|
|
|
if t_render:
|
2013-10-04 11:41:55 +00:00
|
|
|
r = self._render_tag[t_render](self, e, t_att, g_att, v)
|
2013-06-27 09:13:29 +00:00
|
|
|
else:
|
2013-06-30 21:35:15 +00:00
|
|
|
r = self.render_element(e, t_att, g_att, v)
|
2013-09-26 14:51:26 +00:00
|
|
|
if isinstance(r, unicode):
|
|
|
|
return r.encode('utf-8')
|
2013-06-27 09:13:29 +00:00
|
|
|
return r
|
2013-06-27 15:08:07 +00:00
|
|
|
|
2013-06-30 21:35:15 +00:00
|
|
|
def render_element(self, e, t_att, g_att, v, inner=None):
|
|
|
|
# e: element
|
|
|
|
# t_att: t-* attributes
|
|
|
|
# g_att: generated attributes
|
|
|
|
# v: values
|
|
|
|
# inner: optional innerXml
|
|
|
|
if inner:
|
|
|
|
g_inner = inner
|
|
|
|
else:
|
|
|
|
g_inner = []
|
|
|
|
for n in e.childNodes:
|
2013-10-18 09:59:13 +00:00
|
|
|
try:
|
|
|
|
g_inner.append(self.render_node(n, v))
|
2013-11-02 16:55:15 +00:00
|
|
|
except Exception, ex:
|
|
|
|
# add qweb metdata on exception
|
|
|
|
if not getattr(ex, 'qweb_template', None):
|
|
|
|
setattr(e, 'qweb_template', v.get('__template__'))
|
|
|
|
if not getattr(ex, 'qweb_node', None):
|
|
|
|
setattr(ex, 'qweb_node', e)
|
|
|
|
raise
|
2013-06-27 15:08:07 +00:00
|
|
|
name = str(e.nodeName)
|
|
|
|
inner = "".join(g_inner)
|
2013-06-30 21:35:15 +00:00
|
|
|
trim = t_att.get("trim", 0)
|
2013-06-27 15:08:07 +00:00
|
|
|
if trim == 0:
|
2013-06-27 09:13:29 +00:00
|
|
|
pass
|
2013-06-27 15:08:07 +00:00
|
|
|
elif trim == 'left':
|
|
|
|
inner = inner.lstrip()
|
|
|
|
elif trim == 'right':
|
|
|
|
inner = inner.rstrip()
|
|
|
|
elif trim == 'both':
|
|
|
|
inner = inner.strip()
|
|
|
|
if name == "t":
|
2013-06-27 09:13:29 +00:00
|
|
|
return inner
|
2013-08-05 13:06:05 +00:00
|
|
|
elif len(inner) or name not in self._void_elements:
|
2013-09-27 12:10:12 +00:00
|
|
|
return "<%s%s>%s</%s>" % tuple(
|
|
|
|
v if isinstance(v, str) else v.encode('utf-8')
|
|
|
|
for v in (name, g_att, inner, name))
|
2013-06-27 09:13:29 +00:00
|
|
|
else:
|
2013-06-27 15:08:07 +00:00
|
|
|
return "<%s%s/>" % (name, g_att)
|
2013-06-27 09:13:29 +00:00
|
|
|
|
|
|
|
# Attributes
|
2013-06-27 15:08:07 +00:00
|
|
|
def render_att_att(self, e, an, av, v):
|
2013-06-27 09:13:29 +00:00
|
|
|
if an.startswith("t-attf-"):
|
2013-06-27 15:08:07 +00:00
|
|
|
att, val = an[7:], self.eval_format(av, v)
|
2013-06-27 09:13:29 +00:00
|
|
|
elif an.startswith("t-att-"):
|
2013-08-29 13:26:41 +00:00
|
|
|
att, val = an[6:], self.eval(av, v)
|
|
|
|
if isinstance(val, unicode):
|
2013-08-30 14:40:40 +00:00
|
|
|
val = val.encode("utf8")
|
2013-06-27 09:13:29 +00:00
|
|
|
else:
|
2013-06-27 15:08:07 +00:00
|
|
|
att, val = self.eval_object(av, v)
|
2013-09-26 14:51:26 +00:00
|
|
|
return val and ' %s="%s"' % (att, werkzeug.utils.escape(val)) or " "
|
2013-06-27 09:13:29 +00:00
|
|
|
|
2013-09-24 08:46:20 +00:00
|
|
|
def render_att_href(self, e, an, av, v):
|
|
|
|
return self.url_for(e, an, av, v)
|
|
|
|
def render_att_src(self, e, an, av, v):
|
|
|
|
return self.url_for(e, an, av, v)
|
2013-09-26 14:53:42 +00:00
|
|
|
def render_att_action(self, e, an, av, v):
|
|
|
|
return self.url_for(e, an, av, v)
|
2013-09-24 08:46:20 +00:00
|
|
|
def url_for(self, e, an, av, v):
|
|
|
|
if 'url_for' not in v:
|
|
|
|
raise KeyError("qweb: no 'url_for' found in context")
|
2013-10-14 15:04:42 +00:00
|
|
|
# Temporary implementation of t-keep-query until qweb py v2
|
|
|
|
keep_query = e.attributes.get('t-keep-query')
|
|
|
|
if keep_query:
|
|
|
|
params = self.eval_format(keep_query.value, v)
|
|
|
|
keep_query = [q.strip() for q in params.split(',')]
|
|
|
|
path = str(v['url_for'](self.eval_format(av, v), keep_query=keep_query))
|
2013-09-27 12:05:03 +00:00
|
|
|
return ' %s="%s"' % (an[2:], werkzeug.utils.escape(path))
|
2013-09-24 08:46:20 +00:00
|
|
|
|
2013-06-27 09:13:29 +00:00
|
|
|
# Tags
|
2013-06-27 15:08:07 +00:00
|
|
|
def render_tag_raw(self, e, t_att, g_att, v):
|
2013-06-30 21:35:15 +00:00
|
|
|
inner = self.eval_str(t_att["raw"], v)
|
2013-08-08 08:07:42 +00:00
|
|
|
return self.render_element(e, t_att, g_att, v, inner)
|
2013-06-27 15:08:07 +00:00
|
|
|
|
|
|
|
def render_tag_rawf(self, e, t_att, g_att, v):
|
2013-06-30 21:35:15 +00:00
|
|
|
inner = self.eval_format(t_att["rawf"], v)
|
2013-08-08 08:07:42 +00:00
|
|
|
return self.render_element(e, t_att, g_att, v, inner)
|
2013-06-27 15:08:07 +00:00
|
|
|
|
|
|
|
def render_tag_esc(self, e, t_att, g_att, v):
|
2013-09-26 14:51:26 +00:00
|
|
|
inner = werkzeug.utils.escape(self.eval_str(t_att["esc"], v))
|
2013-08-08 08:07:42 +00:00
|
|
|
return self.render_element(e, t_att, g_att, v, inner)
|
2013-06-27 15:08:07 +00:00
|
|
|
|
|
|
|
def render_tag_escf(self, e, t_att, g_att, v):
|
2013-09-26 14:51:26 +00:00
|
|
|
inner = werkzeug.utils.escape(self.eval_format(t_att["escf"], v))
|
2013-08-08 08:07:42 +00:00
|
|
|
return self.render_element(e, t_att, g_att, v, inner)
|
2013-06-27 15:08:07 +00:00
|
|
|
|
|
|
|
def render_tag_foreach(self, e, t_att, g_att, v):
|
|
|
|
expr = t_att["foreach"]
|
|
|
|
enum = self.eval_object(expr, v)
|
|
|
|
if enum is not None:
|
|
|
|
var = t_att.get('as', expr).replace('.', '_')
|
2013-10-04 11:41:55 +00:00
|
|
|
d = v.copy()
|
2013-06-27 15:08:07 +00:00
|
|
|
size = -1
|
2013-09-10 13:44:02 +00:00
|
|
|
if isinstance(enum, (list, tuple)):
|
2013-06-27 15:08:07 +00:00
|
|
|
size = len(enum)
|
|
|
|
elif hasattr(enum, 'count'):
|
|
|
|
size = enum.count()
|
|
|
|
d["%s_size" % var] = size
|
|
|
|
d["%s_all" % var] = enum
|
|
|
|
index = 0
|
|
|
|
ru = []
|
2013-06-27 09:13:29 +00:00
|
|
|
for i in enum:
|
2013-06-27 15:08:07 +00:00
|
|
|
d["%s_value" % var] = i
|
|
|
|
d["%s_index" % var] = index
|
|
|
|
d["%s_first" % var] = index == 0
|
|
|
|
d["%s_even" % var] = index % 2
|
|
|
|
d["%s_odd" % var] = (index + 1) % 2
|
|
|
|
d["%s_last" % var] = index + 1 == size
|
|
|
|
if index % 2:
|
|
|
|
d["%s_parity" % var] = 'odd'
|
2013-06-27 09:13:29 +00:00
|
|
|
else:
|
2013-06-27 15:08:07 +00:00
|
|
|
d["%s_parity" % var] = 'even'
|
2013-08-21 14:09:10 +00:00
|
|
|
if 'as' in t_att:
|
2013-06-27 15:08:07 +00:00
|
|
|
d[var] = i
|
2013-09-10 13:44:02 +00:00
|
|
|
elif isinstance(i, dict):
|
2013-08-21 14:09:10 +00:00
|
|
|
d.update(i)
|
2013-06-30 21:35:15 +00:00
|
|
|
ru.append(self.render_element(e, t_att, g_att, d))
|
2013-06-27 15:08:07 +00:00
|
|
|
index += 1
|
2013-06-27 09:13:29 +00:00
|
|
|
return "".join(ru)
|
|
|
|
else:
|
2013-08-21 14:09:10 +00:00
|
|
|
raise NameError("QWeb: foreach enumerator %r is not defined while rendering template %r" % (expr, v.get('__template__')))
|
2013-06-27 15:08:07 +00:00
|
|
|
|
|
|
|
def render_tag_if(self, e, t_att, g_att, v):
|
|
|
|
if self.eval_bool(t_att["if"], v):
|
2013-06-30 21:35:15 +00:00
|
|
|
return self.render_element(e, t_att, g_att, v)
|
2013-06-27 09:13:29 +00:00
|
|
|
else:
|
|
|
|
return ""
|
2013-06-27 15:08:07 +00:00
|
|
|
|
|
|
|
def render_tag_call(self, e, t_att, g_att, v):
|
2013-10-04 11:41:55 +00:00
|
|
|
d = v if 'import' in t_att else v.copy()
|
2013-06-30 21:35:15 +00:00
|
|
|
d[0] = self.render_element(e, t_att, g_att, d)
|
2013-10-04 11:41:55 +00:00
|
|
|
|
2013-10-18 14:48:38 +00:00
|
|
|
return self.render(None, None, self.eval_format(t_att["call"], d), d)
|
2013-06-27 15:08:07 +00:00
|
|
|
|
|
|
|
def render_tag_set(self, e, t_att, g_att, v):
|
2013-07-02 12:00:18 +00:00
|
|
|
if "value" in t_att:
|
|
|
|
v[t_att["set"]] = self.eval_object(t_att["value"], v)
|
2013-08-08 22:39:38 +00:00
|
|
|
elif "valuef" in t_att:
|
|
|
|
v[t_att["set"]] = self.eval_format(t_att["valuef"], v)
|
2013-06-27 09:13:29 +00:00
|
|
|
else:
|
2013-06-30 21:35:15 +00:00
|
|
|
v[t_att["set"]] = self.render_element(e, t_att, g_att, v)
|
2013-06-27 09:13:29 +00:00
|
|
|
return ""
|
2013-06-30 21:35:15 +00:00
|
|
|
|
2013-07-01 15:55:41 +00:00
|
|
|
def render_tag_field(self, e, t_att, g_att, v):
|
|
|
|
""" eg: <span t-record="browse_record(res.partner, 1)" t-field="phone">+1 555 555 8069</span>"""
|
2013-09-17 11:30:22 +00:00
|
|
|
node_name = e.nodeName
|
2013-09-27 12:45:23 +00:00
|
|
|
assert node_name not in ("table", "tbody", "thead", "tfoot", "tr", "td",
|
|
|
|
"ol", "ul", "ol", "dl", "dt", "dd"),\
|
2013-09-17 11:30:22 +00:00
|
|
|
"RTE widgets do not work correctly on %r elements" % node_name
|
2013-09-25 12:10:28 +00:00
|
|
|
assert node_name != 't',\
|
|
|
|
"t-field can not be used on a t element, provide an actual HTML node"
|
2013-08-04 20:23:31 +00:00
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
record, field_name = t_att["field"].rsplit('.', 1)
|
2013-08-04 20:27:20 +00:00
|
|
|
record = self.eval_object(record, v)
|
2013-07-01 15:55:41 +00:00
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
column = record._model._all_columns[field_name].column
|
|
|
|
options = json.loads(t_att.get('field-options') or '{}')
|
|
|
|
field_type = get_field_type(column, options)
|
2013-09-26 14:51:26 +00:00
|
|
|
|
2013-10-07 12:51:55 +00:00
|
|
|
converter = self.get_converter_for(field_type)
|
2013-09-26 14:51:26 +00:00
|
|
|
|
2013-10-18 14:45:46 +00:00
|
|
|
return converter.to_html(v.cr, v.uid, field_name, record, options,
|
|
|
|
e, t_att, g_att, v, context=v.context)
|
2013-10-07 09:15:56 +00:00
|
|
|
|
2013-10-07 12:51:55 +00:00
|
|
|
def get_converter_for(self, field_type):
|
|
|
|
return self.pool.get('ir.qweb.field.' + field_type,
|
|
|
|
self.pool['ir.qweb.field'])
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
|
|
|
|
class FieldConverter(osv.AbstractModel):
|
2013-10-07 12:51:55 +00:00
|
|
|
""" Used to convert a t-field specification into an output HTML field.
|
|
|
|
|
|
|
|
:meth:`~.to_html` is the entry point of this conversion from QWeb, it:
|
|
|
|
|
|
|
|
* converts the record value to html using :meth:`~.record_to_html`
|
|
|
|
* generates the metadata attributes (``data-oe-``) to set on the root
|
|
|
|
result node
|
|
|
|
* generates the root result node itself through :meth:`~.render_element`
|
|
|
|
"""
|
2013-10-07 09:15:56 +00:00
|
|
|
_name = 'ir.qweb.field'
|
|
|
|
|
|
|
|
def attributes(self, cr, uid, field_name, record, options,
|
2013-10-21 08:31:20 +00:00
|
|
|
source_element, g_att, t_att, qweb_context,
|
|
|
|
context=None):
|
2013-10-07 12:51:55 +00:00
|
|
|
"""
|
|
|
|
Generates the metadata attributes (prefixed by ``data-oe-`` for the
|
|
|
|
root node of the field conversion. Attribute values are escaped by the
|
|
|
|
parent using ``werkzeug.utils.escape``.
|
|
|
|
|
|
|
|
The default attributes are:
|
|
|
|
|
|
|
|
* ``model``, the name of the record's model
|
|
|
|
* ``id`` the id of the record to which the field belongs
|
|
|
|
* ``field`` the name of the converted field
|
|
|
|
* ``type`` the logical field type (widget, may not match the column's
|
|
|
|
``type``, may not be any _column subclass name)
|
|
|
|
* ``translate``, a boolean flag (``0`` or ``1``) denoting whether the
|
|
|
|
column is translatable
|
|
|
|
* ``expression``, the original expression
|
|
|
|
|
|
|
|
:returns: iterable of (attribute name, attribute value) pairs.
|
|
|
|
"""
|
2013-10-07 09:15:56 +00:00
|
|
|
column = record._model._all_columns[field_name].column
|
|
|
|
field_type = get_field_type(column, options)
|
|
|
|
return [
|
|
|
|
('data-oe-model', record._model._name),
|
|
|
|
('data-oe-id', record.id),
|
|
|
|
('data-oe-field', field_name),
|
|
|
|
('data-oe-type', field_type),
|
|
|
|
('data-oe-expression', t_att['field']),
|
|
|
|
]
|
|
|
|
|
2013-10-21 08:31:20 +00:00
|
|
|
def value_to_html(self, cr, uid, value, column, options=None, context=None):
|
2013-10-07 09:15:56 +00:00
|
|
|
""" Converts a single value to its HTML version/output
|
|
|
|
"""
|
2013-10-30 15:41:05 +00:00
|
|
|
if not value: return ''
|
2013-10-07 09:15:56 +00:00
|
|
|
return werkzeug.utils.escape(value)
|
|
|
|
|
2013-10-21 08:31:20 +00:00
|
|
|
def record_to_html(self, cr, uid, field_name, record, column, options=None, context=None):
|
2013-10-07 12:51:55 +00:00
|
|
|
""" Converts the specified field of the browse_record ``record`` to
|
|
|
|
HTML
|
2013-10-07 09:15:56 +00:00
|
|
|
"""
|
|
|
|
return self.value_to_html(
|
2013-10-21 08:31:20 +00:00
|
|
|
cr, uid, record[field_name], column, options=None, context=context)
|
2013-10-07 09:15:56 +00:00
|
|
|
|
|
|
|
def to_html(self, cr, uid, field_name, record, options,
|
2013-10-18 14:45:46 +00:00
|
|
|
source_element, t_att, g_att, qweb_context, context=None):
|
2013-10-07 12:51:55 +00:00
|
|
|
""" Converts a ``t-field`` to its HTML output. A ``t-field`` may be
|
|
|
|
extended by a ``t-field-options``, which is a JSON-serialized mapping
|
|
|
|
of configuration values.
|
|
|
|
|
|
|
|
A default configuration key is ``widget`` which can override the
|
|
|
|
field's own ``_type``.
|
2013-10-07 09:15:56 +00:00
|
|
|
"""
|
2013-09-26 14:51:26 +00:00
|
|
|
content = None
|
2013-07-02 16:48:05 +00:00
|
|
|
try:
|
2013-10-07 09:15:56 +00:00
|
|
|
content = self.record_to_html(
|
|
|
|
cr, uid, field_name, record,
|
|
|
|
record._model._all_columns[field_name].column,
|
2013-10-21 08:31:20 +00:00
|
|
|
options, context=context)
|
2013-10-11 11:16:20 +00:00
|
|
|
except Exception:
|
|
|
|
_logger.warning("Could not get field %s for model %s",
|
|
|
|
field_name, record._model._name, exc_info=True)
|
2013-07-01 15:55:41 +00:00
|
|
|
|
2013-09-26 14:51:26 +00:00
|
|
|
g_att += ''.join(
|
|
|
|
' %s="%s"' % (name, werkzeug.utils.escape(value))
|
2013-10-07 09:15:56 +00:00
|
|
|
for name, value in self.attributes(
|
|
|
|
cr, uid, field_name, record, options,
|
|
|
|
source_element, g_att, t_att, qweb_context)
|
2013-09-26 14:51:26 +00:00
|
|
|
)
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
return self.render_element(cr, uid, source_element, t_att, g_att,
|
|
|
|
qweb_context, content)
|
|
|
|
|
2013-10-11 11:16:20 +00:00
|
|
|
def qweb_object(self):
|
|
|
|
return self.pool['ir.qweb']
|
|
|
|
|
2013-10-07 12:51:55 +00:00
|
|
|
def render_element(self, cr, uid, source_element, t_att, g_att,
|
|
|
|
qweb_context, content):
|
|
|
|
""" Final rendering hook, by default just calls ir.qweb's ``render_element``
|
|
|
|
"""
|
2013-10-11 11:16:20 +00:00
|
|
|
return self.qweb_object().render_element(
|
2013-10-07 09:15:56 +00:00
|
|
|
source_element, t_att, g_att, qweb_context, content or '')
|
|
|
|
|
2013-10-21 12:57:23 +00:00
|
|
|
def user_lang(self, cr, uid, context):
|
|
|
|
"""
|
|
|
|
Fetches the res.lang object corresponding to the language code stored
|
|
|
|
in the user's context. Fallbacks to en_US if no lang is present in the
|
|
|
|
context *or the language code is not valid*.
|
|
|
|
|
|
|
|
:returns: res.lang browse_record
|
|
|
|
"""
|
|
|
|
if context is None: context = {}
|
|
|
|
|
|
|
|
lang_code = context.get('lang') or 'en_US'
|
|
|
|
Lang = self.pool['res.lang']
|
|
|
|
|
|
|
|
lang_ids = Lang.search(cr, uid, [('code', '=', lang_code)], context=context) \
|
|
|
|
or Lang.search(cr, uid, [('code', '=', 'en_US')], context=context)
|
|
|
|
|
|
|
|
return Lang.browse(cr, uid, lang_ids[0], context=context)
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
class FloatConverter(osv.AbstractModel):
|
|
|
|
_name = 'ir.qweb.field.float'
|
|
|
|
_inherit = 'ir.qweb.field'
|
|
|
|
|
2013-10-21 09:16:07 +00:00
|
|
|
def precision(self, cr, uid, column, options=None, context=None):
|
|
|
|
_, precision = column.digits or (None, None)
|
|
|
|
return precision
|
|
|
|
|
2013-10-21 08:31:20 +00:00
|
|
|
def value_to_html(self, cr, uid, value, column, options=None, context=None):
|
2013-10-21 09:16:07 +00:00
|
|
|
precision = self.precision(cr, uid, column, options=options, context=context)
|
2013-10-22 08:54:36 +00:00
|
|
|
fmt = '%f' if precision is None else '%.{precision}f'
|
2013-10-21 12:57:23 +00:00
|
|
|
|
|
|
|
lang = self.user_lang(cr, uid, context)
|
2013-10-07 09:15:56 +00:00
|
|
|
|
2013-10-22 08:54:36 +00:00
|
|
|
formatted = lang.format(fmt.format(precision=precision),
|
|
|
|
value, grouping=True)
|
|
|
|
# %f does not strip trailing zeroes. %g does but its precision causes
|
|
|
|
# it to switch to scientific notation starting at a million *and* to
|
|
|
|
# strip decimals. So use %f and if no precision was specified manually
|
|
|
|
# strip trailing 0.
|
|
|
|
if not precision:
|
|
|
|
formatted = re.sub(r'(?:(0|\d+?)0+)$', r'\1', formatted)
|
|
|
|
return werkzeug.utils.escape(formatted)
|
2013-10-07 09:15:56 +00:00
|
|
|
|
2013-10-21 15:26:39 +00:00
|
|
|
class DateConverter(osv.AbstractModel):
|
|
|
|
_name = 'ir.qweb.field.date'
|
|
|
|
_inherit = 'ir.qweb.field'
|
|
|
|
|
|
|
|
def value_to_html(self, cr, uid, value, column, options=None, context=None):
|
2013-10-30 15:41:05 +00:00
|
|
|
if not value: return ''
|
2013-10-21 15:26:39 +00:00
|
|
|
lang = self.user_lang(cr, uid, context=context)
|
|
|
|
|
|
|
|
parse_format = openerp.tools.DEFAULT_SERVER_DATE_FORMAT
|
|
|
|
out_format = lang.date_format.encode('utf-8')
|
|
|
|
|
|
|
|
if isinstance(value, basestring):
|
|
|
|
value = datetime.datetime.strptime(value, parse_format)
|
|
|
|
|
|
|
|
return value.strftime(out_format)
|
|
|
|
|
|
|
|
class DateTimeConverter(osv.AbstractModel):
|
|
|
|
_name = 'ir.qweb.field.datetime'
|
|
|
|
_inherit = 'ir.qweb.field'
|
|
|
|
|
|
|
|
def value_to_html(self, cr, uid, value, column, options=None, context=None):
|
2013-10-30 15:41:05 +00:00
|
|
|
if not value: return ''
|
2013-10-21 15:26:39 +00:00
|
|
|
lang = self.user_lang(cr, uid, context=context)
|
|
|
|
|
|
|
|
parse_format = openerp.tools.DEFAULT_SERVER_DATETIME_FORMAT
|
|
|
|
out_format = (u"%s %s" % (lang.date_format, lang.time_format)).encode('utf-8')
|
|
|
|
|
|
|
|
if isinstance(value, basestring):
|
|
|
|
value = datetime.datetime.strptime(value, parse_format)
|
|
|
|
|
|
|
|
value = column.context_timestamp(
|
|
|
|
cr, uid, timestamp=value, context=context)
|
|
|
|
|
|
|
|
return value.strftime(out_format)
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
class TextConverter(osv.AbstractModel):
|
|
|
|
_name = 'ir.qweb.field.text'
|
|
|
|
_inherit = 'ir.qweb.field'
|
|
|
|
|
2013-10-21 08:31:20 +00:00
|
|
|
def value_to_html(self, cr, uid, value, column, options=None, context=None):
|
2013-10-07 09:15:56 +00:00
|
|
|
"""
|
|
|
|
Escapes the value and converts newlines to br. This is bullshit.
|
|
|
|
"""
|
2013-10-30 15:41:05 +00:00
|
|
|
if not value: return ''
|
2013-10-07 09:15:56 +00:00
|
|
|
return werkzeug.utils.escape(value).replace('\n', '<br>\n')
|
|
|
|
|
|
|
|
class SelectionConverter(osv.AbstractModel):
|
|
|
|
_name = 'ir.qweb.field.selection'
|
|
|
|
_inherit = 'ir.qweb.field'
|
|
|
|
|
2013-10-21 08:31:20 +00:00
|
|
|
def record_to_html(self, cr, uid, field_name, record, column, options=None, context=None):
|
2013-10-07 09:15:56 +00:00
|
|
|
value = record[field_name]
|
2013-10-30 22:07:38 +00:00
|
|
|
if not value: return ''
|
2013-10-07 09:15:56 +00:00
|
|
|
selection = dict(fields.selection.reify(
|
|
|
|
cr, uid, record._model, column))
|
|
|
|
return self.value_to_html(
|
|
|
|
cr, uid, selection[value], column, options=options)
|
|
|
|
|
|
|
|
class ManyToOneConverter(osv.AbstractModel):
|
|
|
|
_name = 'ir.qweb.field.many2one'
|
|
|
|
_inherit = 'ir.qweb.field'
|
|
|
|
|
2013-10-21 08:31:20 +00:00
|
|
|
def value_to_html(self, cr, uid, value, column, options=None, context=None):
|
2013-10-14 10:56:21 +00:00
|
|
|
# value may be a browse_null
|
|
|
|
if not value: return ''
|
2013-10-25 12:47:20 +00:00
|
|
|
name_got = value._model.name_get(
|
|
|
|
cr, openerp.SUPERUSER_ID, value.id, context=context)[0][1]
|
|
|
|
return werkzeug.utils.escape(name_got).replace('\n', '<br>\n')
|
2013-10-07 09:15:56 +00:00
|
|
|
|
|
|
|
class HTMLConverter(osv.AbstractModel):
|
|
|
|
_name = 'ir.qweb.field.html'
|
|
|
|
_inherit = 'ir.qweb.field'
|
|
|
|
|
2013-10-21 08:31:20 +00:00
|
|
|
def value_to_html(self, cr, uid, value, column, options=None, context=None):
|
2013-10-30 15:41:05 +00:00
|
|
|
return value or ''
|
2013-10-07 09:15:56 +00:00
|
|
|
|
2013-10-07 11:31:33 +00:00
|
|
|
class ImageConverter(osv.AbstractModel):
|
2013-10-07 12:51:55 +00:00
|
|
|
""" ``image`` widget rendering, inserts a data:uri-using image tag in the
|
|
|
|
document. May be overridden by e.g. the website module to generate links
|
|
|
|
instead.
|
|
|
|
|
|
|
|
.. todo:: what happens if different output need different converters? e.g.
|
|
|
|
reports may need embedded images or FS links whereas website
|
|
|
|
needs website-aware
|
|
|
|
"""
|
2013-10-07 11:31:33 +00:00
|
|
|
_name = 'ir.qweb.field.image'
|
2013-10-07 09:15:56 +00:00
|
|
|
_inherit = 'ir.qweb.field'
|
|
|
|
|
2013-10-21 08:31:20 +00:00
|
|
|
def value_to_html(self, cr, uid, value, column, options=None, context=None):
|
2013-10-07 09:15:56 +00:00
|
|
|
try:
|
|
|
|
image = Image.open(cStringIO.StringIO(value.decode('base64')))
|
2013-10-07 11:31:33 +00:00
|
|
|
image.verify()
|
2013-10-07 09:15:56 +00:00
|
|
|
except IOError:
|
|
|
|
raise ValueError("Non-image binary fields can not be converted to HTML")
|
2013-10-07 12:51:55 +00:00
|
|
|
except: # image.verify() throws "suitable exceptions", I have no idea what they are
|
2013-10-07 09:15:56 +00:00
|
|
|
raise ValueError("Invalid image content")
|
|
|
|
|
2013-10-07 11:31:33 +00:00
|
|
|
return '<img src="data:%s;base64,%s">' % (Image.MIME[image.format], value)
|
2013-10-07 09:15:56 +00:00
|
|
|
|
2013-10-08 12:20:06 +00:00
|
|
|
class MonetaryConverter(osv.AbstractModel):
|
2013-10-11 14:25:12 +00:00
|
|
|
""" ``monetary`` converter, has a mandatory option
|
|
|
|
``display_currency``.
|
2013-10-07 14:53:46 +00:00
|
|
|
|
2013-10-21 10:05:43 +00:00
|
|
|
The currency is used for formatting *and rounding* of the float value. It
|
|
|
|
is assumed that the linked res_currency has a non-empty rounding value and
|
|
|
|
res.currency's ``round`` method is used to perform rounding.
|
|
|
|
|
2013-10-11 11:16:20 +00:00
|
|
|
.. note:: the monetary converter internally adds the qweb context to its
|
|
|
|
options mapping, so that the context is available to callees.
|
|
|
|
It's set under the ``_qweb_context`` key.
|
2013-10-07 14:53:46 +00:00
|
|
|
"""
|
2013-10-08 12:20:06 +00:00
|
|
|
_name = 'ir.qweb.field.monetary'
|
2013-10-07 14:53:46 +00:00
|
|
|
_inherit = 'ir.qweb.field'
|
|
|
|
|
2013-10-11 11:16:20 +00:00
|
|
|
def to_html(self, cr, uid, field_name, record, options,
|
2013-10-21 08:31:20 +00:00
|
|
|
source_element, t_att, g_att, qweb_context, context=None):
|
2013-10-11 11:16:20 +00:00
|
|
|
options['_qweb_context'] = qweb_context
|
|
|
|
return super(MonetaryConverter, self).to_html(
|
|
|
|
cr, uid, field_name, record, options,
|
2013-10-21 08:31:20 +00:00
|
|
|
source_element, t_att, g_att, qweb_context, context=context)
|
2013-10-11 11:16:20 +00:00
|
|
|
|
2013-10-21 08:31:20 +00:00
|
|
|
def record_to_html(self, cr, uid, field_name, record, column, options, context=None):
|
2013-10-14 09:36:57 +00:00
|
|
|
Currency = self.pool['res.currency']
|
2013-10-11 11:16:20 +00:00
|
|
|
display = self.display_currency(cr, uid, options)
|
|
|
|
|
2013-10-21 12:57:23 +00:00
|
|
|
# lang.format mandates a sprintf-style format. These formats are non-
|
|
|
|
# minimal (they have a default fixed precision instead), and
|
|
|
|
# lang.format will not set one by default. currency.round will not
|
|
|
|
# provide one either. So we need to generate a precision value
|
|
|
|
# (integer > 0) from the currency's rounding (a float generally < 1.0).
|
|
|
|
#
|
|
|
|
# The log10 of the rounding should be the number of digits involved if
|
|
|
|
# negative, if positive clamp to 0 digits and call it a day.
|
|
|
|
# nb: int() ~ floor(), we want nearest rounding instead
|
|
|
|
precision = int(round(math.log10(display.rounding)))
|
|
|
|
fmt = "%.{0}f".format(-precision if precision < 0 else 0)
|
|
|
|
|
|
|
|
lang = self.user_lang(cr, uid, context=context)
|
|
|
|
formatted_amount = lang.format(
|
|
|
|
fmt, Currency.round(cr, uid, display, record[field_name]),
|
|
|
|
grouping=True, monetary=True)
|
|
|
|
|
|
|
|
pre = post = u''
|
2013-10-11 11:16:20 +00:00
|
|
|
if display.position == 'before':
|
2013-10-21 12:57:23 +00:00
|
|
|
pre = u'{symbol} '
|
2013-10-07 15:03:30 +00:00
|
|
|
else:
|
2013-10-21 12:57:23 +00:00
|
|
|
post = u' {symbol}'
|
|
|
|
|
|
|
|
return u'{pre}<span class="oe_currency_value">{0}</span>{post}'.format(
|
|
|
|
formatted_amount,
|
|
|
|
pre=pre, post=post,
|
|
|
|
).format(
|
|
|
|
symbol=display.symbol,
|
|
|
|
)
|
2013-10-07 14:53:46 +00:00
|
|
|
|
2013-10-11 11:16:20 +00:00
|
|
|
def display_currency(self, cr, uid, options):
|
|
|
|
return self.qweb_object().eval_object(
|
|
|
|
options['display_currency'], options['_qweb_context'])
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
def get_field_type(column, options):
|
|
|
|
""" Gets a t-field's effective type from the field's column and its options
|
|
|
|
"""
|
|
|
|
return options.get('widget', column._type)
|
2013-07-01 15:55:41 +00:00
|
|
|
|
2013-10-14 08:52:15 +00:00
|
|
|
# vim:et:
|