Search code examples
pythonodoo

AttributeError: module 'odoo.addons.mail.models.mail_template' has no attribute 'mako_template_env'


This Function works perfectly in odoo 13, but i am migrating the app to odoo 15 and the function is now throwing an AttributeError: module 'odoo.addons.mail.models.mail_template' has no attribute 'mako_template_env'

here's the function;

def render_doc(self, doctype):
        types = {'cert': 'Certificate', 'pol_doc': 'Policy Document',
                 'guarantee_cert': 'Guarantee Policy Certificate'}
        if doctype not in types.keys():
            raise UserError(_('Document must be of type cert or pol_doc'))

        prod_doc_ids = self.policy_id.product_id.product_doc_ids
        doc = prod_doc_ids.filtered(
            lambda c: c.doc_type == doctype) if prod_doc_ids else False
        if doc:
            body = doc.body
        else:
            body = f"<p><strong>{types[doctype]}</strong> is not yet \
                setup for <strong>{self.policy_id.product_id.name}.</strong>\
                Please contact the system administrator.</p>"
        return mail_template.mako_template_env.from_string(body).render({'object': self})

It seems odoo 15 dont have the "mako_template_env" or was it renamed? any help will be appreciated.


Solution

  • If you check clean code about rendering in mail_template , you will see that Odoo replaced mako_template_env with jinja_template_envin v14 and replaced jinja with qweb in v15:

    You can use the inline_template rendering engine:

    We still want user to use dynamic variables for some char fields (eg. subject, from, to, ...). We made a new rendering engine called "inline_template" that will render an expression enclosed by {{ and }}.

    Example:

    self.env['mail.render.mixin']._render_template(
        template_src=body,
        model=self._name,
        res_ids=self.ids,
        add_context={'types': types}
    )
    

    It will return a dict ({res_id: string of rendered template based on record})