[IMP] clean email_template module, merge email_template and mail_gateway module,...
[odoo/odoo.git] / addons / email_template / email_template.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2009 Sharoon Thomas
6 #    Copyright (C) 2010-2010 OpenERP SA (<http://www.openerp.com>)
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>
20 #
21 ##############################################################################
22
23 import base64
24 import random
25 import netsvc
26 import logging
27 import re
28
29 TEMPLATE_ENGINES = []
30
31 from osv import osv, fields
32 from tools.translate import _
33
34 try:
35     from mako.template import Template as MakoTemplate
36     TEMPLATE_ENGINES.append(('mako', 'Mako Templates'))
37 except ImportError:
38     logging.getLogger('init').warning("module email_template: Mako templates not installed")
39
40 import tools
41 import pooler
42 import logging
43
44 def get_value(cursor, user, recid, message=None, template=None, context=None):
45     """
46     Evaluates an expression and returns its value
47     @param cursor: Database Cursor
48     @param user: ID of current user
49     @param recid: ID of the target record under evaluation
50     @param message: The expression to be evaluated
51     @param template: BrowseRecord object of the current template
52     @param context: OpenERP Context
53     @return: Computed message (unicode) or u""
54     """
55     pool = pooler.get_pool(cursor.dbname)
56     if message is None:
57         message = {}
58     #Returns the computed expression
59     if message:
60         try:
61             message = tools.ustr(message)
62             object = pool.get(template.model_int_name).browse(cursor, user, recid, context=context)
63             env = {
64                 'user':pool.get('res.users').browse(cursor, user, user, context=context),
65                 'db':cursor.dbname
66                    }
67             if template.template_language == 'mako':
68                 templ = MakoTemplate(message, input_encoding='utf-8')
69                 reply = MakoTemplate(message).render_unicode(object=object,
70                                                              peobject=object,
71                                                              env=env,
72                                                              format_exceptions=True)
73             elif template.template_language == 'django':
74                 templ = DjangoTemplate(message)
75                 env['object'] = object
76                 env['peobject'] = object
77                 reply = templ.render(Context(env))
78             return reply or False
79         except Exception:
80             logging.exception("can't render %r", message)
81             return u""
82     else:
83         return message
84
85 class email_template(osv.osv):
86     "Templates for sending Email"
87
88     _name = "email.template"
89     _description = 'Email Templates for Models'
90
91     def change_model(self, cursor, user, ids, object_name, context=None):
92         if object_name:
93             mod_name = self.pool.get('ir.model').read(
94                                               cursor,
95                                               user,
96                                               object_name,
97                                               ['model'], context)['model']
98         else:
99             mod_name = False
100         return {
101                 'value':{'model_int_name':mod_name}
102                 }
103
104     _columns = {
105         'name' : fields.char('Name', size=100, required=True),
106         'object_name':fields.many2one('ir.model', 'Resource'),
107         'model_int_name':fields.char('Model Internal Name', size=200,),
108         'from_account':fields.many2one(
109                    'email.smtp_server',
110                    string="Email Account",
111                    help="Emails will be sent from this approved account."),
112         'def_to':fields.char(
113                  'Recipient (To)',
114                  size=250,
115                  help="The Recipient of email. "
116                  "Placeholders can be used here. "
117                  "e.g. ${object.email_to}"),
118         'def_cc':fields.char(
119                  'CC',
120                  size=250,
121                  help="Carbon Copy address(es), comma-separated."
122                     " Placeholders can be used here. "
123                     "e.g. ${object.email_cc}"),
124         'def_bcc':fields.char(
125                   'BCC',
126                   size=250,
127                   help="Blind Carbon Copy address(es), comma-separated."
128                     " Placeholders can be used here. "
129                     "e.g. ${object.email_bcc}"),
130         'reply_to':fields.char('Reply-To',
131                     size=250,
132                     help="The address recipients should reply to,"
133                     " if different from the From address."
134                     " Placeholders can be used here. "
135                     "e.g. ${object.email_reply_to}"),
136         'message_id':fields.char('Message-ID',
137                     size=250,
138                     help="Specify the Message-ID SMTP header to use in outgoing emails. Please note that this overrides the Resource tracking option! Placeholders can be used here."),
139         'track_campaign_item':fields.boolean('Resource Tracking',
140                                 help="Enable this is you wish to include a special \
141 tracking marker in outgoing emails so you can identify replies and link \
142 them back to the corresponding resource record. \
143 This is useful for CRM leads for example"),
144         'lang':fields.char(
145                    'Language',
146                    size=250,
147                    help="The default language for the email."
148                    " Placeholders can be used here. "
149                    "eg. ${object.partner_id.lang}"),
150         'def_subject':fields.char(
151                   'Subject',
152                   size=200,
153                   help="The subject of email."
154                   " Placeholders can be used here.",
155                   translate=True),
156         'def_body_text':fields.text(
157                     'Standard Body (Text)',
158                     help="The text version of the mail",
159                     translate=True),
160         'def_body_html':fields.text(
161                     'Body (Text-Web Client Only)',
162                     help="The text version of the mail",
163                     translate=True),
164         'use_sign':fields.boolean(
165                   'Signature',
166                   help="the signature from the User details"
167                   " will be appended to the mail"),
168         'file_name':fields.char(
169                 'Report Filename',
170                 size=200,
171                 help="Name of the generated report file. Placeholders can be used in the filename. eg: 2009_SO003.pdf",
172                 translate=True),
173         'report_template':fields.many2one(
174                   'ir.actions.report.xml',
175                   'Report to send'),
176         'attachment_ids': fields.many2many(
177                     'ir.attachment',
178                     'email_template_attachment_rel',
179                     'email_template_id',
180                     'attachment_id',
181                     'Attached Files',
182                     help="You may attach existing files to this template, "
183                          "so they will be added in all emails created from this template"),
184         'ref_ir_act_window':fields.many2one(
185                     'ir.actions.act_window',
186                     'Window Action',
187                     help="Action that will open this email template on Resource records",
188                     readonly=True),
189         'ref_ir_value':fields.many2one(
190                    'ir.values',
191                    'Wizard Button',
192                    help="Button in the side bar of the form view of this Resource that will invoke the Window Action",
193                    readonly=True),
194         'allowed_groups':fields.many2many(
195                   'res.groups',
196                   'template_group_rel',
197                   'templ_id', 'group_id',
198                   string="Allowed User Groups",
199                   help="Only users from these groups will be"
200                   " allowed to send mails from this Template"),
201         'model_object_field':fields.many2one(
202                  'ir.model.fields',
203                  string="Field",
204                  help="Select the field from the model you want to use."
205                  "\nIf it is a relationship field you will be able to "
206                  "choose the nested values in the box below\n(Note:If "
207                  "there are no values make sure you have selected the"
208                  " correct model)",
209                  store=False),
210         'sub_object':fields.many2one(
211                  'ir.model',
212                  'Sub-model',
213                  help='When a relation field is used this field'
214                  ' will show you the type of field you have selected',
215                  store=False),
216         'sub_model_object_field':fields.many2one(
217                  'ir.model.fields',
218                  'Sub Field',
219                  help="When you choose relationship fields "
220                  "this field will specify the sub value you can use.",
221                  store=False),
222         'null_value':fields.char(
223                  'Null Value',
224                  help="This Value is used if the field is empty",
225                  size=50, store=False),
226         'copyvalue':fields.char(
227                 'Expression',
228                 size=100,
229                 help="Copy and paste the value in the "
230                 "location you want to use a system value.",
231                 store=False),
232         'table_html':fields.text(
233              'HTML code',
234              help="Copy this html code to your HTML message"
235              " body for displaying the info in your mail.",
236              store=False),
237         #Template language(engine eg.Mako) specifics
238         'template_language':fields.selection(
239                 TEMPLATE_ENGINES,
240                 'Templating Language',
241                 required=True
242                 ),
243         'auto_delete': fields.boolean('Auto Delete', help="Permanently delete emails after sending"),
244     }
245
246     _defaults = {
247         'template_language' : lambda *a:'mako',
248
249     }
250
251     _sql_constraints = [
252         ('name', 'unique (name)','The template name must be unique !')
253     ]
254
255     def create_action(self, cr, uid, ids, context=None):
256         vals = {}
257         if context is None:
258             context = {}
259         template_obj = self.browse(cr, uid, ids, context=context)[0]
260         src_obj = template_obj.object_name.model
261         vals['ref_ir_act_window'] = self.pool.get('ir.actions.act_window').create(cr, uid, {
262              'name': template_obj.name,
263              'type': 'ir.actions.act_window',
264              'res_model': 'email_template.send.wizard',
265              'src_model': src_obj,
266              'view_type': 'form',
267              'context': "{'src_model':'%s','template_id':'%d','src_rec_id':active_id,'src_rec_ids':active_ids}" % (src_obj, template_obj.id),
268              'view_mode':'form,tree',
269              'view_id': self.pool.get('ir.ui.view').search(cr, uid, [('name', '=', 'email_template.send.wizard.form')], context=context)[0],
270              'target': 'new',
271              'auto_refresh':1
272         }, context)
273         vals['ref_ir_value'] = self.pool.get('ir.values').create(cr, uid, {
274              'name': _('Send Mail (%s)') % template_obj.name,
275              'model': src_obj,
276              'key2': 'client_action_multi',
277              'value': "ir.actions.act_window," + str(vals['ref_ir_act_window']),
278              'object': True,
279          }, context)
280         self.write(cr, uid, ids, {
281             'ref_ir_act_window': vals['ref_ir_act_window'],
282             'ref_ir_value': vals['ref_ir_value'],
283         }, context)
284         return True
285
286     def unlink_action(self, cr, uid, ids, context=None):
287         for template in self.browse(cr, uid, ids, context=context):
288             try:
289                 if template.ref_ir_act_window:
290                     self.pool.get('ir.actions.act_window').unlink(cr, uid, template.ref_ir_act_window.id, context)
291                 if template.ref_ir_value:
292                     self.pool.get('ir.values').unlink(cr, uid, template.ref_ir_value.id, context)
293             except:
294                 raise osv.except_osv(_("Warning"), _("Deletion of Record failed"))
295
296     def delete_action(self, cr, uid, ids, context=None):
297         self.unlink_action(cr, uid, ids, context=context)
298         return True
299
300     def unlink(self, cr, uid, ids, context=None):
301         self.unlink_action(cr, uid, ids, context=context)
302         return super(email_template, self).unlink(cr, uid, ids, context=context)
303
304     def copy(self, cr, uid, id, default=None, context=None):
305         if default is None:
306             default = {}
307         default = default.copy()
308         old = self.read(cr, uid, id, ['name'], context=context)
309         new_name = _("Copy of template ") + old.get('name', 'No Name')
310         check = self.search(cr, uid, [('name', '=', new_name)], context=context)
311         if check:
312             new_name = new_name + '_' + random.choice('abcdefghij') + random.choice('lmnopqrs') + random.choice('tuvwzyz')
313         default.update({'name':new_name})
314         return super(email_template, self).copy(cr, uid, id, default, context)
315
316     def build_expression(self, field_name, sub_field_name, null_value, template_language='mako'):
317         """
318         Returns a template expression based on data provided
319         @param field_name: field name
320         @param sub_field_name: sub field name (M2O)
321         @param null_value: default value if the target value is empty
322         @param template_language: name of template engine
323         @return: computed expression
324         """
325
326         expression = ''
327         if template_language == 'mako':
328             if field_name:
329                 expression = "${object." + field_name
330                 if sub_field_name:
331                     expression += "." + sub_field_name
332                 if null_value:
333                     expression += " or '''%s'''" % null_value
334                 expression += "}"
335         elif template_language == 'django':
336             if field_name:
337                 expression = "{{object." + field_name
338                 if sub_field_name:
339                     expression += "." + sub_field_name
340                 if null_value:
341                     expression += "|default: '''%s'''" % null_value
342                 expression += "}}"
343         return expression
344
345     def onchange_model_object_field(self, cr, uid, ids, model_object_field, template_language, context=None):
346         if not model_object_field:
347             return {}
348         result = {}
349         field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
350         #Check if field is relational
351         if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
352             res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
353             if res_ids:
354                 result['sub_object'] = res_ids[0]
355                 result['copyvalue'] = self.build_expression(False,
356                                                       False,
357                                                       False,
358                                                       template_language)
359                 result['sub_model_object_field'] = False
360                 result['null_value'] = False
361         else:
362             #Its a simple field... just compute placeholder
363             result['sub_object'] = False
364             result['copyvalue'] = self.build_expression(field_obj.name,
365                                                   False,
366                                                   False,
367                                                   template_language
368                                                   )
369             result['sub_model_object_field'] = False
370             result['null_value'] = False
371         return {'value':result}
372
373     def onchange_sub_model_object_field(self, cr, uid, ids, model_object_field, sub_model_object_field, template_language, context=None):
374         if not model_object_field or not sub_model_object_field:
375             return {}
376         result = {}
377         field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
378         if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
379             res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
380             sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context)
381             if res_ids:
382                 result['sub_object'] = res_ids[0]
383                 result['copyvalue'] = self.build_expression(field_obj.name,
384                                                       sub_field_obj.name,
385                                                       False,
386                                                       template_language
387                                                       )
388                 result['sub_model_object_field'] = sub_model_object_field
389                 result['null_value'] = False
390         else:
391             #Its a simple field... just compute placeholder
392             result['sub_object'] = False
393             result['copyvalue'] = self.build_expression(field_obj.name,
394                                                   False,
395                                                   False,
396                                                   template_language
397                                                   )
398             result['sub_model_object_field'] = False
399             result['null_value'] = False
400         return {'value':result}
401
402     def onchange_null_value(self, cr, uid, ids, model_object_field, sub_model_object_field, null_value, template_language, context=None):
403         if not model_object_field and not null_value:
404             return {}
405         result = {}
406         field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
407         if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
408             res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
409             sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context)
410             if res_ids:
411                 result['sub_object'] = res_ids[0]
412                 result['copyvalue'] = self.build_expression(field_obj.name,
413                                                       sub_field_obj.name,
414                                                       null_value,
415                                                       template_language
416                                                       )
417                 result['sub_model_object_field'] = sub_model_object_field
418                 result['null_value'] = null_value
419         else:
420             #Its a simple field... just compute placeholder
421             result['sub_object'] = False
422             result['copyvalue'] = self.build_expression(field_obj.name,
423                                                   False,
424                                                   null_value,
425                                                   template_language
426                                                   )
427             result['sub_model_object_field'] = False
428             result['null_value'] = null_value
429         return {'value':result}
430
431     def _add_attachment(self, cursor, user, mailbox_id, name, data, filename, context=None):
432         """
433         Add an attachment to a given mailbox entry.
434
435         :param data: base64 encoded attachment data to store
436         """
437         attachment_obj = self.pool.get('ir.attachment')
438         attachment_data = {
439             'name':  (name or '') + _(' (Email Attachment)'),
440             'datas': data,
441             'datas_fname': filename,
442             'description': name or _('No Description'),
443             'res_model':'email_template.mailbox',
444             'res_id': mailbox_id,
445         }
446         attachment_id = attachment_obj.create(cursor,
447                                               user,
448                                               attachment_data,
449                                               context)
450         if attachment_id:
451             self.pool.get('email_template.mailbox').write(
452                               cursor,
453                               user,
454                               mailbox_id,
455                               {
456                                'attachments_ids':[(4, attachment_id)],
457                                'mail_type':'multipart/mixed'
458                               },
459                               context)
460
461     def generate_attach_reports(self,
462                                  cursor,
463                                  user,
464                                  template,
465                                  record_id,
466                                  mail,
467                                  context=None):
468         """
469         Generate report to be attached and attach it
470         to the email, and add any directly attached files as well.
471
472         @param cursor: Database Cursor
473         @param user: ID of User
474         @param template: Browse record of
475                          template
476         @param record_id: ID of the target model
477                           for which this mail has
478                           to be generated
479         @param mail: Browse record of email object
480         @return: True
481         """
482         if template.report_template:
483             reportname = 'report.' + \
484                 self.pool.get('ir.actions.report.xml').read(
485                                              cursor,
486                                              user,
487                                              template.report_template.id,
488                                              ['report_name'],
489                                              context)['report_name']
490             service = netsvc.LocalService(reportname)
491             data = {}
492             data['model'] = template.model_int_name
493             (result, format) = service.create(cursor,
494                                               user,
495                                               [record_id],
496                                               data,
497                                               context)
498             fname = tools.ustr(get_value(cursor, user, record_id,
499                                          template.file_name, template, context)
500                                or 'Report')
501             ext = '.' + format
502             if not fname.endswith(ext):
503                 fname += ext
504             self._add_attachment(cursor, user, mail.id, mail.subject, base64.b64encode(result), fname, context)
505
506         if template.attachment_ids:
507             for attachment in template.attachment_ids:
508                 self._add_attachment(cursor, user, mail.id, attachment.name, attachment.datas, attachment.datas_fname, context)
509
510         return True
511
512     def _generate_mailbox_item_from_template(self,
513                                       cursor,
514                                       user,
515                                       template,
516                                       record_id,
517                                       context=None):
518         """
519         Generates an email from the template for
520         record record_id of target object
521
522         @param cursor: Database Cursor
523         @param user: ID of User
524         @param template: Browse record of
525                          template
526         @param record_id: ID of the target model
527                           for which this mail has
528                           to be generated
529         @return: ID of created object
530         """
531         if context is None:
532             context = {}
533         #If account to send from is in context select it, else use enforced account
534         if 'account_id' in context.keys():
535             from_account = self.pool.get('email.smtp_server').read(
536                                                     cursor,
537                                                     user,
538                                                     context.get('account_id'),
539                                                     ['name', 'email_id'],
540                                                     context
541                                                     )
542         else:
543             from_account = {
544                             'id':template.from_account.id,
545                             'name':template.from_account.name,
546                             'email_id':template.from_account.email_id
547                             }
548         lang = get_value(cursor,
549                          user,
550                          record_id,
551                          template.lang,
552                          template,
553                          context)
554         if lang:
555             ctx = context.copy()
556             ctx.update({'lang':lang})
557             template = self.browse(cursor, user, template.id, context=ctx)
558
559         # determine name of sender, either it is specified in email_id or we
560         # use the account name
561         email_id = from_account['email_id'].strip()
562         email_from = re.findall(r'([^ ,<@]+@[^> ,]+)', email_id)[0]
563         if email_from != email_id:
564             # we should keep it all, name is probably specified in the address
565             email_from = from_account['email_id']
566         else:
567             email_from = tools.ustr(from_account['name']) + "<" + tools.ustr(email_id) + ">"
568
569         # FIXME: should do this in a loop and rename template fields to the corresponding
570         # mailbox fields. (makes no sense to have different names I think.
571         mailbox_values = {
572             'email_from': email_from,
573             'email_to':get_value(cursor,
574                                user,
575                                record_id,
576                                template.def_to,
577                                template,
578                                context),
579             'email_cc':get_value(cursor,
580                                user,
581                                record_id,
582                                template.def_cc,
583                                template,
584                                context),
585             'email_bcc':get_value(cursor,
586                                 user,
587                                 record_id,
588                                 template.def_bcc,
589                                 template,
590                                 context),
591             'reply_to':get_value(cursor,
592                                 user,
593                                 record_id,
594                                 template.reply_to,
595                                 template,
596                                 context),
597             'subject':get_value(cursor,
598                                     user,
599                                     record_id,
600                                     template.def_subject,
601                                     template,
602                                     context),
603             'body_text':get_value(cursor,
604                                       user,
605                                       record_id,
606                                       template.def_body_text,
607                                       template,
608                                       context),
609             'body_html':get_value(cursor,
610                                       user,
611                                       record_id,
612                                       template.def_body_html,
613                                       template,
614                                       context),
615             'account_id' :from_account['id'],
616             #This is a mandatory field when automatic emails are sent
617             'state':'na',
618             'folder':'drafts',
619             'mail_type':'multipart/alternative',
620         }
621
622         if template['message_id']:
623             # use provided message_id with placeholders
624             mailbox_values.update({'message_id': get_value(cursor, user, record_id, template['message_id'], template, context)})
625
626         elif template['track_campaign_item']:
627             # get appropriate message-id
628             mailbox_values.update({'message_id': tools.misc.generate_tracking_message_id(record_id)})
629
630         if not mailbox_values['account_id']:
631             raise Exception("Unable to send the mail. No account linked to the template.")
632         #Use signatures if allowed
633         if template.use_sign:
634             sign = self.pool.get('res.users').read(cursor,
635                                                    user,
636                                                    user,
637                                                    ['signature'],
638                                                    context)['signature']
639             if mailbox_values['body_text']:
640                 mailbox_values['body_text'] += sign
641             if mailbox_values['body_html']:
642                 mailbox_values['body_html'] += sign
643         mailbox_id = self.pool.get('email_template.mailbox').create(
644                                                              cursor,
645                                                              user,
646                                                              mailbox_values,
647                                                              context)
648
649         return mailbox_id
650
651
652     def generate_mail(self,
653                       cursor,
654                       user,
655                       template_id,
656                       record_ids,
657                       context=None):
658         if context is None:
659             context = {}
660         template = self.browse(cursor, user, template_id, context=context)
661         if not template:
662             raise Exception("The requested template could not be loaded")
663         result = True
664         mailbox_obj = self.pool.get('email_template.mailbox')
665         for record_id in record_ids:
666             mailbox_id = self._generate_mailbox_item_from_template(
667                                                                 cursor,
668                                                                 user,
669                                                                 template,
670                                                                 record_id,
671                                                                 context)
672             mail = mailbox_obj.browse(
673                                         cursor,
674                                         user,
675                                         mailbox_id,
676                                         context=context
677                                               )
678             if template.report_template or template.attachment_ids:
679                 self.generate_attach_reports(
680                                               cursor,
681                                               user,
682                                               template,
683                                               record_id,
684                                               mail,
685                                               context
686                                               )
687
688             self.pool.get('email_template.mailbox').write(
689                                                 cursor,
690                                                 user,
691                                                 mailbox_id,
692                                                 {'folder':'outbox'},
693                                                 context=context
694             )
695             # TODO : manage return value of all the records
696             result = self.pool.get('email_template.mailbox').send_this_mail(cursor, user, [mailbox_id], context)
697         return result
698
699 email_template()
700
701 ## FIXME: this class duplicates a lot of features of the email template send wizard,
702 ##        one of the 2 should inherit from the other!
703
704 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: