[IMP] email_template: add a lambda function to allow a user to format a date from a template mail into a specific timezone, and/or in a specific format.
Eg: ${format_tz(object.name, object.employee_id.address_id.tz,'%m/%d/%Y %H:%M')}
This commit is contained in:
parent
6655a020c3
commit
581275a63c
|
@ -34,9 +34,34 @@ from openerp import tools
|
||||||
from openerp.tools.translate import _
|
from openerp.tools.translate import _
|
||||||
from urllib import urlencode, quote as quote
|
from urllib import urlencode, quote as quote
|
||||||
|
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def format_tz(pool, cr, uid, dt, tz=False, format=False, context=None):
|
||||||
|
context = dict(context or {})
|
||||||
|
if tz:
|
||||||
|
context['tz'] = tz or pool.get('res.users').read(cr, SUPERUSER_ID, uid, ['tz'])['tz'] or "UTC"
|
||||||
|
timestamp = datetime.datetime.strptime(dt, tools.DEFAULT_SERVER_DATETIME_FORMAT)
|
||||||
|
|
||||||
|
ts = fields.datetime.context_timestamp(cr, uid, timestamp, context)
|
||||||
|
|
||||||
|
if format:
|
||||||
|
return ts.strftime(format)
|
||||||
|
else:
|
||||||
|
lang = context.get("lang")
|
||||||
|
lang_params = {}
|
||||||
|
if lang:
|
||||||
|
res_lang = pool.get('res.lang')
|
||||||
|
ids = res_lang.search(cr, uid, [("code", "=", lang)])
|
||||||
|
if ids:
|
||||||
|
lang_params = res_lang.read(cr, uid, ids[0], ["date_format", "time_format"])
|
||||||
|
format_date = lang_params.get("date_format", '%B-%d-%Y')
|
||||||
|
format_time = lang_params.get("time_format", '%I-%M %p')
|
||||||
|
|
||||||
|
fdate = ts.strftime(format_date)
|
||||||
|
ftime = ts.strftime(format_time)
|
||||||
|
return "%s %s (%s)" % (fdate, ftime, tz)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# We use a jinja2 sandboxed environment to render mako templates.
|
# We use a jinja2 sandboxed environment to render mako templates.
|
||||||
# Note that the rendering does not cover all the mako syntax, in particular
|
# Note that the rendering does not cover all the mako syntax, in particular
|
||||||
|
@ -164,6 +189,7 @@ class email_template(osv.osv):
|
||||||
user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
|
user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
|
||||||
records = self.pool[model].browse(cr, uid, res_ids, context=context) or [None]
|
records = self.pool[model].browse(cr, uid, res_ids, context=context) or [None]
|
||||||
variables = {
|
variables = {
|
||||||
|
'format_tz': lambda dt, tz=False, format=False: format_tz(self.pool, cr, uid, dt, tz, format, context),
|
||||||
'user': user,
|
'user': user,
|
||||||
'ctx': context, # context kw would clash with mako internals
|
'ctx': context, # context kw would clash with mako internals
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue