Help

0

Replacing an email template using custom modul

Hi all,

actually Im on odoo13 and try to replace the password reset email (and some others) by installing my own custom modul.

auth_signup.set_password_email

Just updating that one using...

<record id="auth_signup.set_password_email" model="mail.template">
....

Does not work because of the no_update flag.

Just deleting that email template and recreating it did work in odoo 10 but not in odoo 13.
Here it just complains a missing external_identifier.
Fiddling with the update flag might work but I dont like this approach.
There must be a better way.


Some reasearch revealed that the best way to that would be to inherit the auth_singnup model, create my own template and then create a function to redirect the email sending to my new template.

As a newbe I have some ideas what that inheritance would be all about, but at the function stuff Im still lost.

Could anybody provide me with some sample code for that passsword reset mail that I can use to build that into my module?

 

Next thing is:

How to replace that email template for multiple languages using a custom modul?


How to do that in the UI was easy to find out.

But what I need is to do that using a custom module...


Thx and cheers

Stephan


Avatar
Discard
2 Answers
0
Best Answer

Hi Ermin,

So what is your suggestion?

I tried quite some ideas to solve that problem,  but none of them worked...

Cheers

Stephan

1 Comment
Avatar
Discard
Avatar
Ermin Trevisan
-

Hi Stephan, if I would know the answer already, I would have posted it as answer :-)

I have just made this comment for you and others in order to recommend to dig deeper. I have no time right now to do that myself.

0
Best Answer

Hi all.

Found a solution myself.
Its a quite forceful approach but nothing else worked till now.

Basic Idea:
Don't replace or modify the jinja email templates.
Just replace its translation!

That approach works for any language, even the base language.
The html bodies and subjects of those jinja email templates are used as  keys to find a translation.
If its present it will be used instead the original text elements.

Changing existing translations could be easily done using the email template dialog at the technical menu.
But how to change it by installing a module?
Usually already existing translations are not replaced by a custom moduls .po file.
For that to achieve you have the reload the entire language with the replace option checked.
Especially when the translation resides in a module like base, sales or any other core module this approach does not help because reloading the language in full will restore the original templates too.

To change those translations they have to be deleted and to be created anew by your module.
Fortunately we have a tool at hand to achive exactly this.

Here are the steps to change the email templates of a given language.

1.) Make a backup of your database in case something goes awfully wrong.

No kidding. There may happen some weired things with the translation mechanism.
At least on my side I broke the translation several times by now and the only way back was to restore a proper backup of the whole system. As soon as you have the replacement modul working, its stable and easy to use though.

2.) Edit the template translation manually using the email template dialog at the technical menu and test them

Yes, its way more convenient to 'develop' and 'test' the new templates on a dev, test or staging environment using the ui instead of a installable module. So you have always a live backup at hand (at the production instance) in case you need to revert to a proper working template translation.

3.) Find out the names of the modules that are inplementing the email templates you want to change.

In my case the list contained:

  1. account

  2. auth_signup

  3. purchase

  4. sale

  5. website_portal

  6. website_sale

But this depends on your installed modules.

4.) Export the translations of those modules for your desired language into a single .po file

5.) Create a module that deletes all email template translations of your choice by issuing an xml statement like this:

<delete model="ir.translation" search="[('name','like','mail.template'),('lang', '=','de_DE')]"/>

6.) Place the above created .po file (in this case with the name 'de.po') inside the translation folder of the modul.

This will whipe all german email template translations and replaces them with the ones defined in the de.po file as soon as you install or upgrade the modul.

During development of your templates that approach is easy to use and only requires an update of that modul as soon as you change the content of the modules .po file.

As soon as you are done with development of the templates you should fine tune the module.

Find proper domain search clauses to delete just the template you want to change and not just all of them. This usually requires to look into the original src column of the translation records to find something unique and then create proper delete xml's like this:

<!-- Delete german mail template translations -->
<!-- Template body and subject will be loaded again via the translation de.po file -->
<!-- Auth Signup: Odoo Account Created -->
<delete model="ir.translation" search="[('module', '=','auth_signup'),('name','=','mail.template,subject'),('lang', '=','de_DE'),('src','=','Welcome to ${object.company_id.name}!')]"/>
<delete model="ir.translation" search="[('module', '=','auth_signup'),('name','=','mail.template,body_html'),('lang', '=','de_DE'),('src','like','Your account has been successfully created!')]"/>
<!-- Auth Signup: Odoo Connection -->
<delete model="ir.translation" search="[('module', '=','auth_signup'),('name','=','mail.template,subject'),('lang', '=','de_DE'),('src','=','${object.create_uid.name} from ${object.company_id.name} invites you to connect to Odoo')]"/>
<delete model="ir.translation" search="[('module', '=','auth_signup'),('name','=','mail.template,body_html'),('lang', '=','de_DE'),('src','like','You have been invited by ${object.create_uid.name} of ${object.company_id.name} to connect on Odoo')]"/>
This code snippet deletes just the auth sign up and account creation email template by selecting their respective html body and subject translation. The .po file of the module then only need to contain those deleted translation records.
To find those entries in the .po file and extract them into a reduced .po file could easily be done using a text editor with search capability. My tool of choice for all odoo development tasks including this is PyCharm.
However one should be quite familar with the structure of .po files to not break the very lengthy html_body translations. Especially when it comes to further or later changes to those templates. Editing the content of such a reduced .po files is better done using  POEdit or like tools designed for such translation files to avoid any formating issues.

Regarding the name of your modul I would suggest to place a 'x_' in front like 'x_MyEmailTemplates'. This ensures that your module is the last one processed when it comes to upgrades.


Good luck and remember taking a backup first prior issuing any delete statements that mangle with translations or any other table at the database. Otherwise you may not be able to recover from any mistake easily without fully reinstalling odoo from scratch.

Cheers
Stephan

1 Comment
Avatar
Discard
Avatar
Ermin Trevisan
-

IMHO this is not the way to go.