Skip to content

Commit

Permalink
imp: pass basic variables and localizer to report environment
Browse files Browse the repository at this point in the history
  • Loading branch information
lcbautista committed Jul 23, 2024
1 parent 45a1a51 commit f069560
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
3 changes: 1 addition & 2 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
from . import poweremail_mailbox
from . import poweremail_serveraction
from . import wizard

from . import wizard
from . import utils

import logging

Expand Down
17 changes: 10 additions & 7 deletions poweremail_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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':
Expand All @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit f069560

Please sign in to comment.