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 ##############################################################################
23 from osv import fields
26 class email_compose_message(osv.osv_memory):
27 _name = 'email.compose.message'
28 _inherit = 'email.message.template'
29 _description = 'This is the wizard for Compose E-mail'
31 def default_get(self, cr, uid, fields, context=None):
34 result = super(email_compose_message, self).default_get(cr, uid, fields, context=context)
35 message_pool = self.pool.get('email.message')
36 message_id = context.get('message_id', False)
38 message_data = message_pool.browse(cr, uid, int(message_id), context)
39 if 'message_id' in fields:
40 result['message_id'] = message_data and message_data.message_id
42 if 'attachment_ids' in fields:
43 result['attachment_ids'] = message_data and message_pool.read(cr, uid, message_id, ['attachment_ids'])['attachment_ids']
45 if 'res_id' in fields:
46 result['res_id'] = message_data and message_data.res_id
48 if 'email_from' in fields:
49 result['email_from'] = message_data and message_data.email_from
51 if 'email_to' in fields:
52 result['email_to'] = message_data and message_data.email_to
54 if 'email_cc' in fields:
55 result['email_cc'] = message_data and message_data.email_cc
57 if 'email_bcc' in fields:
58 result['email_bcc'] = message_data and message_data.email_bcc
61 result['name'] = tools.ustr(message_data and message_data.name or '')
62 if context.get('mail','') == 'reply':
63 result['name'] = "Re :- " + result['name']
65 if 'description' in fields:
66 description = message_data and message_data.description and message_data.description or ''
67 if context.get('mail','') == 'reply':
68 header = '-------- Original Message --------'
69 sender = 'From: %s' % tools.ustr(message_data.email_from or '')
70 email_to = 'To: %s' % tools.ustr(message_data.email_to or '')
71 sentdate = 'Date: %s' % message_data.date
72 desc = '\n > \t %s' % tools.ustr(description.replace('\n', "\n > \t") or '')
73 result['description'] = '\n'.join([header, sender, email_to, sentdate, desc])
75 result['description'] = description
77 if 'reply_to' in fields:
78 result['reply_to'] = message_data and message_data.reply_to
81 result['model'] = message_data and message_data.model
83 if 'user_id' in fields:
84 result['user_id'] = message_data and message_data.user_id and message_data.user_id.id or False
86 if 'references' in fields:
87 result['references'] = tools.ustr(message_data and message_data.references)
89 if 'sub_type' in fields:
90 result['sub_type'] = message_data and message_data.sub_type
92 if 'headers' in fields:
93 result['headers'] = message_data and message_data.headers
95 if 'priority' in fields:
96 result['priority'] = message_data and message_data.priority
99 result['debug'] = message_data and message_data.debug
104 'attachment_ids': fields.many2many('ir.attachment','email_message_send_attachment_rel', 'wizard_id', 'attachment_id', 'Attachments'),
105 'debug':fields.boolean('Debug', readonly=True),
108 def save_to_drafts(self, cr, uid, ids, context=None):
111 email_id = self.save_to_mailbox(cr, uid, ids, context=context)
112 self.pool.get('email.message').write(cr, uid, email_id, {'folder':'drafts', 'state': 'draft'}, context)
115 def send_mail(self, cr, uid, ids, context=None):
118 email_id = self.save_to_mailbox(cr, uid, ids, context)
121 def save_to_mailbox(self, cr, uid, ids, context=None):
123 email_message_pool = self.pool.get('email.message')
125 for mail in self.browse(cr, uid, ids, context=context):
126 for attach in mail.attachment_ids:
127 attachment.append((attach.datas_fname, attach.datas))
128 email_id = email_message_pool.email_send(cr, uid, mail.email_from, mail.email_to, mail.name, mail.description,
129 model=mail.model, email_cc=mail.email_cc, email_bcc=mail.email_bcc, reply_to=mail.reply_to,
130 attach=attachment, message_id=mail.message_id, openobject_id=mail.res_id, debug=mail.debug,
131 subtype=mail.sub_type, x_headers=mail.headers, priority=mail.priority, smtp_server_id=mail.smtp_server_id and mail.smtp_server_id.id, context=context)
132 email_ids.append(email_id)
135 email_compose_message()
137 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: