Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poder importar ficheros .mako en el cuerpo de la plantilla de Poweremail y nuevas variables en el scope de la plantilla #163

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 17 additions & 7 deletions poweremail_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@
from osv import osv, fields
from tools.translate import _
from tools.safe_eval import safe_eval
from tools import config
#Try and check the available templating engines
from mako.template import Template #For backward combatibility
try:
from mako.template import Template as MakoTemplate
from mako.lookup import TemplateLookup
from mako import exceptions
TEMPLATE_ENGINES.append(('mako', 'Mako Templates'))
except:
Expand Down Expand Up @@ -71,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 @@ -182,24 +185,31 @@ 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':
templ = MakoTemplate(message, input_encoding='utf-8')
addons_lookup = TemplateLookup(
directories=[config['addons_path']], input_encoding='utf-8'
)
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': 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)