1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2009 Sharoon Thomas
6 # Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
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.
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.
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/>
21 ##############################################################################
24 from osv import fields
30 from tools.translate import _
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'
40 def get_template_value(self, cr, uid, message=None, model=None, record_id=None, context=None):
42 return mako_template.get_value(cr, uid, message=message, model=model, record_id=record_id, context=context)
44 def get_email_template(self, cr, uid, template_id=False, record_id=None, context=None):
45 "Return Template Object"
49 template_id = context.get('template_id', False)
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)
56 # Use translated template if necessary
59 template = self.browse(cr, uid, template.id, ctx)
62 def onchange_model_id(self, cr, uid, ids, model_id, context=None):
65 mod_name = self.pool.get('ir.model').browse(cr, uid, model_id, context).model
66 return {'value':{'model':mod_name}}
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"),
80 help="The default language for the email."
81 " Placeholders can be used here. "
82 "eg. ${object.partner_id.lang}"),
83 'subject':fields.char(
86 help="The subject of email."
87 " Placeholders can be used here.",
89 # 'description':fields.text(
90 # 'Standard Body (Text)',
91 # help="The text version of the mail",
93 # 'body_html':fields.text(
94 # 'Body (Text-Web Client Only)',
95 # help="The text version of the mail",
97 'user_signature':fields.boolean(
99 help="the signature from the User details"
100 " will be appended to the mail"),
101 'report_name':fields.char(
104 help="Name of the generated report file. Placeholders can be used in the filename. eg: 2009_SO003.pdf",
106 'report_template':fields.many2one(
107 'ir.actions.report.xml',
109 'attachment_ids': fields.many2many(
111 'email_template_attachment_rel',
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',
120 help="Action that will open this email template on Resource records",
122 'ref_ir_value':fields.many2one(
125 help="Button in the side bar of the form view of this Resource that will invoke the Window Action",
127 'allowed_groups':fields.many2many(
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(
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"
143 'sub_object':fields.many2one(
146 help='When a relation field is used this field'
147 ' will show you the type of field you have selected',
149 'sub_model_object_field':fields.many2one(
152 help="When you choose relationship fields "
153 "this field will specify the sub value you can use.",
155 'null_value':fields.char(
157 help="This Value is used if the field is empty",
158 size=50, store=False),
159 'copyvalue':fields.char(
162 help="Copy and paste the value in the "
163 "location you want to use a system value.",
165 'table_html':fields.text(
167 help="Copy this html code to your HTML message"
168 " body for displaying the info in your mail.",
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'),
175 ('name', 'unique (name)','The template name must be unique !')
178 def create_action(self, cr, uid, ids, context=None):
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,
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',
200 vals['ref_ir_value'] = self.pool.get('ir.values').create(cr, uid, {
201 'name': _('Send Mail (%s)') % template.name,
203 'key2': 'client_action_multi',
204 'value': "ir.actions.act_window," + str(vals['ref_ir_act_window']),
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),
213 def unlink_action(self, cr, uid, ids, context=None):
214 for template in self.browse(cr, uid, ids, context=context):
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)
221 raise osv.except_osv(_("Warning"), _("Deletion of Record failed"))
223 def delete_action(self, cr, uid, ids, context=None):
224 self.unlink_action(cr, uid, ids, context=context)
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)
231 def copy(self, cr, uid, id, default=None, context=None):
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)
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)
243 def build_expression(self, field_name, sub_field_name, null_value):
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
253 expression = "${object." + field_name
255 expression += "." + sub_field_name
257 expression += " or '''%s'''" % null_value
261 # def onchange_model_object_field(self, cr, uid, ids, model_object_field, context=None):
262 # if not model_object_field:
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)
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
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}
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:
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)
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
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}
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:
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)
313 # result['sub_object'] = res_ids[0]
314 # result['copyvalue'] = self.build_expression(field_obj.name,
315 # sub_field_obj.name,
319 # result['sub_model_object_field'] = sub_model_object_field
320 # result['null_value'] = null_value
322 # #Its a simple field... just compute placeholder
323 # result['sub_object'] = False
324 # result['copyvalue'] = self.build_expression(field_obj.name,
329 # result['sub_model_object_field'] = False
330 # result['null_value'] = null_value
331 # return {'value':result}
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):
337 'sub_model_object_field': False,
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)
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
357 'copyvalue': self.build_expression(field_value.name, False, null_value or False),
358 'null_value': null_value or False
360 return {'value':result}
363 def _generate_email(self, cr, uid, template_id, record_id, context=None):
365 Generates an email from the template for
366 record record_id of target object
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
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
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
388 email_from = tools.ustr(smtp_server.name) + "<" + tools.ustr(email_id) + ">"
390 model = template.model_id.model
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),
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)})
406 elif template['track_campaign_item']:
407 # get appropriate message-id
408 values.update({'message_id': tools.misc.generate_tracking_message_id(record_id)})
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
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
425 data['model'] = template.model
427 # Ensure report is rendered using template's language
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)
435 report_name = reportname
436 report_name = report_name + "." + format
437 attachment.append((report_name, result))
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))
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)})
455 def generate_email(self, cr, uid, template_id, record_id, context=None):
458 email_id = self._generate_email(cr, uid, template_id, record_id, context)
463 class email_message(osv.osv):
464 _inherit = 'email.message'
466 'template_id': fields.many2one('email.template', 'Email-Template', readonly=True),
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)
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):
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)
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)
496 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: