1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2009 Sharoon Thomas
6 # Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
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.
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.
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/>
21 ##############################################################################
23 from osv import osv, fields
25 from tools.translate import _
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'
32 def default_get(self, cr, uid, fields, context=None):
35 result = super(email_template_send_wizard, self).default_get(cr, uid, fields, context=context)
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):
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)
49 if 'user_signature' in fields:
50 result['user_signature'] = template.user_signature
52 if 'report_template' in fields:
53 result['report_template'] = template.report_template and template.report_template.id or False
55 if 'template_id' in fields:
56 result['template_id'] = template.id
58 if 'smtp_server_id' in fields:
59 result['smtp_server_id'] = template.smtp_server_id.id
61 if 'message_id' in fields:
62 result['message_id'] = template.message_id
64 if 'track_campaign_item' in fields:
65 result['track_campaign_item'] = template.track_campaign_item
67 if 'attachment_ids' in fields:
68 result['attachment_ids'] = template_pool.read(cr, uid, template.id, ['attachment_ids'])['attachment_ids']
70 if 'requested' in fields:
71 result['requested'] = len(context.get('src_rec_ids',''))
74 result['state'] = len(context.get('src_rec_ids','')) > 1 and 'multi' or 'single'
76 if 'model_id' in fields:
77 result['model_id'] = model_pool.search(cr, uid, [('model','=',context.get('src_model'))],context=context)[0]
79 if 'res_id' in fields:
80 result['res_id'] = context['active_id']
82 if 'email_to' in fields:
83 result['email_to'] = _get_template_value('email_to')
85 if 'email_cc' in fields:
86 result['email_cc'] = _get_template_value('email_cc')
88 if 'email_bcc' in fields:
89 result['email_bcc'] = _get_template_value('email_bcc')
91 if 'subject' in fields:
92 result['subject'] = _get_template_value('subject')
94 if 'description' in fields:
95 result['description'] = _get_template_value('description')
97 #if 'body_html' in fields:
98 # result['body_html'] = _get_template_value('body_html')
100 if 'reply_to' in fields:
101 result['reply_to'] = _get_template_value('reply_to')
103 if 'report_name' in fields:
104 result['report_name'] = _get_template_value('report_name')
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),
123 #def fields_get(self, cr, uid, fields=None, context=None, write_access=True):
124 # if context is None:
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'])]
131 def save_to_drafts(self, cr, uid, ids, context=None):
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'}
138 def send_mail(self, cr, uid, ids, context=None):
141 mailid = self.save_to_mailbox(cr, uid, ids, context)
142 return {'type': 'ir.actions.act_window_close'}
144 def get_generated(self, cr, uid, ids=None, context=None):
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)
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),
160 raise osv.except_osv(_("Email Template"),_("Email sending failed for one or more objects."))
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)
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)
177 email_template_send_wizard()
179 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: