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
1=== added directory 'email_template_template'
2=== added file 'email_template_template/__init__.py'
3--- email_template_template/__init__.py 1970-01-01 00:00:00 +0000
4+++ email_template_template/__init__.py 2013-06-17 09:05:36 +0000
5@@ -0,0 +1,1 @@
6+import model
7
8=== added file 'email_template_template/__openerp__.py'
9--- email_template_template/__openerp__.py 1970-01-01 00:00:00 +0000
10+++ email_template_template/__openerp__.py 2013-06-17 09:05:36 +0000
11@@ -0,0 +1,96 @@
12+# -*- coding: utf-8 -*-
13+##############################################################################
14+#
15+# OpenERP, Open Source Management Solution
16+# This module copyright (C) 2013 Therp BV (<http://therp.nl>).
17+#
18+# This program is free software: you can redistribute it and/or modify
19+# it under the terms of the GNU Affero General Public License as
20+# published by the Free Software Foundation, either version 3 of the
21+# License, or (at your option) any later version.
22+#
23+# This program is distributed in the hope that it will be useful,
24+# but WITHOUT ANY WARRANTY; without even the implied warranty of
25+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26+# GNU Affero General Public License for more details.
27+#
28+# You should have received a copy of the GNU Affero General Public License
29+# along with this program. If not, see <http://www.gnu.org/licenses/>.
30+#
31+##############################################################################
32+{
33+ "name": "Templates for email templates",
34+ "version": "1.0",
35+ "author": "Therp BV",
36+ "category": 'Tools',
37+ 'complexity': "expert",
38+ "description": """If an organisation's email layout is a bit more
39+complicated, changes can be tedious when having to do that across several email
40+templates. So this addon allows to define templates for mails that is referenced
41+by other mail templates.
42+This way we can put the layout parts into the template template and only content
43+in the other templates. Changing the layout is then only a matter of changing
44+the template template.
45+
46+
47+Usage:
48+Create an email template with the related document model 'Email Templates'. Now
49+most of the fields gray out and you can only edit body_text and body_html. Be
50+sure to use ${body_text} and ${body_html} respectively in your template
51+template.
52+
53+Then select this newly created template templates in one of your actual
54+templates.
55+
56+For example, create a template template
57+-----
58+Example Corp logo
59+Example Corp header
60+${object.body_text} <- this gets evaluated to the body_text of a template using this template template
61+Example Corp
62+Example street 42
63+Example city
64+Example Corp footer
65+-----
66+
67+Then in your template you write
68+
69+-----
70+Dear ${object.partner_id.name},
71+
72+Your order has been booked on date ${object.date} for a total amount of ${object.sum}.
73+-----
74+
75+And it will be evaluated to
76+
77+-----
78+Example Corp logo
79+Example Corp header
80+Dear Jane Doe,
81+
82+Your order has been booked on date 04/17/2013 for a total amount of 42.
83+Example Corp
84+Example street 42
85+Example city
86+Example Corp footer
87+-----
88+
89+Given 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):
90+
91+In your template template:
92+
93+------
94+Dear ${'${object.name}'}, <-- gets evaluated to "${object.name}" in the first step, then to the content of object.name
95+${object.body_html}
96+Best,
97+Example Corp
98+------""",
99+ 'website': 'http://therp.nl',
100+ 'images': [],
101+ 'depends': ['email_template'],
102+ 'data': [
103+ 'view/email_template.xml',
104+ ],
105+ "license": 'AGPL-3',
106+}
107+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
108
109=== added directory 'email_template_template/model'
110=== added file 'email_template_template/model/__init__.py'
111--- email_template_template/model/__init__.py 1970-01-01 00:00:00 +0000
112+++ email_template_template/model/__init__.py 2013-06-17 09:05:36 +0000
113@@ -0,0 +1,21 @@
114+# -*- coding: utf-8 -*-
115+##############################################################################
116+#
117+# OpenERP, Open Source Management Solution
118+# This module copyright (C) 2013 Therp BV (<http://therp.nl>).
119+#
120+# This program is free software: you can redistribute it and/or modify
121+# it under the terms of the GNU Affero General Public License as
122+# published by the Free Software Foundation, either version 3 of the
123+# License, or (at your option) any later version.
124+#
125+# This program is distributed in the hope that it will be useful,
126+# but WITHOUT ANY WARRANTY; without even the implied warranty of
127+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128+# GNU Affero General Public License for more details.
129+#
130+# You should have received a copy of the GNU Affero General Public License
131+# along with this program. If not, see <http://www.gnu.org/licenses/>.
132+#
133+##############################################################################
134+import email_template
135
136=== added file 'email_template_template/model/email_template.py'
137--- email_template_template/model/email_template.py 1970-01-01 00:00:00 +0000
138+++ email_template_template/model/email_template.py 2013-06-17 09:05:36 +0000
139@@ -0,0 +1,61 @@
140+# -*- coding: utf-8 -*-
141+##############################################################################
142+#
143+# OpenERP, Open Source Management Solution
144+# This module copyright (C) 2013 Therp BV (<http://therp.nl>).
145+#
146+# This program is free software: you can redistribute it and/or modify
147+# it under the terms of the GNU Affero General Public License as
148+# published by the Free Software Foundation, either version 3 of the
149+# License, or (at your option) any later version.
150+#
151+# This program is distributed in the hope that it will be useful,
152+# but WITHOUT ANY WARRANTY; without even the implied warranty of
153+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
154+# GNU Affero General Public License for more details.
155+#
156+# You should have received a copy of the GNU Affero General Public License
157+# along with this program. If not, see <http://www.gnu.org/licenses/>.
158+#
159+##############################################################################
160+from openerp.osv.orm import Model
161+from openerp.osv import fields
162+from openerp.addons.email_template.email_template import mako_template_env
163+
164+
165+class email_template(Model):
166+ _inherit = 'email.template'
167+
168+ def _get_is_template_template(self, cr, uid, ids, fields_name, arg,
169+ context=None):
170+ cr.execute('''select
171+ id, (select count(*) > 0 from email_template e
172+ where email_template_id=email_template.id)
173+ from email_template
174+ where id in %s''', (tuple(ids),))
175+ return dict(cr.fetchall())
176+
177+ _columns = {
178+ 'email_template_id': fields.many2one('email.template', 'Template'),
179+ 'is_template_template': fields.function(
180+ _get_is_template_template, type='boolean',
181+ string='Is a template template'),
182+ }
183+
184+ def get_email_template(self, cr, uid, template_id=False, record_id=None,
185+ context=None):
186+ this = super(email_template, self).get_email_template(
187+ cr, uid, template_id, record_id, context)
188+
189+ if this.email_template_id and not this.is_template_template:
190+ for field in ['body_html']:
191+ if this[field] and this.email_template_id[field]:
192+ try:
193+ mako_template_env.autoescape = False
194+ this._data[this.id][field] = self.render_template(
195+ cr, uid, this.email_template_id[field],
196+ this.email_template_id.model,
197+ this.id, this._context)
198+ finally:
199+ mako_template_env.autoescape = True
200+ return this
201
202=== added directory 'email_template_template/view'
203=== added file 'email_template_template/view/email_template.xml'
204--- email_template_template/view/email_template.xml 1970-01-01 00:00:00 +0000
205+++ email_template_template/view/email_template.xml 2013-06-17 09:05:36 +0000
206@@ -0,0 +1,71 @@
207+<?xml version="1.0" encoding="utf-8"?>
208+<openerp>
209+ <data>
210+ <record id="email_template_form" model="ir.ui.view">
211+ <field name="name">email.template.form</field>
212+ <field name="model">email.template</field>
213+ <field name="inherit_id" ref="email_template.email_template_form" />
214+ <field name="type">form</field>
215+ <field name="arch" type="xml">
216+ <data>
217+ <field name="name" position="after">
218+ <field name="is_template_template" invisible="1" />
219+ <field name="email_template_id" domain="[('email_template_id', '=', False), ('model_id', '=', %(email_template.model_email_template)s)]"
220+ attrs="{'readonly': [('is_template_template','=',True), ('email_template_id','=',False)]}"
221+ context="{'default_model_id': %(email_template.model_email_template)s}"
222+ />
223+
224+ </field>
225+ <field name="model_id" position="attributes">
226+ <attribute name="attrs">
227+ {'readonly': [('is_template_template','=',True)]}
228+ </attribute>
229+ </field>
230+ <field name="email_from" position="attributes">
231+ <attribute name="required">0</attribute>
232+ <attribute name="attrs">
233+ {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
234+ </attribute>
235+ </field>
236+ <field name="email_to" position="attributes">
237+ <attribute name="required">0</attribute>
238+ <attribute name="attrs">
239+ {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
240+ </attribute>
241+ </field>
242+ <field name="email_cc" position="attributes">
243+ <attribute name="attrs">
244+ {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
245+ </attribute>
246+ </field>
247+ <field name="email_recipients" position="attributes">
248+ <attribute name="attrs">
249+ {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
250+ </attribute>
251+ </field>
252+ <field name="reply_to" position="attributes">
253+ <attribute name="attrs">
254+ {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
255+ </attribute>
256+ </field>
257+ <field name="lang" position="attributes">
258+ <attribute name="attrs">
259+ {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
260+ </attribute>
261+ </field>
262+ <field name="user_signature" position="attributes">
263+ <attribute name="attrs">
264+ {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
265+ </attribute>
266+ </field>
267+ <field name="subject" position="attributes">
268+ <attribute name="required">0</attribute>
269+ <attribute name="attrs">
270+ {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]}
271+ </attribute>
272+ </field>
273+ </data>
274+ </field>
275+ </record>
276+ </data>
277+</openerp>