[FIX] crm: crash send new email wizard
[odoo/odoo.git] / addons / crm / wizard / wizard_crm_new_send_email.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
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 mx.DateTime import now
24
25 import wizard
26 import netsvc
27 import ir
28 import pooler
29 import tools
30 import base64
31 from tools.translate import _
32
33
34 email_send_form = '''<?xml version="1.0"?>
35 <form string="Mass Mailing">
36     <field name="to"/>
37     <newline/>
38     <field name="cc"/>
39     <newline/>
40     <field name="subject"/>
41     <newline/>
42     <field name="text" />
43     <newline/>
44     <field name="doc1" />
45     <newline/>
46     <field name="doc2" />
47     <newline/>
48     <field name="doc3" />
49     <separator colspan="4" string="Set case state to"/>
50     <newline/>
51     <field name="state" />
52 </form>'''
53
54 email_send_fields = {
55     'to': {'string':"To", 'type':'char', 'size':64, 'required':True},
56     'cc': {'string':"CC", 'type':'char', 'size':128,},
57     'subject': {'string':'Subject', 'type':'char', 'size':128, 'required':True},
58     'text': {'string':'Message', 'type':'text_tag', 'required':True},
59     'state':{'string':'State', 'type':'selection', 'selection':[('done','Done'),('pending','Pending'),('unchanged','Unchanged')]},
60     'doc1' :  {'string':"Attachment1", 'type':'binary'},
61     'doc2' :  {'string':"Attachment2", 'type':'binary'},
62     'doc3' :  {'string':"Attachment3", 'type':'binary'},
63     'state' :  {'string':"Set State to", 'type':'selection', 'required' : True, 'default' :'done',\
64                     'selection': [('unchanged','Unchanged'),('done','Done'),('pending','Pending')]},
65 }
66
67 # this sends an email to ALL the addresses of the selected partners.
68 def _mass_mail_send(self, cr, uid, data, context):
69     attach = filter(lambda x: x, [data['form']['doc1'],  data['form']['doc2'],  data['form']['doc3']])
70     attach = map(lambda x: x and ('Attachment'+str(attach.index(x)+1), base64.decodestring(x)), attach)
71
72     pool = pooler.get_pool(cr.dbname)
73     case_pool=pool.get(data.get('model'))
74
75     case = case_pool.browse(cr,uid,data['ids'])[0]    
76     emails = [data['form']['to']] + (data['form']['cc'] or '').split(',')
77     emails = filter(None, emails)
78     body = data['form']['text']
79     if case.user_id.signature:
80         body += '\n\n%s' % (case.user_id.signature)
81     case_pool._history(cr, uid, [case], _('Send'), history=True, email=data['form']['to'], details=body)
82     flag = tools.email_send(
83         case.user_id.address_id.email,
84         emails,
85         data['form']['subject'],
86         case_pool.format_body(body),
87         attach=attach,
88         reply_to=case.section_id.reply_to,
89         openobject_id=str(case.id), 
90         subtype="html"
91     )
92     if flag:
93         if data['form']['state'] == 'unchanged':
94             pass
95         elif data['form']['state'] == 'done':
96             case_pool.case_close(cr, uid, data['ids'])
97         elif data['form']['state'] == 'pending':
98             case_pool.case_pending(cr, uid, data['ids'])
99         cr.commit()
100         raise wizard.except_wizard(_('Email!'),("Email Successfully Sent"))
101     else:
102         raise wizard.except_wizard(_('Warning!'),_("Email not sent !"))
103     return {}
104
105 def _get_info(self, cr, uid, data, context):
106     if not data['id']:
107         return {}
108         
109     pool = pooler.get_pool(cr.dbname)
110     case = pool.get(data.get('model')).browse(cr, uid, data['id'])
111     if not case.user_id:
112         raise wizard.except_wizard(_('Error'),_('You must define a responsible user for this case in order to use this action!'))
113     if not case.user_id.address_id.email:
114         raise wizard.except_wizard(_('Warning!'),_("Please specify user's email address !"))
115         
116     return {
117         'to': case.email_from, 
118         'subject': case.name, 
119         'cc': case.email_cc or ''
120     }
121     
122 class wizard_send_mail(wizard.interface):
123     states = {
124         'init': {
125             'actions': [_get_info],
126             'result': {'type': 'form', 'arch': email_send_form, 'fields': email_send_fields, 'state':[('end','Cancel','gtk-cancel'), ('send','Send Email','gtk-go-forward')]}
127         },
128         'send': {
129             'actions': [_mass_mail_send],
130             'result': {'type': 'state', 'state':'end'}
131         }
132     }
133 wizard_send_mail('crm.new.send.mail')
134 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
135