forked from NaN-tic/trytond-electronic_mail_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.py
383 lines (339 loc) · 14 KB
/
template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# This file is part electronic_mail_template module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
import logging
import mimetypes
from email import encoders, charset
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.utils import formatdate, make_msgid
from genshi.template import TextTemplate
try:
from jinja2 import Template as Jinja2Template
jinja2_loaded = True
except ImportError:
jinja2_loaded = False
logging.getLogger('electronic_mail_template').error(
'Unable to import jinja2. Install jinja2 package.')
from trytond.model import ModelView, ModelSQL, fields
from trytond.pyson import Eval
from trytond.pool import Pool
from trytond.i18n import gettext
from trytond.exceptions import UserError
from trytond.transaction import Transaction
from trytond.modules.electronic_mail_template.tools import unaccent
from trytond.report import Report
class Template(ModelSQL, ModelView):
'Email Template'
__name__ = 'electronic.mail.template'
from_ = fields.Char('From')
sender = fields.Char('Sender')
to = fields.Char('To')
cc = fields.Char('CC')
bcc = fields.Char('BCC')
subject = fields.Char('Subject', translate=True)
smtp_server = fields.Many2One('smtp.server', 'SMTP Server',
domain=[('state', '=', 'done')], required=True)
name = fields.Char('Name', required=True, translate=True)
model = fields.Many2One('ir.model', 'Model', required=True)
mailbox = fields.Many2One('electronic.mail.mailbox', 'Mailbox',
required=True)
draft_mailbox = fields.Many2One('electronic.mail.mailbox', 'Draft Mailbox',
required=True)
language = fields.Char('Language', help=('Expression to find the ISO '
'langauge code'))
plain = fields.Text('Plain Text Body', translate=True)
html = fields.Text('HTML Body', translate=True)
reports = fields.Many2Many('electronic.mail.template.ir.action.report',
'template', 'report', 'Reports')
engine = fields.Selection('get_engines', 'Engine', required=True)
triggers = fields.One2Many('ir.trigger', 'email_template', 'Triggers',
context={
'model': Eval('model'),
'email_template': True,
})
signature = fields.Boolean('Use Signature',
help='The signature from the User details will be appened to the '
'mail.')
message_id = fields.Char('Message-ID', help='Unique Message Identifier')
in_reply_to = fields.Char('In Repply To')
@staticmethod
def default_engine():
return 'jinja2' if jinja2_loaded else 'genshi'
@classmethod
def get_engines(cls):
'''Returns the engines as list of tuple
:return: List of tuples
'''
engines = [
('python', 'Python'),
('genshi', 'Genshi'),
]
if jinja2_loaded:
engines.append(('jinja2', 'Jinja2'))
return engines
@classmethod
def check_xml_record(cls, records, values):
'''It should be possible to overwrite templates'''
return True
def eval(self, expression, record):
'''Evaluates the given :attr:expression
:param expression: Expression to evaluate
:param record: The browse record of the record
'''
engine_method = getattr(self, '_engine_' + self.engine)
return engine_method(expression, record)
@staticmethod
def template_context(record):
"""Generate the tempalte context
This is mainly to assist in the inheritance pattern
"""
User = Pool().get('res.user')
user = None
if Transaction().user:
user = User(Transaction().user)
return {
'record': record,
'user': user,
'format_date': Report.format_date,
'format_datetime': Report.format_datetime,
'format_timedelta': Report.format_timedelta,
'format_currency': Report.format_currency,
'format_number': Report.format_number,
}
@classmethod
def _engine_python(cls, expression, record):
'''Evaluate the pythonic expression and return its value
'''
if expression is None:
return ''
assert record is not None, 'Record is undefined'
template_context = cls.template_context(record)
return eval(expression, template_context)
@classmethod
def _engine_genshi(cls, expression, record):
'''
:param expression: Expression to evaluate
:param record: Browse record
'''
if not expression:
return ''
template = TextTemplate(expression)
template_context = cls.template_context(record)
try:
return template.generate(**template_context).render(
encoding=None)
except Exception as message:
raise UserError(gettext(
'electronic_mail_template.generate_template_exception',
error=repr(message)))
@classmethod
def _engine_jinja2(cls, expression, record):
'''
:param expression: Expression to evaluate
:param record: Browse record
'''
if not jinja2_loaded or not expression:
return ''
template = Jinja2Template(expression)
template_context = cls.template_context(record)
return template.render(template_context)
@classmethod
def render(cls, template, record, values):
'''Renders the template and returns as email object
:param template: Browse Record of the template
:param record: Browse Record of the record on which the template
is to generate the data on
:return: 'email.message.Message' instance
'''
ElectronicMail = Pool().get('electronic.mail')
message = MIMEMultipart()
messageid = template.eval(values['message_id'], record)
message['Message-Id'] = messageid or make_msgid()
message['Date'] = formatdate(localtime=1)
if values.get('in_reply_to'):
message['In-Reply-To'] = template.eval(values['in_reply_to'],
record)
message['From'] = ElectronicMail.validate_emails(
template.eval(values['from_'], record))
if values.get('sender'):
message['Sender'] = ElectronicMail.validate_emails(
template.eval(values['sender'], record))
message['To'] = ElectronicMail.validate_emails(
template.eval(values['to'], record))
if values.get('cc'):
message['Cc'] = ElectronicMail.validate_emails(
template.eval(values['cc'], record))
if values.get('bcc'):
message['Bcc'] = ElectronicMail.validate_emails(
template.eval(values['bcc'], record))
message['Subject'] = Header(template.eval(values['subject'],
record), 'utf-8')
# HTML & Text Alternate parts
plain = template.eval(values['plain'], record)
html = template.eval(values['html'], record)
header = """
<html>
<head><head>
<body>
"""
footer = """
</body>
</html>
"""
if html:
html = "%s%s" % (header, html)
if template.signature:
User = Pool().get('res.user')
user = User(Transaction().user)
if html and user.signature_html:
signature = user.signature_html
html = '%s<br>--<br>%s' % (html, signature)
if plain and user.signature:
signature = user.signature
plain = '%s\n--\n%s' % (plain, signature)
if html and not user.signature_html:
html = '%s<br>--<br>%s' % (html,
signature.replace('\n', '<br>'))
if html:
html = "%s%s" % (html, footer)
body = None
if html and plain:
body = MIMEMultipart('alternative')
charset.add_charset('utf-8', charset.QP, charset.QP)
if plain:
if body:
body.attach(MIMEText(plain, 'plain', _charset='utf-8'))
else:
message.attach(MIMEText(plain, 'plain', _charset='utf-8'))
if html:
if body:
body.attach(MIMEText(html, 'html', _charset='utf-8'))
else:
message.attach(MIMEText(html, 'html', _charset='utf-8'))
if body:
message.attach(body)
# Attach reports
if template.reports:
reports = cls.render_reports(template, record)
for report in reports:
ext, data, filename, file_name = report[0:5]
if file_name:
filename = template.eval(file_name, record)
filename = unaccent(filename)
filename = ext and '%s.%s' % (filename, ext) or filename
content_type, _ = mimetypes.guess_type(filename)
maintype, subtype = (
content_type or 'application/octet-stream'
).split('/', 1)
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(data)
encoders.encode_base64(attachment)
attachment.add_header(
'Content-Disposition', 'attachment', filename=filename)
message.attach(attachment)
return message
@classmethod
def render_reports(cls, template, record):
'''Renders the reports and returns as a list of tuple
:param template: Browse Record of the template
:param record: Browse Record of the record on which the template
is to generate the data on
:return: List of tuples with:
report_type
data
the report name
the report file name (optional)
'''
pool = Pool()
ActionReport = pool.get('ir.action.report')
if template.language:
language = template.eval(template.language, record)
else:
language = False
reports = []
for report_action in template.reports:
if language:
with Transaction().set_context(language=language):
report_action = ActionReport(report_action.id)
report = Pool().get(report_action.report_name, type='report')
report_execute = report.execute([record.id], {'id': record.id})
if report_execute:
reports.append([report_execute, report_action.file_name])
# The boolean for direct print in the tuple is useless for emails
return [(r[0][0], r[0][1], r[0][3], r[1]) for r in reports]
@classmethod
def render_and_send(cls, template_id, records):
"""
Render the template and send
:param template_id: ID template
:param records: List Object of the records
"""
pool = Pool()
Configuration = pool.get('electronic.mail.configuration')
ElectronicEmail = pool.get('electronic.mail')
Template = pool.get('electronic.mail.template')
template = cls(template_id)
config = Configuration(1)
for record in records:
# load data in language when send a record
if template.language:
language = template.eval(template.language, record)
with Transaction().set_context(language=language):
template = Template(template.id)
values = {'template': template}
tmpl_fields = ('from_', 'sender', 'to', 'cc', 'bcc', 'subject',
'message_id', 'in_reply_to', 'plain', 'html')
for field_name in tmpl_fields:
values[field_name] = getattr(template, field_name)
mail_message = cls.render(template, record, values)
electronic_mail = ElectronicEmail.create_from_mail(
mail_message, template.mailbox.id, record)
if not electronic_mail:
continue
electronic_mail.template = template
electronic_mail.save()
with Transaction().set_context(
queue_name='electronic_mail',
queue_scheduled_at=config.send_email_after):
ElectronicEmail.__queue__.send_mail([electronic_mail])
return True
@classmethod
def mail_from_trigger(cls, records, trigger_id):
"""
To be used with ir.trigger to send mails automatically
The process involves identifying the tempalte which needs
to be pulled when the trigger is.
:param records: Object of the records
:param trigger_id: ID of the trigger
"""
Trigger = Pool().get('ir.trigger')
trigger = Trigger(trigger_id)
return cls.render_and_send(trigger.email_template.id, records)
def get_attachments(self, records):
record_ids = [r.id for r in records]
attachments = []
for report in self.reports:
report = Pool().get(report.report_name, type='report')
ext, data, filename, file_name = report.execute(record_ids, {})
if file_name:
filename = self.eval(file_name, record_ids)
filename = ext and '%s.%s' % (filename, ext) or filename
content_type, _ = mimetypes.guess_type(filename)
maintype, subtype = (
content_type or 'application/octet-stream'
).split('/', 1)
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(data)
encoders.encode_base64(attachment)
attachment.add_header(
'Content-Disposition', 'attachment', filename=filename)
attachments.append(attachment)
return attachments
class TemplateReport(ModelSQL):
'Template - Report Action'
__name__ = 'electronic.mail.template.ir.action.report'
template = fields.Many2One('electronic.mail.template', 'Template')
report = fields.Many2One('ir.action.report', 'Report')