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 def _reopen(self,res_id,model):
31 return {'type': 'ir.actions.act_window',
35 'res_model': self._name,
38 # save original model in context, otherwise
39 # it will be lost on the action's context switch
40 'context': {'mail.compose.target.model': model}
43 class mail_compose_message(osv.osv_memory):
44 _inherit = 'mail.compose.message'
46 def _get_templates(self, cr, uid, context=None):
48 Return Email Template of particular Model.
53 email_template= self.pool.get('email.template')
55 if context.get('message_id'):
56 mail_message = self.pool.get('mail.message')
57 message_data = mail_message.browse(cr, uid, int(context.get('message_id')), context)
58 model = message_data.model
59 elif context.get('mail.compose.target.model') or context.get('active_model'):
60 model = context.get('mail.compose.target.model', context.get('active_model'))
62 record_ids = email_template.search(cr, uid, [('model', '=', model)])
63 return email_template.name_get(cr, uid, record_ids, context) + [(False,'')]
67 'use_template': fields.boolean('Use Template'),
68 'template_id': fields.selection(_get_templates, 'Template',
69 size=-1 # means we want an int db column
74 'template_id' : lambda self, cr, uid, context={} : context.get('mail.compose.template_id', False)
77 def on_change_template(self, cr, uid, ids, use_template, template_id, email_from=None, email_to=None, context=None):
82 res_id = context.get('active_id', False)
83 if context.get('mail.compose.message.mode') == 'mass_mail':
84 # use the original template values - to be rendered when actually sent
85 # by super.send_mail()
86 values = self.pool.get('email.template').read(cr, uid, template_id, self.fields_get_keys(cr, uid), context)
88 # render the mail as one-shot
89 values = self.pool.get('email.template').generate_email(cr, uid, template_id, res_id, context=context)
90 # retrofit generated attachments in the expected field format
91 if values['attachments']:
92 attachment = values.pop('attachments')
93 attachment_obj = self.pool.get('ir.attachment')
95 for fname, fcontent in attachment.iteritems():
98 'datas': base64.b64encode(fcontent),
100 'description': fname,
101 'res_model' : self._name,
102 'res_id' : ids[0] if ids else False
104 att_ids.append(attachment_obj.create(cr, uid, data_attach))
105 values['attachment_ids'] = att_ids
108 values = self.default_get(cr, uid, self.fields_get_keys(cr, uid), context)
109 values.update(use_template=use_template, template_id=template_id)
111 return {'value': values}
114 def template_toggle(self, cr, uid, ids, context=None):
115 for record in self.browse(cr, uid, ids, context=context):
116 had_template = record.use_template
117 record.write({'use_template': not(had_template)})
119 # equivalent to choosing an empty template
120 onchange_defaults = self.on_change_template(cr, uid, record.id, not(had_template),
121 False, email_from=record.email_from,
122 email_to=record.email_to, context=context)
123 record.write(onchange_defaults['value'])
124 return _reopen(self, record.id, record.model)
126 def save_as_template(self, cr, uid, ids, context=None):
129 email_template = self.pool.get('email.template')
130 model_pool = self.pool.get('ir.model')
131 for record in self.browse(cr, uid, ids, context=context):
132 model = record.model or context.get('active_model')
133 model_ids = model_pool.search(cr, uid, [('model', '=', model)])
134 model_id = model_ids and model_ids[0] or False
137 model_name = model_pool.browse(cr, uid, model_id, context=context).name
138 template_name = "%s: %s" % (model_name, tools.ustr(record.subject))
140 'name': template_name,
141 'email_from': record.email_from or False,
142 'subject': record.subject or False,
143 'body_text': record.body_text or False,
144 'email_to': record.email_to or False,
145 'email_cc': record.email_cc or False,
146 'email_bcc': record.email_bcc or False,
147 'reply_to': record.reply_to or False,
148 'model_id': model_id or False,
149 'attachment_ids': [(6, 0, [att.id for att in record.attachment_ids])]
151 template_id = email_template.create(cr, uid, values, context=context)
152 record.write({'template_id': template_id,
153 'use_template': True})
155 # _reopen same wizard screen with new template preselected
156 return _reopen(self, record.id, model)
158 # override the basic implementation
159 def render_template(self, cr, uid, template, model, res_id, context=None):
160 return self.pool.get('email.template').render_template(cr, uid, template, model, res_id, context=context)
162 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: