[IMP] email_template :- Change the action of old email.send wizard to new email.compo...
[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-Today 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 from osv import osv
24 from osv import fields
25 import base64
26 import random
27 import netsvc
28 import logging
29 import re
30 from tools.translate import _
31 import tools
32 import pooler
33
34 class email_template(osv.osv):
35     "Templates for sending Email"
36     _inherit = 'email.message.template'
37     _name = "email.template"
38     _description = 'Email Templates for Models'
39
40     def get_template_value(self, cr, uid, message=None, model=None, record_id=None, context=None):
41         import mako_template
42         return mako_template.get_value(cr, uid, message=message, model=model, record_id=record_id, context=context)
43
44     def get_email_template(self, cr, uid, template_id=False, record_id=None, context=None):
45         "Return Template Object"
46         if context is None:
47             context = {}
48         if not template_id:
49             template_id = context.get('template_id', False)
50         if not template_id:
51             return False
52
53         template = self.browse(cr, uid, int(template_id), context)
54         lang = self.get_template_value(cr, uid, template.lang, template.model, record_id, context)
55         if lang:
56             # Use translated template if necessary
57             ctx = context.copy()
58             ctx['lang'] = lang
59             template = self.browse(cr, uid, template.id, ctx)
60         return template
61
62     def onchange_model_id(self, cr, uid, ids, model_id, context=None):
63         mod_name = False
64         if model_id:
65             mod_name = self.pool.get('ir.model').browse(cr, uid, model_id, context).model
66         return {'value':{'model':mod_name}}
67
68     _columns = {
69         'name': fields.char('Name', size=250),
70         'model_id':fields.many2one('ir.model', 'Resource'),
71         'model': fields.related('model_id', 'model', string='Model', type="char", size=128, store=True, readonly=True),
72         'track_campaign_item':fields.boolean('Resource Tracking',
73                                 help="Enable this is you wish to include a special \
74 tracking marker in outgoing emails so you can identify replies and link \
75 them back to the corresponding resource record. \
76 This is useful for CRM leads for example"),
77         'lang':fields.char(
78                    'Language',
79                    size=250,
80                    help="The default language for the email."
81                    " Placeholders can be used here. "
82                    "eg. ${object.partner_id.lang}"),
83         'subject':fields.char(
84                   'Subject',
85                   size=200,
86                   help="The subject of email."
87                   " Placeholders can be used here.",
88                   translate=True),
89 #        'description':fields.text(
90 #                    'Standard Body (Text)',
91 #                    help="The text version of the mail",
92 #                    translate=True),
93 #        'body_html':fields.text(
94 #                    'Body (Text-Web Client Only)',
95 #                    help="The text version of the mail",
96 #                    translate=True),
97         'user_signature':fields.boolean(
98                   'Signature',
99                   help="the signature from the User details"
100                   " will be appended to the mail"),
101         'report_name':fields.char(
102                 'Report Filename',
103                 size=200,
104                 help="Name of the generated report file. Placeholders can be used in the filename. eg: 2009_SO003.pdf",
105                 translate=True),
106         'report_template':fields.many2one(
107                   'ir.actions.report.xml',
108                   'Report to send'),
109         'attachment_ids': fields.many2many(
110                     'ir.attachment',
111                     'email_template_attachment_rel',
112                     'email_template_id',
113                     'attachment_id',
114                     'Attached Files',
115                     help="You may attach existing files to this template, "
116                          "so they will be added in all emails created from this template"),
117         'ref_ir_act_window':fields.many2one(
118                     'ir.actions.act_window',
119                     'Window Action',
120                     help="Action that will open this email template on Resource records",
121                     readonly=True),
122         'ref_ir_value':fields.many2one(
123                    'ir.values',
124                    'Wizard Button',
125                    help="Button in the side bar of the form view of this Resource that will invoke the Window Action",
126                    readonly=True),
127         'allowed_groups':fields.many2many(
128                   'res.groups',
129                   'template_group_rel',
130                   'templ_id', 'group_id',
131                   string="Allowed User Groups",
132                   help="Only users from these groups will be"
133                   " allowed to send mails from this Template"),
134         'model_object_field':fields.many2one(
135                  'ir.model.fields',
136                  string="Field",
137                  help="Select the field from the model you want to use."
138                  "\nIf it is a relationship field you will be able to "
139                  "choose the nested values in the box below\n(Note:If "
140                  "there are no values make sure you have selected the"
141                  " correct model)",
142                  store=False),
143         'sub_object':fields.many2one(
144                  'ir.model',
145                  'Sub-model',
146                  help='When a relation field is used this field'
147                  ' will show you the type of field you have selected',
148                  store=False),
149         'sub_model_object_field':fields.many2one(
150                  'ir.model.fields',
151                  'Sub Field',
152                  help="When you choose relationship fields "
153                  "this field will specify the sub value you can use.",
154                  store=False),
155         'null_value':fields.char(
156                  'Null Value',
157                  help="This Value is used if the field is empty",
158                  size=50, store=False),
159         'copyvalue':fields.char(
160                 'Expression',
161                 size=100,
162                 help="Copy and paste the value in the "
163                 "location you want to use a system value.",
164                 store=False),
165         'table_html':fields.text(
166              'HTML code',
167              help="Copy this html code to your HTML message"
168              " body for displaying the info in your mail.",
169              store=False),
170         'auto_delete': fields.boolean('Auto Delete', help="Permanently delete emails after sending"),
171         'model': fields.related('model_id','model', type='char', size=128, string='Object'),
172     }
173
174     _sql_constraints = [
175         ('name', 'unique (name)','The template name must be unique !')
176     ]
177
178     def create_action(self, cr, uid, ids, context=None):
179         vals = {}
180         if context is None:
181             context = {}
182         action_obj = self.pool.get('ir.actions.act_window')
183         data_obj = self.pool.get('ir.model.data')
184         for template in self.browse(cr, uid, ids, context=context):
185             src_obj = template.model_id.model
186             model_data_id = data_obj._get_id(cr, uid, 'emails', 'email_compose_message_wizard_form')
187             res_id = data_obj.browse(cr, uid, model_data_id, context=context).res_id
188             vals['ref_ir_act_window'] = action_obj.create(cr, uid, {
189                  'name': template.name,
190                  'type': 'ir.actions.act_window',
191                  'res_model': 'email.compose.message',
192                  'src_model': src_obj,
193                  'view_type': 'form',
194                  'context': "{'email_model':'%s','template_id':'%d','src_rec_id':active_id,'src_rec_ids':active_ids}" % (src_obj, template.id),
195                  'view_mode':'form,tree',
196                  'view_id': res_id,
197                  'target': 'new',
198                  'auto_refresh':1
199             }, context)
200             vals['ref_ir_value'] = self.pool.get('ir.values').create(cr, uid, {
201                  'name': _('Send Mail (%s)') % template.name,
202                  'model': src_obj,
203                  'key2': 'client_action_multi',
204                  'value': "ir.actions.act_window," + str(vals['ref_ir_act_window']),
205                  'object': True,
206              }, context)
207         self.write(cr, uid, ids, {
208                     'ref_ir_act_window': vals.get('ref_ir_act_window',False),
209                     'ref_ir_value': vals.get('ref_ir_value',False),
210                 }, context)
211         return True
212
213     def unlink_action(self, cr, uid, ids, context=None):
214         for template in self.browse(cr, uid, ids, context=context):
215             try:
216                 if template.ref_ir_act_window:
217                     self.pool.get('ir.actions.act_window').unlink(cr, uid, template.ref_ir_act_window.id, context)
218                 if template.ref_ir_value:
219                     self.pool.get('ir.values').unlink(cr, uid, template.ref_ir_value.id, context)
220             except:
221                 raise osv.except_osv(_("Warning"), _("Deletion of Record failed"))
222
223     def delete_action(self, cr, uid, ids, context=None):
224         self.unlink_action(cr, uid, ids, context=context)
225         return True
226
227     def unlink(self, cr, uid, ids, context=None):
228         self.unlink_action(cr, uid, ids, context=context)
229         return super(email_template, self).unlink(cr, uid, ids, context=context)
230
231     def copy(self, cr, uid, id, default=None, context=None):
232         if default is None:
233             default = {}
234         default = default.copy()
235         old = self.read(cr, uid, id, ['name'], context=context)
236         new_name = _("Copy of template ") + old.get('name', 'No Name')
237         check = self.search(cr, uid, [('name', '=', new_name)], context=context)
238         if check:
239             new_name = new_name + '_' + random.choice('abcdefghij') + random.choice('lmnopqrs') + random.choice('tuvwzyz')
240         default.update({'name':new_name})
241         return super(email_template, self).copy(cr, uid, id, default, context)
242
243     def build_expression(self, field_name, sub_field_name, null_value):
244         """
245         Returns a template expression based on data provided
246         @param field_name: field name
247         @param sub_field_name: sub field name (M2O)
248         @param null_value: default value if the target value is empty
249         @return: computed expression
250         """
251         expression = ''
252         if field_name:
253             expression = "${object." + field_name
254             if sub_field_name:
255                 expression += "." + sub_field_name
256             if null_value:
257                 expression += " or '''%s'''" % null_value
258             expression += "}"
259         return expression
260 #
261 #    def onchange_model_object_field(self, cr, uid, ids, model_object_field, context=None):
262 #        if not model_object_field:
263 #            return {}
264 #        result = {}
265 #        field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
266 #        #Check if field is relational
267 #        if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
268 #            res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
269 #            if res_ids:
270 #                result['sub_object'] = res_ids[0]
271 #                result['copyvalue'] = self.build_expression(False, False, False)
272 #                result['sub_model_object_field'] = False
273 #                result['null_value'] = False
274 #        else:
275 #            #Its a simple field... just compute placeholder
276 #            result['sub_object'] = False
277 #            result['copyvalue'] = self.build_expression(field_obj.name, False, False)
278 #            result['sub_model_object_field'] = False
279 #            result['null_value'] = False
280 #        return {'value':result}
281 #
282 #    def onchange_sub_model_object_field(self, cr, uid, ids, model_object_field, sub_model_object_field, context=None):
283 #        if not model_object_field or not sub_model_object_field:
284 #            return {}
285 #        result = {}
286 #        field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
287 #        if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
288 #            res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
289 #            sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context)
290 #            if res_ids:
291 #                result['sub_object'] = res_ids[0]
292 #                result['copyvalue'] = self.build_expression(field_obj.name, sub_field_obj.name, False)
293 #                result['sub_model_object_field'] = sub_model_object_field
294 #                result['null_value'] = False
295 #        else:
296 #            #Its a simple field... just compute placeholder
297 #            result['sub_object'] = False
298 #            result['copyvalue'] = self.build_expression(field_obj.name, False, False)
299 #            result['sub_model_object_field'] = False
300 #            result['null_value'] = False
301 #        return {'value':result}
302 #
303 #
304 #    def onchange_null_value(self, cr, uid, ids, model_object_field, sub_model_object_field, null_value, template_language, context=None):
305 #        if not model_object_field and not null_value:
306 #            return {}
307 #        result = {}
308 #        field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
309 #        if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
310 #            res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
311 #            sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context)
312 #            if res_ids:
313 #                result['sub_object'] = res_ids[0]
314 #                result['copyvalue'] = self.build_expression(field_obj.name,
315 #                                                      sub_field_obj.name,
316 #                                                      null_value,
317 #                                                      template_language
318 #                                                      )
319 #                result['sub_model_object_field'] = sub_model_object_field
320 #                result['null_value'] = null_value
321 #        else:
322 #            #Its a simple field... just compute placeholder
323 #            result['sub_object'] = False
324 #            result['copyvalue'] = self.build_expression(field_obj.name,
325 #                                                  False,
326 #                                                  null_value,
327 #                                                  template_language
328 #                                                  )
329 #            result['sub_model_object_field'] = False
330 #            result['null_value'] = null_value
331 #        return {'value':result}
332
333     def onchange_sub_model_object_value_field(self, cr, uid, ids, model_object_field, sub_model_object_field=False, null_value=None, context=None):
334         result = {
335             'sub_object': False,
336             'copyvalue': False,
337             'sub_model_object_field': False,
338             'null_value': False
339             }
340         if model_object_field:
341             fields_obj = self.pool.get('ir.model.fields')
342             field_value = fields_obj.browse(cr, uid, model_object_field, context)
343             if field_value.ttype in ['many2one', 'one2many', 'many2many']:
344                 res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_value.relation)], context=context)
345                 sub_field_value = False
346                 if sub_model_object_field:
347                     sub_field_value = fields_obj.browse(cr, uid, sub_model_object_field, context)
348                 if res_ids:
349                     result.update({
350                         'sub_object': res_ids[0],
351                         'copyvalue': self.build_expression(field_value.name, sub_field_value and sub_field_value.name or False, null_value or False),
352                         'sub_model_object_field': sub_model_object_field or False,
353                         'null_value': null_value or False
354                         })
355             else:
356                 result.update({
357                         'copyvalue': self.build_expression(field_value.name, False, null_value or False),
358                         'null_value': null_value or False
359                         })
360         return {'value':result}
361
362
363     def _generate_email(self, cr, uid, template_id, record_id, context=None):
364         """
365         Generates an email from the template for
366         record record_id of target object
367         """
368         if context is None:
369             context = {}
370         smtp_pool = self.pool.get('email.smtp_server')
371         email_message_pool = self.pool.get('email.message')
372         report_xml_pool = self.pool.get('ir.actions.report.xml')
373         template = self.get_email_template(cr, uid, template_id, record_id, context)
374         smtp_server_id = context.get('smtp_server_id', False)
375         if not smtp_server_id and template.smtp_server_id:
376             smtp_server_id = template.smtp_server_id.id
377         else:
378             smtp_ids = smtp_pool.search(cr, uid, [('default','=',True)])
379             smtp_server_id = smtp_ids and smtp_ids[0]
380         smtp_server = smtp_pool.browse(cr, uid, smtp_server_id, context=context)
381         # determine name of sender, either it is specified in email_id
382
383         email_id = smtp_server.email_id.strip()
384         email_from = re.findall(r'([^ ,<@]+@[^> ,]+)', email_id)[0]
385         if email_from != email_id:
386             email_from = smtp_server.email_id
387         else:
388             email_from = tools.ustr(smtp_server.name) + "<" + tools.ustr(email_id) + ">"
389
390         model = template.model_id.model
391         values = {
392             'email_from': email_from,
393             'email_to': self.get_template_value(cr, uid, template.email_to, model, record_id, context),
394             'email_cc': self.get_template_value(cr, uid, template.email_cc, model, record_id, context),
395             'email_bcc': self.get_template_value(cr, uid, template.email_bcc, model, record_id, context),
396             'reply_to': self.get_template_value(cr, uid, template.reply_to, model, record_id, context),
397             'name': self.get_template_value(cr, uid, template.subject, model, record_id, context),
398             'description': self.get_template_value(cr, uid, template.description, model, record_id, context),
399             #'body_html': self.get_template_value(cr, uid, template.body_html, model, record_id, context),
400         }
401
402         if template.message_id:
403             # use provided message_id with placeholders
404             values.update({'message_id': self.get_template_value(cr, uid, template.message_id, model, record_id, context)})
405
406         elif template['track_campaign_item']:
407             # get appropriate message-id
408             values.update({'message_id': tools.misc.generate_tracking_message_id(record_id)})
409
410         #Use signatures if allowed
411         if template.user_signature:
412             sign = self.pool.get('res.users').read(cr, uid, uid, ['signature'], context)['signature']
413             if values['description']:
414                 values['description'] += '\n\n' + sign
415             #if values['body_html']:
416             #    values['body_html'] += sign
417
418         attachment = []
419
420         # Add report as a Document
421         if template.report_template:
422             report_name = template.report_name
423             reportname = 'report.' + report_xml_pool.browse(cr, uid, template.report_template.id, context).report_name
424             data = {}
425             data['model'] = template.model
426
427             # Ensure report is rendered using template's language
428             ctx = context.copy()
429             if template.lang:
430                 ctx['lang'] = self.get_template_value(cr, uid, template.lang, template.model, record_id, context)
431             service = netsvc.LocalService(reportname)
432             (result, format) = service.create(cr, uid, [record_id], data, ctx)
433             result = base64.b64encode(result)
434             if not report_name:
435                 report_name = reportname
436             report_name = report_name + "." + format
437             attachment.append((report_name, result))
438
439
440         # Add document attachments
441         for attach in template.attachment_ids:
442             #attach = attahcment_obj.browse(cr, uid, attachment_id, context)
443             attachment.append((attach.datas_fname, attach.datas))
444
445         #Send emails
446         context.update({'notemplate':True})
447         email_id = email_message_pool.email_send(cr, uid, values.get('email_from'), values.get('email_to'), values.get('name'), values.get('description'),
448                     model=model, email_cc=values.get('email_cc'), email_bcc=values.get('email_bcc'), reply_to=values.get('reply_to'),
449                     attach=attachment, message_id=values.get('message_id'), openobject_id=record_id, debug=True, subtype='plain', x_headers={}, priority='3', smtp_server_id=smtp_server.id, context=context)
450         email_message_pool.write(cr, uid, email_id, {'template_id': context.get('template_id',template.id)})
451         return email_id
452
453
454
455     def generate_email(self, cr, uid, template_id, record_id,  context=None):
456         if context is None:
457             context = {}
458         email_id = self._generate_email(cr, uid, template_id, record_id, context)
459         return email_id
460
461 email_template()
462
463 class email_message(osv.osv):
464     _inherit = 'email.message'
465     _columns = {
466         'template_id': fields.many2one('email.template', 'Email-Template', readonly=True),
467         }
468
469     def process_email_queue(self, cr, uid, ids=None, context=None):
470         result = super(email_message, self).process_email_queue(cr, uid, ids, context)
471         attachment_obj = self.pool.get('ir.attachment')
472         for message in self.browse(cr, uid, result, context):
473             if message.template_id and message.template_id.auto_delete:
474                 self.unlink(cr, uid, [id], context=context)
475                 attachment_ids = [x.id for x in message.attachments_ids]
476                 attachment_obj.unlink(cr, uid, attachment_ids, context=context)
477         return result
478
479     def email_send(self, cr, uid, email_from, email_to, subject, body, model=False, email_cc=None, email_bcc=None, reply_to=False, attach=None,
480             message_id=False, references=False, openobject_id=False, debug=False, subtype='plain', x_headers={}, priority='3', smtp_server_id=False, context=None):
481         if context is None:
482             context = {}
483         notemplate = context.get('notemplate', True)
484         if (not notemplate) and model and openobject_id:
485             template_pool = self.pool.get('email.template')
486             template_ids = template_pool.search(cr, uid, [('model','=',model)])
487             if template_ids and len(template_ids):
488                 template_id = template_ids[0]
489                 return template_pool.generate_email(cr, uid, template_id, openobject_id, context=context)
490
491         return super(email_message, self).email_send(cr, uid, email_from, email_to, subject, body, model=model, email_cc=email_cc, email_bcc=email_bcc, reply_to=reply_to, attach=attach,
492                 message_id=message_id, references=references, openobject_id=openobject_id, debug=debug, subtype=subtype, x_headers=x_headers, priority=priority, smtp_server_id=smtp_server_id, context=context)
493
494 email_message()
495
496 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: