738b1561119662e9eb1b86cebe4480b59a927cdb
[odoo/odoo.git] / addons / emails / wizard / email_compose_message.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
6 #
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.
11 #
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.
16 #
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/>
19 #
20 ##############################################################################
21
22 from osv import osv
23 from osv import fields
24 import tools
25
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'
30
31     def default_get(self, cr, uid, fields, context=None):
32         if context is None:
33             context = {}
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)
37         if message_id:
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
41
42             if 'attachment_ids' in fields:
43                 result['attachment_ids'] = message_data and message_pool.read(cr, uid, message_id, ['attachment_ids'])['attachment_ids']
44
45             if 'res_id' in fields:
46                 result['res_id'] = message_data and message_data.res_id
47
48             if 'email_from' in fields:
49                 result['email_from'] = message_data and message_data.email_from
50
51             if 'email_to' in fields:
52                 result['email_to'] = message_data and message_data.email_to
53
54             if 'email_cc' in fields:
55                 result['email_cc'] = message_data and message_data.email_cc
56
57             if 'email_bcc' in fields:
58                 result['email_bcc'] = message_data and message_data.email_bcc
59
60             if 'name' in fields:
61                 result['name']  = tools.ustr(message_data and message_data.name or '')
62                 if context.get('mail','') == 'reply':
63                     result['name'] = "Re :- " + result['name']
64
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])
74                 else:
75                     result['description'] = description
76
77             if 'reply_to' in fields:
78                 result['reply_to'] = message_data and message_data.reply_to
79
80             if 'model' in fields:
81                 result['model'] = message_data and message_data.model
82
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
85
86             if 'references' in fields:
87                 result['references'] = tools.ustr(message_data and message_data.references)
88
89             if 'sub_type' in fields:
90                 result['sub_type'] = message_data and message_data.sub_type
91
92             if 'headers' in fields:
93                 result['headers'] = message_data and message_data.headers
94
95             if 'priority' in fields:
96                 result['priority'] = message_data and message_data.priority
97
98             if 'debug' in fields:
99                 result['debug'] = message_data and message_data.debug
100
101         return result
102
103     _columns = {
104         'attachment_ids': fields.many2many('ir.attachment','email_message_send_attachment_rel', 'wizard_id', 'attachment_id', 'Attachments'),
105         'debug':fields.boolean('Debug', readonly=True),
106     }
107
108     def save_to_drafts(self, cr, uid, ids, context=None):
109         if context is None:
110             context = {}
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)
113         return {}
114
115     def send_mail(self, cr, uid, ids, context=None):
116         if context is None:
117             context = {}
118         email_id = self.save_to_mailbox(cr, uid, ids, context)
119         return {}
120
121     def save_to_mailbox(self, cr, uid, ids, context=None):
122         email_ids = []
123         email_message_pool = self.pool.get('email.message')
124         attachment = []
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)
133         return email_ids
134
135 email_compose_message()
136
137 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: