Merge lp:~therp-nl/server-env-tools/7.0-email_template_template into lp:~server-env-tools-core-editors/server-env-tools/7.0

Proposed by Holger Brunn (Therp)
Status: Merged
Merged at revision: 40
Proposed branch: lp:~therp-nl/server-env-tools/7.0-email_template_template
Merge into: lp:~server-env-tools-core-editors/server-env-tools/7.0
Diff against target: 277 lines (+250/-0)
5 files modified
email_template_template/__init__.py (+1/-0)
email_template_template/__openerp__.py (+96/-0)
email_template_template/model/__init__.py (+21/-0)
email_template_template/model/email_template.py (+61/-0)
email_template_template/view/email_template.xml (+71/-0)
To merge this branch: bzr merge lp:~therp-nl/server-env-tools/7.0-email_template_template
Reviewer Review Type Date Requested Status
Guewen Baconnier @ Camptocamp code review, no test Approve
Review via email: mp+166677@code.launchpad.net

Commit message

[ADD] email_template_template

Description of the change

To post a comment you must log in.
38. By Holger Brunn (Therp)

[IMP] add a usage example in the description

Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) wrote :

LGTM as for the 6.1 version

review: Approve (code review, no test)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added directory 'email_template_template'
=== added file 'email_template_template/__init__.py'
--- email_template_template/__init__.py 1970-01-01 00:00:00 +0000
+++ email_template_template/__init__.py 2013-06-17 09:05:36 +0000
@@ -0,0 +1,1 @@
1import model
02
=== added file 'email_template_template/__openerp__.py'
--- email_template_template/__openerp__.py 1970-01-01 00:00:00 +0000
+++ email_template_template/__openerp__.py 2013-06-17 09:05:36 +0000
@@ -0,0 +1,96 @@
1# -*- coding: utf-8 -*-
2##############################################################################
3#
4# OpenERP, Open Source Management Solution
5# This module copyright (C) 2013 Therp BV (<http://therp.nl>).
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU Affero General Public License as
9# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Affero General Public License for more details.
16#
17# You should have received a copy of the GNU Affero General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20##############################################################################
21{
22 "name": "Templates for email templates",
23 "version": "1.0",
24 "author": "Therp BV",
25 "category": 'Tools',
26 'complexity': "expert",
27 "description": """If an organisation's email layout is a bit more
28complicated, changes can be tedious when having to do that across several email
29templates. So this addon allows to define templates for mails that is referenced
30by other mail templates.
31This way we can put the layout parts into the template template and only content
32in the other templates. Changing the layout is then only a matter of changing
33the template template.
34
35
36Usage:
37Create an email template with the related document model 'Email Templates'. Now
38most of the fields gray out and you can only edit body_text and body_html. Be
39sure to use ${body_text} and ${body_html} respectively in your template
40template.
41
42Then select this newly created template templates in one of your actual
43templates.
44
45For example, create a template template
46-----
47Example Corp logo
48Example Corp header
49${object.body_text} <- this gets evaluated to the body_text of a template using this template template
50Example Corp
51Example street 42
52Example city
53Example Corp footer
54-----
55
56Then in your template you write
57
58-----
59Dear ${object.partner_id.name},
60
61Your order has been booked on date ${object.date} for a total amount of ${object.sum}.
62-----
63
64And it will be evaluated to
65
66-----
67Example Corp logo
68Example Corp header
69Dear Jane Doe,
70
71Your order has been booked on date 04/17/2013 for a total amount of 42.
72Example Corp
73Example street 42
74Example city
75Example Corp footer
76-----
77
78Given the way evaluation works internally (body_text of the template template is evaluated two times, first with the instance of email.template of your own template, then with the object your template refers to), you can do some trickery if you know that a template template is always used with the same kind of model (that is, models that have the same field name):
79
80In your template template:
81
82------
83Dear ${'${object.name}'}, <-- gets evaluated to "${object.name}" in the first step, then to the content of object.name
84${object.body_html}
85Best,
86Example Corp
87------""",
88 'website': 'http://therp.nl',
89 'images': [],
90 'depends': ['email_template'],
91 'data': [
92 'view/email_template.xml',
93 ],
94 "license": 'AGPL-3',
95}
96# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
097
=== added directory 'email_template_template/model'
=== added file 'email_template_template/model/__init__.py'
--- email_template_template/model/__init__.py 1970-01-01 00:00:00 +0000
+++ email_template_template/model/__init__.py 2013-06-17 09:05:36 +0000
@@ -0,0 +1,21 @@
1# -*- coding: utf-8 -*-
2##############################################################################
3#
4# OpenERP, Open Source Management Solution
5# This module copyright (C) 2013 Therp BV (<http://therp.nl>).
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU Affero General Public License as
9# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Affero General Public License for more details.
16#
17# You should have received a copy of the GNU Affero General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20##############################################################################
21import email_template
022
=== added file 'email_template_template/model/email_template.py'
--- email_template_template/model/email_template.py 1970-01-01 00:00:00 +0000
+++ email_template_template/model/email_template.py 2013-06-17 09:05:36 +0000
@@ -0,0 +1,61 @@
1# -*- coding: utf-8 -*-
2##############################################################################
3#
4# OpenERP, Open Source Management Solution
5# This module copyright (C) 2013 Therp BV (<http://therp.nl>).
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU Affero General Public License as
9# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Affero General Public License for more details.
16#
17# You should have received a copy of the GNU Affero General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20##############################################################################
21from openerp.osv.orm import Model
22from openerp.osv import fields
23from openerp.addons.email_template.email_template import mako_template_env
24
25
26class email_template(Model):
27 _inherit = 'email.template'
28
29 def _get_is_template_template(self, cr, uid, ids, fields_name, arg,
30 context=None):
31 cr.execute('''select
32 id, (select count(*) > 0 from email_template e
33 where email_template_id=email_template.id)
34 from email_template
35 where id in %s''', (tuple(ids),))
36 return dict(cr.fetchall())
37
38 _columns = {
39 'email_template_id': fields.many2one('email.template', 'Template'),
40 'is_template_template': fields.function(
41 _get_is_template_template, type='boolean',
42 string='Is a template template'),
43 }
44
45 def get_email_template(self, cr, uid, template_id=False, record_id=None,
46 context=None):
47 this = super(email_template, self).get_email_template(
48 cr, uid, template_id, record_id, context)
49
50 if this.email_template_id and not this.is_template_template:
51 for field in ['body_html']:
52 if this[field] and this.email_template_id[field]:
53 try:
54 mako_template_env.autoescape = False
55 this._data[this.id][field] = self.render_template(
56 cr, uid, this.email_template_id[field],
57 this.email_template_id.model,
58 this.id, this._context)
59 finally:
60 mako_template_env.autoescape = True
61 return this
062
=== added directory 'email_template_template/view'
=== added file 'email_template_template/view/email_template.xml'
--- email_template_template/view/email_template.xml 1970-01-01 00:00:00 +0000
+++ email_template_template/view/email_template.xml 2013-06-17 09:05:36 +0000
@@ -0,0 +1,71 @@
1<?xml version="1.0" encoding="utf-8"?>
2<openerp>
3 <data>
4 <record id="email_template_form" model="ir.ui.view">
5 <field name="name">email.template.form</field>
6 <field name="model">email.template</field>
7 <field name="inherit_id" ref="email_template.email_template_form" />
8 <field name="type">form</field>
9 <field name="arch" type="xml">
10 <data>
11 <field name="name" position="after">
12 <field name="is_template_template" invisible="1" />
13 <field name="email_template_id" domain="[('email_template_id', '=', False), ('model_id', '=', %(email_template.model_email_template)s)]"
14 attrs="{'readonly': [('is_template_template','=',True), ('email_template_id','=',False)]}"
15 context="{'default_model_id': %(email_template.model_email_template)s}"
16 />
17
18 </field>
19 <field name="model_id" position="attributes">
20 <attribute name="attrs">
21 {'readonly': [('is_template_template','=',True)]}
22 </attribute>
23 </field>
24 <field name="email_from" position="attributes">
25 <attribute name="required">0</attribute>
26 <attribute name="attrs">
27 {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
28 </attribute>
29 </field>
30 <field name="email_to" position="attributes">
31 <attribute name="required">0</attribute>
32 <attribute name="attrs">
33 {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
34 </attribute>
35 </field>
36 <field name="email_cc" position="attributes">
37 <attribute name="attrs">
38 {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
39 </attribute>
40 </field>
41 <field name="email_recipients" position="attributes">
42 <attribute name="attrs">
43 {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
44 </attribute>
45 </field>
46 <field name="reply_to" position="attributes">
47 <attribute name="attrs">
48 {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
49 </attribute>
50 </field>
51 <field name="lang" position="attributes">
52 <attribute name="attrs">
53 {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
54 </attribute>
55 </field>
56 <field name="user_signature" position="attributes">
57 <attribute name="attrs">
58 {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
59 </attribute>
60 </field>
61 <field name="subject" position="attributes">
62 <attribute name="required">0</attribute>
63 <attribute name="attrs">
64 {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
65 </attribute>
66 </field>
67 </data>
68 </field>
69 </record>
70 </data>
71</openerp>