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
26 from tools.translate import _
30 class mail_compose_message(osv.osv_memory):
31 _inherit = 'mail.compose.message'
33 def _get_templates(self, cr, uid, context=None):
35 Return Email Template of particular Model.
40 email_template= self.pool.get('email.template')
42 if context.get('message_id'):
43 mail_message = self.pool.get('mail.message')
44 message_data = mail_message.browse(cr, uid, int(context.get('message_id')), context)
45 model = message_data.model
46 elif context.get('mail.compose.target.model') or context.get('active_model'):
47 model = context.get('mail.compose.target.model', context.get('active_model'))
49 record_ids = email_template.search(cr, uid, [('model', '=', model)])
50 return email_template.name_get(cr, uid, record_ids, context) + [(False,'')]
54 'use_template': fields.boolean('Use Template'),
55 'template_id': fields.selection(_get_templates, 'Template',
56 size=-1 # means we want an int db column
61 'template_id' : lambda self, cr, uid, context={} : context.get('mail.compose.template_id', False)
64 def on_change_template(self, cr, uid, ids, use_template, template_id, email_from=None, email_to=None, context=None):
69 res_id = context.get('active_id', False)
70 if context.get('mail.compose.message.mode') == 'mass_mail':
71 # use the original template values - to be rendered when actually sent
72 # by super.send_mail()
73 values = self.pool.get('email.template').read(cr, uid, template_id, self.fields_get_keys(cr, uid), context)
75 # render the mail as one-shot
76 values = self.pool.get('email.template').generate_email(cr, uid, template_id, res_id, context=context)
77 # get partner_ids back
78 values['dest_partner_ids'] = values['partner_ids']
79 # retrofit generated attachments in the expected field format
80 if values['attachments']:
81 attachment = values.pop('attachments')
82 attachment_obj = self.pool.get('ir.attachment')
84 for fname, fcontent in attachment.iteritems():
90 'res_model' : self._name,
91 'res_id' : ids[0] if ids else False
93 att_ids.append(attachment_obj.create(cr, uid, data_attach))
94 values['attachment_ids'] = att_ids
97 values = self.default_get(cr, uid, self.fields_get_keys(cr, uid), context)
98 values.update(use_template=use_template, template_id=template_id)
100 return {'value': values}
102 def template_toggle(self, cr, uid, ids, context=None):
103 for record in self.browse(cr, uid, ids, context=context):
105 use_template = record.use_template
106 # simulate an on_change on use_template
107 values.update(self.onchange_use_template(cr, uid, ids, not use_template, context=context)['value'])
111 def onchange_use_template(self, cr, uid, ids, use_template, context=None):
112 values = {'use_template': use_template}
113 for record in self.browse(cr, uid, ids, context=context):
115 # equivalent to choosing an empty template
116 onchange_template_values = self.on_change_template(cr, uid, record.id, use_template,
117 False, email_from=record.email_from, email_to=record.email_to, context=context)
118 values.update(onchange_template_values['value'])
119 return {'value': values}
121 def save_as_template(self, cr, uid, ids, context=None):
124 email_template = self.pool.get('email.template')
125 model_pool = self.pool.get('ir.model')
126 for record in self.browse(cr, uid, ids, context=context):
127 model = record.model or context.get('active_model')
128 model_ids = model_pool.search(cr, uid, [('model', '=', model)])
129 model_id = model_ids and model_ids[0] or False
132 model_name = model_pool.browse(cr, uid, model_id, context=context).name
133 template_name = "%s: %s" % (model_name, tools.ustr(record.subject))
135 'name': template_name,
136 'email_from': record.email_from or False,
137 'subject': record.subject or False,
138 'body_text': record.body_text or False,
139 'email_to': record.email_to or False,
140 'email_cc': record.email_cc or False,
141 'email_bcc': record.email_bcc or False,
142 'reply_to': record.reply_to or False,
143 'model_id': model_id or False,
144 'attachment_ids': [(6, 0, [att.id for att in record.attachment_ids])]
146 template_id = email_template.create(cr, uid, values, context=context)
147 record.write({'template_id': template_id,
148 'use_template': True})
150 # _reopen same wizard screen with new template preselected
153 # override the basic implementation
154 def render_template(self, cr, uid, template, model, res_id, context=None):
155 return self.pool.get('email.template').render_template(cr, uid, template, model, res_id, context=context)
157 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: