1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2009 Sharoon Thomas
6 # Copyright (C) 2010-2010 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 ##############################################################################
31 from osv import osv, fields
32 from tools.translate import _
35 from mako.template import Template as MakoTemplate
36 TEMPLATE_ENGINES.append(('mako', 'Mako Templates'))
38 logging.getLogger('init').warning("module email_template: Mako templates not installed")
44 def get_value(cursor, user, recid, message=None, template=None, context=None):
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""
55 pool = pooler.get_pool(cursor.dbname)
58 #Returns the computed expression
61 message = tools.ustr(message)
62 object = pool.get(template.model_int_name).browse(cursor, user, recid, context=context)
64 'user':pool.get('res.users').browse(cursor, user, user, context=context),
67 if template.template_language == 'mako':
68 templ = MakoTemplate(message, input_encoding='utf-8')
69 reply = MakoTemplate(message).render_unicode(object=object,
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))
80 logging.exception("can't render %r", message)
85 class email_template(osv.osv):
86 "Templates for sending Email"
88 _name = "email.template"
89 _description = 'Email Templates for Models'
91 def change_model(self, cursor, user, ids, object_name, context=None):
93 mod_name = self.pool.get('ir.model').read(
97 ['model'], context)['model']
101 'value':{'model_int_name':mod_name}
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(
110 string="Email Account",
111 help="Emails will be sent from this approved account."),
112 'def_to':fields.char(
115 help="The Recipient of email. "
116 "Placeholders can be used here. "
117 "e.g. ${object.email_to}"),
118 'def_cc':fields.char(
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(
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',
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',
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"),
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(
153 help="The subject of email."
154 " Placeholders can be used here.",
156 'def_body_text':fields.text(
157 'Standard Body (Text)',
158 help="The text version of the mail",
160 'def_body_html':fields.text(
161 'Body (Text-Web Client Only)',
162 help="The text version of the mail",
164 'use_sign':fields.boolean(
166 help="the signature from the User details"
167 " will be appended to the mail"),
168 'file_name':fields.char(
171 help="Name of the generated report file. Placeholders can be used in the filename. eg: 2009_SO003.pdf",
173 'report_template':fields.many2one(
174 'ir.actions.report.xml',
176 'attachment_ids': fields.many2many(
178 'email_template_attachment_rel',
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',
187 help="Action that will open this email template on Resource records",
189 'ref_ir_value':fields.many2one(
192 help="Button in the side bar of the form view of this Resource that will invoke the Window Action",
194 'allowed_groups':fields.many2many(
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(
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"
210 'sub_object':fields.many2one(
213 help='When a relation field is used this field'
214 ' will show you the type of field you have selected',
216 'sub_model_object_field':fields.many2one(
219 help="When you choose relationship fields "
220 "this field will specify the sub value you can use.",
222 'null_value':fields.char(
224 help="This Value is used if the field is empty",
225 size=50, store=False),
226 'copyvalue':fields.char(
229 help="Copy and paste the value in the "
230 "location you want to use a system value.",
232 'table_html':fields.text(
234 help="Copy this html code to your HTML message"
235 " body for displaying the info in your mail.",
237 #Template language(engine eg.Mako) specifics
238 'template_language':fields.selection(
240 'Templating Language',
243 'auto_delete': fields.boolean('Auto Delete', help="Permanently delete emails after sending"),
247 'template_language' : lambda *a:'mako',
252 ('name', 'unique (name)','The template name must be unique !')
255 def create_action(self, cr, uid, ids, context=None):
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,
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],
273 vals['ref_ir_value'] = self.pool.get('ir.values').create(cr, uid, {
274 'name': _('Send Mail (%s)') % template_obj.name,
276 'key2': 'client_action_multi',
277 'value': "ir.actions.act_window," + str(vals['ref_ir_act_window']),
280 self.write(cr, uid, ids, {
281 'ref_ir_act_window': vals['ref_ir_act_window'],
282 'ref_ir_value': vals['ref_ir_value'],
286 def unlink_action(self, cr, uid, ids, context=None):
287 for template in self.browse(cr, uid, ids, context=context):
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)
294 raise osv.except_osv(_("Warning"), _("Deletion of Record failed"))
296 def delete_action(self, cr, uid, ids, context=None):
297 self.unlink_action(cr, uid, ids, context=context)
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)
304 def copy(self, cr, uid, id, default=None, context=None):
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)
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)
316 def build_expression(self, field_name, sub_field_name, null_value, template_language='mako'):
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
327 if template_language == 'mako':
329 expression = "${object." + field_name
331 expression += "." + sub_field_name
333 expression += " or '''%s'''" % null_value
335 elif template_language == 'django':
337 expression = "{{object." + field_name
339 expression += "." + sub_field_name
341 expression += "|default: '''%s'''" % null_value
345 def onchange_model_object_field(self, cr, uid, ids, model_object_field, template_language, context=None):
346 if not model_object_field:
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)
354 result['sub_object'] = res_ids[0]
355 result['copyvalue'] = self.build_expression(False,
359 result['sub_model_object_field'] = False
360 result['null_value'] = False
362 #Its a simple field... just compute placeholder
363 result['sub_object'] = False
364 result['copyvalue'] = self.build_expression(field_obj.name,
369 result['sub_model_object_field'] = False
370 result['null_value'] = False
371 return {'value':result}
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:
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)
382 result['sub_object'] = res_ids[0]
383 result['copyvalue'] = self.build_expression(field_obj.name,
388 result['sub_model_object_field'] = sub_model_object_field
389 result['null_value'] = False
391 #Its a simple field... just compute placeholder
392 result['sub_object'] = False
393 result['copyvalue'] = self.build_expression(field_obj.name,
398 result['sub_model_object_field'] = False
399 result['null_value'] = False
400 return {'value':result}
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:
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)
411 result['sub_object'] = res_ids[0]
412 result['copyvalue'] = self.build_expression(field_obj.name,
417 result['sub_model_object_field'] = sub_model_object_field
418 result['null_value'] = null_value
420 #Its a simple field... just compute placeholder
421 result['sub_object'] = False
422 result['copyvalue'] = self.build_expression(field_obj.name,
427 result['sub_model_object_field'] = False
428 result['null_value'] = null_value
429 return {'value':result}
431 def _add_attachment(self, cursor, user, mailbox_id, name, data, filename, context=None):
433 Add an attachment to a given mailbox entry.
435 :param data: base64 encoded attachment data to store
437 attachment_obj = self.pool.get('ir.attachment')
439 'name': (name or '') + _(' (Email Attachment)'),
441 'datas_fname': filename,
442 'description': name or _('No Description'),
443 'res_model':'email_template.mailbox',
444 'res_id': mailbox_id,
446 attachment_id = attachment_obj.create(cursor,
451 self.pool.get('email_template.mailbox').write(
456 'attachments_ids':[(4, attachment_id)],
457 'mail_type':'multipart/mixed'
461 def generate_attach_reports(self,
469 Generate report to be attached and attach it
470 to the email, and add any directly attached files as well.
472 @param cursor: Database Cursor
473 @param user: ID of User
474 @param template: Browse record of
476 @param record_id: ID of the target model
477 for which this mail has
479 @param mail: Browse record of email object
482 if template.report_template:
483 reportname = 'report.' + \
484 self.pool.get('ir.actions.report.xml').read(
487 template.report_template.id,
489 context)['report_name']
490 service = netsvc.LocalService(reportname)
492 data['model'] = template.model_int_name
493 (result, format) = service.create(cursor,
498 fname = tools.ustr(get_value(cursor, user, record_id,
499 template.file_name, template, context)
502 if not fname.endswith(ext):
504 self._add_attachment(cursor, user, mail.id, mail.subject, base64.b64encode(result), fname, context)
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)
512 def _generate_mailbox_item_from_template(self,
519 Generates an email from the template for
520 record record_id of target object
522 @param cursor: Database Cursor
523 @param user: ID of User
524 @param template: Browse record of
526 @param record_id: ID of the target model
527 for which this mail has
529 @return: ID of created object
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(
538 context.get('account_id'),
539 ['name', 'email_id'],
544 'id':template.from_account.id,
545 'name':template.from_account.name,
546 'email_id':template.from_account.email_id
548 lang = get_value(cursor,
556 ctx.update({'lang':lang})
557 template = self.browse(cursor, user, template.id, context=ctx)
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']
567 email_from = tools.ustr(from_account['name']) + "<" + tools.ustr(email_id) + ">"
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.
572 'email_from': email_from,
573 'email_to':get_value(cursor,
579 'email_cc':get_value(cursor,
585 'email_bcc':get_value(cursor,
591 'reply_to':get_value(cursor,
597 'subject':get_value(cursor,
600 template.def_subject,
603 'body_text':get_value(cursor,
606 template.def_body_text,
609 'body_html':get_value(cursor,
612 template.def_body_html,
615 'account_id' :from_account['id'],
616 #This is a mandatory field when automatic emails are sent
619 'mail_type':'multipart/alternative',
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)})
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)})
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,
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(
652 def generate_mail(self,
660 template = self.browse(cursor, user, template_id, context=context)
662 raise Exception("The requested template could not be loaded")
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(
672 mail = mailbox_obj.browse(
678 if template.report_template or template.attachment_ids:
679 self.generate_attach_reports(
688 self.pool.get('email_template.mailbox').write(
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)
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!
704 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: