From f069560876c7ff5ee77f74900091ac311fe3d34d Mon Sep 17 00:00:00 2001 From: Luis Carlos Bautista Date: Mon, 22 Jul 2024 19:24:39 +0200 Subject: [PATCH] imp: pass basic variables and localizer to report environment --- __init__.py | 3 +-- poweremail_template.py | 17 ++++++++++------- utils.py | 22 ++++++++++++++++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 utils.py diff --git a/__init__.py b/__init__.py index df754a9..30db42f 100644 --- a/__init__.py +++ b/__init__.py @@ -29,8 +29,7 @@ from . import poweremail_mailbox from . import poweremail_serveraction from . import wizard - -from . import wizard +from . import utils import logging diff --git a/poweremail_template.py b/poweremail_template.py index d4ee1c1..c26dd65 100644 --- a/poweremail_template.py +++ b/poweremail_template.py @@ -73,6 +73,7 @@ import pooler from .poweremail_mailbox import _priority_selection from .poweremail_core import get_email_default_lang +from .utils import Localizer def send_on_create(self, cr, uid, vals, context=None): @@ -184,14 +185,12 @@ def get_value(cursor, user, recid, message=None, template=None, context=None): if not context: context = {} ctx = context.copy() - ctx.update({'browse_reference': True}) - object = pool.get( - template.object_name.model - ).browse(cursor, user, recid, ctx) + ctx['browse_reference'] = True + ctx['lang'] = context.get('lang', get_email_default_lang()) + object = pool.get(template.object_name.model).simple_browse(cursor, user, recid, context=ctx) env = context.copy() env.update({ - 'user': pool.get('res.users').browse(cursor, user, user, - context), + 'user': pool.get('res.users').simple_browse(cursor, user, user, context=ctx), 'db': cursor.dbname }) if template.template_language == 'mako': @@ -201,12 +200,16 @@ def get_value(cursor, user, recid, message=None, template=None, context=None): templ = MakoTemplate(message, input_encoding='utf-8', lookup=addons_lookup) extra_render_values = env.get('extra_render_values', {}) or {} values = { + 'pool': object.pool, + 'cursor': cursor, + 'uid': user, 'object': object, 'peobject': object, 'env': env, 'format_exceptions': True, 'template': template, - 'lang': context.get('lang', config.get('default_lang', 'en_US')), + 'lang': ctx['lang'], + 'localize': Localizer(cursor, user, ctx['lang']) } values.update(extra_render_values) reply = templ.render_unicode(**values) diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..f9b3814 --- /dev/null +++ b/utils.py @@ -0,0 +1,22 @@ +from datetime import datetime +import pooler + + +class Localizer(object): + def __init__(self, cursor, uid, lang): + pool = pooler.get_pool(cursor.dbname) + + lang_o = pool.get('res.lang') + lang_id = lang_o.search(cursor, uid, [('code', '=', lang)])[0] + self.lang = lang_o.simple_browse(cursor, uid, lang_id) + + def amount(self, amount, digits=2, monetary=True): + return self.lang.format('%.{}f'.format(digits), amount, monetary=monetary) + + def date(self, date_str): + new_format = self.lang.date_format + return datetime.strptime(date_str, "%Y-%m-%d").strftime(new_format) + + def datetime(self, datetime_str): + new_format = "{} {}".format(self.lang.date_format, self.lang.time_format) + return datetime.strptime(datetime_str, "%Y-%m-%d").strftime(new_format)