6bf48bb4f42280b0ccba6c20f9fb001e483299cb
[odoo/odoo.git] / addons / email_template / wizard / email_template_send_wizard.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2009 Sharoon Thomas
6 #    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>
20 #
21 ##############################################################################
22
23 from osv import osv, fields
24 import netsvc
25 from tools.translate import _
26
27 class email_template_send_wizard(osv.osv_memory):
28     _name = 'email_template.send.wizard'
29     _inherit = 'email.template'
30     _description = 'This is the wizard for sending mail'
31
32     def default_get(self, cr, uid, fields, context=None):
33         if context is None:
34             context = {}
35         result = super(email_template_send_wizard, self).default_get(cr, uid, fields, context=context)
36
37         template_pool = self.pool.get('email.template')
38         model_pool = self.pool.get('ir.model')
39         template_id=context.get('template_id', False)
40         template = template_pool.get_email_template(cr, uid, template_id=template_id, context=context)
41         def _get_template_value(field):
42             if not template:
43                 return False
44             if len(context['src_rec_ids']) > 1: # Multiple Mail: Gets original template values for multiple email change
45                 return getattr(template, field)
46             else: # Simple Mail: Gets computed template values
47                 return self.get_template_value(cr, uid, getattr(template, field), template.model, context.get('active_id'), context)
48
49         if 'user_signature' in fields:
50             result['user_signature'] = template.user_signature
51
52         if 'report_template' in fields:
53             result['report_template'] = template.report_template and template.report_template.id or False
54
55         if 'template_id' in fields:
56             result['template_id'] = template.id
57
58         if 'smtp_server_id' in fields:
59             result['smtp_server_id'] = template.smtp_server_id.id
60
61         if 'message_id' in fields:
62             result['message_id'] = template.message_id
63
64         if 'track_campaign_item' in fields:
65             result['track_campaign_item'] = template.track_campaign_item
66
67         if 'attachment_ids' in fields:
68             result['attachment_ids'] = template_pool.read(cr, uid, template.id, ['attachment_ids'])['attachment_ids']
69
70         if 'requested' in fields:
71             result['requested'] = len(context.get('src_rec_ids',''))
72
73         if 'state' in fields:
74             result['state'] =  len(context.get('src_rec_ids','')) > 1 and 'multi' or 'single'
75
76         if 'model_id' in fields:
77             result['model_id'] = model_pool.search(cr, uid, [('model','=',context.get('src_model'))],context=context)[0]
78
79         if 'res_id' in fields:
80             result['res_id'] = context['active_id']
81
82         if 'email_to' in fields:
83             result['email_to'] = _get_template_value('email_to')
84
85         if 'email_cc' in fields:
86             result['email_cc'] = _get_template_value('email_cc')
87
88         if 'email_bcc' in fields:
89             result['email_bcc'] = _get_template_value('email_bcc')
90
91         if 'subject' in fields:
92             result['subject'] = _get_template_value('subject')
93
94         if 'description' in fields:
95             result['description'] = _get_template_value('description')
96
97         #if 'body_html' in fields:
98         #    result['body_html'] = _get_template_value('body_html')
99
100         if 'reply_to' in fields:
101             result['reply_to'] = _get_template_value('reply_to')
102
103         if 'report_name' in fields:
104             result['report_name'] = _get_template_value('report_name')
105
106         return result
107
108     _columns = {
109         'requested':fields.integer('No of requested Mails',readonly=True),
110         'generated':fields.integer('No of generated Mails',readonly=True),
111         'full_success':fields.boolean('Complete Success',readonly=True),
112         'attachment_ids': fields.many2many('ir.attachment','send_wizard_attachment_rel', 'wizard_id', 'attachment_id', 'Attachments'),
113         'state':fields.selection([
114                         ('single','Simple Mail Wizard Step 1'),
115                         ('multi','Multiple Mail Wizard Step 1'),
116                         ('done','Wizard Complete')
117                                   ],'State',readonly=True),
118     }
119
120     _defaults = {
121     }
122
123     #def fields_get(self, cr, uid, fields=None, context=None, write_access=True):
124     #    if context is None:
125     #        context = {}
126     #    result = super(email_template_send_wizard, self).fields_get(cr, uid, fields, context, write_access)
127     #    if 'attachment_ids' in result and 'src_model' in context:
128     #        result['attachment_ids']['domain'] = [('res_model','=',context['src_model']),('res_id','=',context['active_id'])]
129     #    return result
130
131     def save_to_drafts(self, cr, uid, ids, context=None):
132         if context is None:
133             context = {}
134         mailid = self.save_to_mailbox(cr, uid, ids, context=context)
135         self.pool.get('email.message').write(cr, uid, mailid, {'state': 'outgoing'}, context)
136         return {'type': 'ir.actions.act_window_close'}
137
138     def send_mail(self, cr, uid, ids, context=None):
139         if context is None:
140             context = {}
141         mailid = self.save_to_mailbox(cr, uid, ids, context)
142         return {'type': 'ir.actions.act_window_close'}
143
144     def get_generated(self, cr, uid, ids=None, context=None):
145         if ids is None:
146             ids = []
147         if context is None:
148             context = {}
149         logger = netsvc.Logger()
150         if context.get('src_rec_ids'):
151             #Means there are multiple items selected for email.
152             mail_ids = self.save_to_mailbox(cr, uid, ids, context)
153             if mail_ids:
154                 logger.notifyChannel("email-template", netsvc.LOG_INFO, _("Emails for multiple items saved in outbox."))
155                 self.write(cr, uid, ids, {
156                     'generated':len(mail_ids),
157                     'state':'done'
158                 }, context)
159             else:
160                 raise osv.except_osv(_("Email Template"),_("Email sending failed for one or more objects."))
161         return True
162
163     def save_to_mailbox(self, cr, uid, ids, context=None):
164         #def get_end_value(id, value):
165         #    if len(context['src_rec_ids']) > 1: # Multiple Mail: Gets value from the template
166         #        return self.get_value(cr, uid, template, value, context, id)
167         #    else:
168         #        return value
169
170         email_ids = []
171         for template in self.browse(cr, uid, ids, context=context):
172             for record_id in context.get('src_rec_ids',[]):
173                 email_id = self.generate_email(cr, uid, template.id, record_id, context)
174                 email_ids.append(email_id)
175         return email_ids
176
177 email_template_send_wizard()
178
179 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: