2011-02-23 09:27:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from osv import osv
|
|
|
|
from osv import fields
|
2011-04-13 10:41:45 +00:00
|
|
|
from tools.translate import _
|
2011-04-20 11:54:30 +00:00
|
|
|
import binascii
|
2011-02-23 09:27:08 +00:00
|
|
|
|
|
|
|
class email_compose_message(osv.osv_memory):
|
|
|
|
_name = 'email.compose.message'
|
|
|
|
_inherit = 'email.compose.message'
|
2011-03-01 10:28:52 +00:00
|
|
|
|
2011-02-23 09:27:08 +00:00
|
|
|
def _get_templates(self, cr, uid, context=None):
|
|
|
|
"""
|
|
|
|
Return Email Template of particular Model.
|
|
|
|
"""
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
record_ids = []
|
|
|
|
email_temp_pool = self.pool.get('email.template')
|
|
|
|
model = False
|
2011-02-23 12:22:28 +00:00
|
|
|
if context.get('message_id'):
|
|
|
|
message_pool = self.pool.get('email.message')
|
|
|
|
message_data = message_pool.browse(cr, uid, int(context.get('message_id')), context)
|
|
|
|
model = message_data.model
|
2011-05-11 11:04:23 +00:00
|
|
|
elif context.get('active_model', False):
|
|
|
|
model = context.get('active_model')
|
2011-02-23 09:27:08 +00:00
|
|
|
if model:
|
2011-05-11 11:04:23 +00:00
|
|
|
record_ids = email_temp_pool.search(cr, uid, [('model', '=', model)])
|
2011-02-28 11:39:19 +00:00
|
|
|
return email_temp_pool.name_get(cr, uid, record_ids, context) + [(False,'')]
|
2011-02-23 09:27:08 +00:00
|
|
|
return []
|
|
|
|
|
|
|
|
_columns = {
|
|
|
|
'template_id': fields.selection(_get_templates, 'Template'),
|
|
|
|
}
|
|
|
|
|
2011-04-13 13:04:25 +00:00
|
|
|
def get_template_value(self, cr, uid, message, model, resource_id, context=None):
|
|
|
|
template_pool = self.pool.get('email.template')
|
|
|
|
return template_pool.get_template_value(cr, uid, message, model, resource_id, context)
|
|
|
|
|
2011-04-14 04:55:03 +00:00
|
|
|
def on_change_template(self, cr, uid, ids, template_id, context=None):
|
2011-02-23 09:27:08 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2011-04-20 11:54:30 +00:00
|
|
|
att_ids = []
|
2011-05-10 13:50:48 +00:00
|
|
|
values = {}
|
|
|
|
if template_id:
|
|
|
|
res_id = context.get('active_id', False)
|
|
|
|
values = self.pool.get('email.template').generate_email(cr, uid, template_id, res_id, context=context)
|
|
|
|
if values['attachment']:
|
|
|
|
attachment = values['attachment']
|
|
|
|
attachment_obj = self.pool.get('ir.attachment')
|
|
|
|
for fname, fcontent in attachment.items():
|
|
|
|
data_attach = {
|
|
|
|
'name': fname,
|
|
|
|
'datas': binascii.b2a_base64(str(fcontent)),
|
|
|
|
'datas_fname': fname,
|
|
|
|
'description': _('Mail attachment'),
|
|
|
|
'res_model' : self._name,
|
|
|
|
'res_id' : ids and ids[0] or False
|
|
|
|
}
|
|
|
|
att_ids.append(attachment_obj.create(cr, uid, data_attach))
|
|
|
|
values['attachment_ids'] = att_ids
|
2011-04-20 11:54:30 +00:00
|
|
|
return {'value': values}
|
2011-02-23 09:27:08 +00:00
|
|
|
|
2011-04-26 12:20:54 +00:00
|
|
|
def save_as_template(self, cr, uid, ids, context=None):
|
2011-04-26 11:54:45 +00:00
|
|
|
'''
|
|
|
|
create a new template record
|
|
|
|
'''
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
template_pool = self.pool.get('email.template')
|
|
|
|
model_pool = self.pool.get('ir.model')
|
|
|
|
for record in self.browse(cr, uid, ids, context=context):
|
2011-04-28 08:51:07 +00:00
|
|
|
model = context.get('active_model', record.model or False)
|
2011-05-11 11:04:23 +00:00
|
|
|
model = model_pool.search(cr, uid, [('model', '=', model)])[0]
|
2011-04-26 12:38:24 +00:00
|
|
|
model_name = model_pool.browse(cr, uid, model, context=context).name
|
2011-04-26 11:54:45 +00:00
|
|
|
values = {
|
2011-04-26 12:38:24 +00:00
|
|
|
'name': model_name,
|
2011-04-26 11:54:45 +00:00
|
|
|
'email_from': record.email_from or False,
|
|
|
|
'subject': record.subject or False,
|
|
|
|
'body': record.body or False,
|
|
|
|
'email_to': record.email_to or False,
|
|
|
|
'email_cc': record.email_cc or False,
|
|
|
|
'email_bcc': record.email_bcc or False,
|
|
|
|
'reply_to': record.reply_to or False,
|
|
|
|
'auto_delete': record.auto_delete,
|
|
|
|
'model_id': model or False,
|
|
|
|
'smtp_server_id': record.smtp_server_id.id or False,
|
|
|
|
'attachment_ids': [(6, 0, [att.id for att in record.attachment_ids])]
|
|
|
|
}
|
|
|
|
template_pool.create(cr, uid, values, context=context)
|
2011-04-27 10:35:36 +00:00
|
|
|
return {'type': 'ir.actions.act_window_close'}
|
2011-04-26 11:54:45 +00:00
|
|
|
|
2011-02-23 09:27:08 +00:00
|
|
|
email_compose_message()
|
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|