1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>
20 ##############################################################################
25 from osv import fields
27 class mail_compose_message(osv.osv_memory):
28 _inherit = 'mail.compose.message'
30 def _get_templates(self, cr, uid, context=None):
34 email_template_obj = self.pool.get('email.template')
35 message_id = context.get('default_parent_id', context.get('message_id', context.get('active_id')))
37 if context.get('default_composition_mode') == 'reply' and message_id:
38 message_data = self.pool.get('mail.message').browse(cr, uid, message_id, context=context)
40 model = message_data.model
42 model = context.get('default_model', context.get('active_model'))
45 record_ids = email_template_obj.search(cr, uid, [('model', '=', model)], context=context)
46 return email_template_obj.name_get(cr, uid, record_ids, context) + [(False, '')]
49 def default_get(self, cr, uid, fields, context=None):
52 result = super(mail_compose_message, self).default_get(cr, uid, fields, context=context)
53 result['template_id'] = context.get('default_template_id', context.get('mail.compose.template_id', False))
54 # force html when using templates
55 if result.get('use_template'):
56 result['content_subtype'] = 'html'
60 'use_template': fields.boolean('Use Template'),
61 # incredible hack of the day: size=-1 means we want an int db column instead of an str one
62 'template_id': fields.selection(_get_templates, 'Template', size=-1),
65 def onchange_template_id(self, cr, uid, ids, use_template, template_id, composition_mode, model, res_id, context=None):
66 """ - use_template not set: return default_get
67 - use_template set in mass_mailing: we cannot render, so return the template values
68 - use_template set: return rendered values """
69 if use_template and template_id and composition_mode == 'mass_mail':
70 values = self.pool.get('email.template').read(cr, uid, template_id, ['subject', 'body_html'], context)
72 elif use_template and template_id:
73 values = self.generate_email_for_composer(cr, uid, template_id, res_id, context=context)
74 # transform attachments into attachment_ids
75 values['attachment_ids'] = []
76 ir_attach_obj = self.pool.get('ir.attachment')
77 for attach_fname, attach_datas in values.pop('attachments', []):
80 'datas': attach_datas,
81 'datas_fname': attach_fname,
85 values['attachment_ids'].append(ir_attach_obj.create(cr, uid, data_attach, context=context))
87 values = self.default_get(cr, uid, ['body', 'body_html', 'subject', 'partner_ids', 'attachment_ids'], context=context)
89 if values.get('body_html'):
90 values['body'] = values.pop('body_html')
91 values.update(use_template=use_template, template_id=template_id)
92 return {'value': values}
94 def toggle_template(self, cr, uid, ids, context=None):
95 """ hit toggle template mode button: calls onchange_use_template to
96 emulate an on_change, then writes the values to update the form. """
97 for record in self.browse(cr, uid, ids, context=context):
98 onchange_res = self.onchange_use_template(cr, uid, ids, not record.use_template,
99 record.template_id, record.composition_mode, record.model, record.res_id, context=context).get('value', {})
100 # update partner_ids and attachment_ids
101 onchange_res['partner_ids'] = [(4, partner_id) for partner_id in onchange_res.pop('partner_ids', [])]
102 onchange_res['attachment_ids'] = [(4, attachment_id) for attachment_id in onchange_res.pop('attachment_ids', [])]
103 record.write(onchange_res)
106 def onchange_use_template(self, cr, uid, ids, use_template, template_id, composition_mode, model, res_id, context=None):
107 """ onchange_use_template (values: True or False). If use_template is
108 False, we do as an onchange with template_id False for values """
109 values = self.onchange_template_id(cr, uid, ids, use_template,
110 template_id, composition_mode, model, res_id, context=context)
111 # force html when using templates
113 values['value']['content_subtype'] = 'html'
116 def save_as_template(self, cr, uid, ids, context=None):
117 """ hit save as template button: current form value will be a new
118 template attached to the current document. """
119 email_template = self.pool.get('email.template')
120 ir_model_pool = self.pool.get('ir.model')
121 for record in self.browse(cr, uid, ids, context=context):
122 model_ids = ir_model_pool.search(cr, uid, [('model', '=', record.model)], context=context)
123 model_id = model_ids and model_ids[0] or False
126 model_name = ir_model_pool.browse(cr, uid, model_id, context=context).name
127 template_name = "%s: %s" % (model_name, tools.ustr(record.subject))
129 'name': template_name,
130 'subject': record.subject or False,
131 'body_html': record.body or False,
132 'model_id': model_id or False,
133 'attachment_ids': [(6, 0, [att.id for att in record.attachment_ids])]
135 template_id = email_template.create(cr, uid, values, context=context)
136 record.write({'template_id': template_id, 'use_template': True})
139 #------------------------------------------------------
140 # Wizard validation and send
141 #------------------------------------------------------
143 def generate_email_for_composer(self, cr, uid, template_id, res_id, context=None):
144 """ Call email_template.generate_email(), get fields relevant for
145 mail.compose.message, transform email_cc and email_to into partner_ids """
146 template_values = self.pool.get('email.template').generate_email(cr, uid, template_id, res_id, context=context)
147 # filter template values
148 fields = ['body', 'body_html', 'subject', 'email_to', 'email_cc', 'attachments']
149 values = dict((field, template_values[field]) for field in fields if template_values.get(field))
150 values['body'] = values.pop('body_html', '')
151 # transform email_to, email_cc into partner_ids
152 values['partner_ids'] = []
153 mails = tools.email_split(values.pop('email_to', '') + ' ' + values.pop('email_cc', ''))
155 partner_id = self.pool.get('res.partner').find_or_create(cr, uid, mail, context=context)
156 values['partner_ids'].append(partner_id)
159 def render_message(self, cr, uid, wizard, res_id, context=None):
160 """ Generate an email from the template for given (model, res_id) pair.
161 This method is meant to be inherited by email_template that will
162 produce a more complete dictionary, with email_to, ...
164 # generate the composer email
165 values = self.generate_email_for_composer(cr, uid, wizard.template_id, res_id, context=context)
166 # get values to return
167 email_dict = super(mail_compose_message, self).render_message(cr, uid, wizard, res_id, context)
168 email_dict.update(values)
171 def render_template(self, cr, uid, template, model, res_id, context=None):
172 return self.pool.get('email.template').render_template(cr, uid, template, model, res_id, context=context)
174 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: