2010-03-22 10:33:41 +00:00
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved
# $Id$
#
# This program is free software: you can redistribute it and/or modify
2010-10-28 06:54:18 +00:00
# it under the terms of the GNU Affero General Public License as published by
2010-03-22 10:33:41 +00:00
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2010-10-28 06:54:18 +00:00
# GNU Affero General Public License for more details.
2010-03-22 10:33:41 +00:00
#
2010-10-28 06:54:18 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-03-22 10:33:41 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
2010-07-15 11:24:25 +00:00
from crm import crm
2010-03-22 10:33:41 +00:00
from osv import osv , fields
from tools . translate import _
import base64
2010-07-15 11:24:25 +00:00
import itertools
2010-03-22 10:33:41 +00:00
import tools
2010-07-15 12:46:23 +00:00
import re
2010-03-22 10:33:41 +00:00
2010-09-01 09:46:28 +00:00
AVAILABLE_STATES = crm . AVAILABLE_STATES + [ ( ' unchanged ' , ' Unchanged ' ) ]
2010-05-11 13:51:57 +00:00
class crm_send_new_email_attachment ( osv . osv_memory ) :
_name = ' crm.send.mail.attachment '
_columns = {
' binary ' : fields . binary ( ' Attachment ' , required = True ) ,
' name ' : fields . char ( ' Name ' , size = 128 , required = True ) ,
' wizard_id ' : fields . many2one ( ' crm.send.mail ' , ' Wizard ' , required = True ) ,
}
crm_send_new_email_attachment ( )
2010-07-14 10:10:31 +00:00
class crm_send_new_email ( osv . osv_memory ) :
2010-03-22 10:33:41 +00:00
""" Sends new email for the case """
_name = " crm.send.mail "
2010-04-07 07:03:45 +00:00
_description = " Send new email "
2010-03-22 10:33:41 +00:00
_columns = {
2010-07-02 16:31:01 +00:00
' email_to ' : fields . char ( ' To ' , size = 512 , required = True ) ,
' email_from ' : fields . char ( ' From ' , size = 128 , required = True ) ,
2010-07-14 10:10:31 +00:00
' reply_to ' : fields . char ( ' Reply To ' , size = 128 , required = True , help = " Reply-to of the Sales team defined on this case " ) ,
2010-07-19 08:41:44 +00:00
' email_cc ' : fields . char ( ' CC ' , size = 512 , help = " These addresses will receive a copy of this email. To modify the permanent CC list, edit the global CC field of this case " ) ,
2010-07-02 16:31:01 +00:00
' subject ' : fields . char ( ' Subject ' , size = 512 , required = True ) ,
2010-07-15 07:33:13 +00:00
' body ' : fields . text ( ' Message Body ' , required = True ) ,
2010-09-01 09:46:28 +00:00
' state ' : fields . selection ( AVAILABLE_STATES , string = ' Set New State To ' , required = True ) ,
2010-05-11 13:51:57 +00:00
' attachment_ids ' : fields . one2many ( ' crm.send.mail.attachment ' , ' wizard_id ' ) ,
2010-09-28 07:04:04 +00:00
' html ' : fields . boolean ( ' HTML formatting? ' , help = " Select this if you want to send email with HTML formatting. " ) ,
2010-04-07 07:03:45 +00:00
}
2010-03-22 10:33:41 +00:00
def action_send ( self , cr , uid , ids , context = None ) :
""" This sends an email to ALL the addresses of the selected partners.
"""
2010-05-03 12:30:48 +00:00
hist_obj = self . pool . get ( ' mailgate.message ' )
2010-04-07 07:03:45 +00:00
2010-11-23 07:05:05 +00:00
if context is None :
2010-03-22 10:33:41 +00:00
context = { }
2010-07-15 07:33:13 +00:00
if not context . get ( ' active_model ' ) :
2010-03-22 10:33:41 +00:00
raise osv . except_osv ( _ ( ' Error ' ) , _ ( ' Can not send mail! ' ) )
2010-07-15 07:33:13 +00:00
model = context . get ( ' active_model ' )
2010-03-22 10:33:41 +00:00
case_pool = self . pool . get ( model )
res_id = context and context . get ( ' active_id ' , False ) or False
2010-05-11 13:51:57 +00:00
for obj in self . browse ( cr , uid , ids , context = context ) :
attach = [
( x . name , base64 . decodestring ( x . binary ) ) for x in obj . attachment_ids
]
2010-09-28 07:04:04 +00:00
subtype = ' plain '
2010-05-11 13:51:57 +00:00
message_id = None
2010-07-01 10:46:31 +00:00
ref_id = None
2010-05-11 13:51:57 +00:00
2010-07-15 11:24:25 +00:00
case = case_pool . browse ( cr , uid , res_id , context = context )
2010-03-22 10:33:41 +00:00
if context . get ( ' mail ' , ' new ' ) == ' new ' :
2010-06-25 17:41:41 +00:00
if case . message_ids :
2010-05-03 12:30:48 +00:00
message_id = case . message_ids [ 0 ] . message_id
2010-07-15 11:24:25 +00:00
elif context . get ( ' mail ' ) == ' forward ' :
# extract attachements from case and emails according to mode
attachments = [ ]
attach_pool = self . pool . get ( ' ir.attachment ' )
direct_attachments = attach_pool . search ( cr , uid , [ ( ' res_model ' , ' = ' , ' crm.lead ' ) , ( ' res_id ' , ' = ' , res_id ) ] , context = context )
attachments + = attach_pool . browse ( cr , uid , direct_attachments , context = context )
if obj . history in [ ' latest ' , ' whole ' ] and case . message_ids :
msgs = case . message_ids
if obj . history == ' latest ' :
msgs = msgs [ : 1 ]
attachments . extend ( itertools . chain ( * [ m . attachment_ids for m in msgs ] ) )
attach_all = [ ( a . datas_fname or a . name , base64 . decodestring ( a . datas ) ) for a in attachments if a . datas ]
attach + = attach_all
2010-03-22 10:33:41 +00:00
else :
2010-07-15 11:24:25 +00:00
hist = hist_obj . browse ( cr , uid , res_id , context = context )
2010-04-15 15:42:20 +00:00
message_id = hist . message_id
2010-06-24 13:03:07 +00:00
model = hist . model
2010-07-15 07:33:13 +00:00
case_pool = self . pool . get ( model )
2010-06-25 08:04:10 +00:00
res_id = hist . res_id
2010-07-01 10:46:31 +00:00
ref_id = hist . ref_id
2010-07-15 11:24:25 +00:00
case = case_pool . browse ( cr , uid , res_id , context = context )
2010-08-19 16:28:22 +00:00
emails = re . findall ( r ' ([^ ,<@]+@[^> ,]+) ' , obj . email_to or ' ' )
2010-07-16 10:41:21 +00:00
email_cc = re . findall ( r ' ([^ ,<@]+@[^> ,]+) ' , obj . email_cc or ' ' )
2010-03-22 10:33:41 +00:00
emails = filter ( None , emails )
2010-07-15 07:33:13 +00:00
body = obj . body
2010-03-22 10:33:41 +00:00
2010-07-15 07:33:13 +00:00
body = body and tools . ustr ( body ) or ' '
2010-06-25 12:17:12 +00:00
email_from = getattr ( obj , ' email_from ' , False )
x_headers = { }
2010-04-27 13:57:44 +00:00
if message_id :
2010-04-27 14:33:18 +00:00
x_headers [ ' References ' ] = " %s " % ( message_id )
2010-09-28 07:04:04 +00:00
if obj . html :
subtype = ' html '
2010-05-18 05:32:14 +00:00
flag = tools . email_send (
email_from ,
emails ,
obj . subject ,
body ,
2010-06-24 13:03:07 +00:00
email_cc = email_cc ,
2010-05-18 05:32:14 +00:00
attach = attach ,
2010-09-28 07:04:04 +00:00
subtype = subtype ,
2010-07-14 10:10:31 +00:00
reply_to = obj . reply_to ,
2010-05-18 05:32:14 +00:00
openobject_id = str ( case . id ) ,
x_headers = x_headers
)
2010-06-25 17:41:41 +00:00
2010-09-28 07:04:04 +00:00
if not flag :
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( ' Unable to send mail. Please check SMTP is configured properly. ' ) )
2010-07-14 11:42:48 +00:00
2010-07-15 11:24:25 +00:00
msg_dict = { ' new ' : ' Send ' , ' reply ' : ' Reply ' , ' forward ' : ' Forward ' }
case_pool . history ( cr , uid , [ case ] , _ ( msg_dict [ context . get ( ' mail ' , ' new ' ) ] ) , history = True , \
2010-07-14 11:42:48 +00:00
email = obj . email_to , details = body , \
subject = obj . subject , email_from = email_from , \
2010-07-15 07:33:13 +00:00
email_cc = ' , ' . join ( email_cc ) , message_id = message_id , \
2010-07-14 11:42:48 +00:00
references = ref_id or message_id , attach = attach )
if obj . state == ' unchanged ' :
pass
elif obj . state == ' done ' :
case_pool . case_close ( cr , uid , [ case . id ] )
elif obj . state == ' draft ' :
case_pool . case_reset ( cr , uid , [ case . id ] )
elif obj . state in [ ' cancel ' , ' open ' , ' pending ' ] :
act = ' case_ ' + obj . state
getattr ( case_pool , act ) ( cr , uid , [ case . id ] )
2010-03-22 10:33:41 +00:00
return { }
def default_get ( self , cr , uid , fields , context = None ) :
"""
This function gets default values
"""
2010-11-23 07:05:05 +00:00
if context is None :
2010-03-22 10:33:41 +00:00
context = { }
2010-07-15 07:33:13 +00:00
if not context . get ( ' active_model ' ) :
2010-03-22 10:33:41 +00:00
raise osv . except_osv ( _ ( ' Error ' ) , _ ( ' Can not send mail! ' ) )
2010-07-14 10:10:31 +00:00
res = super ( crm_send_new_email , self ) . default_get ( cr , uid , fields , context = context )
2010-03-22 10:33:41 +00:00
if context . get ( ' mail ' ) == ' reply ' :
res . update ( self . get_reply_defaults ( cr , uid , fields , context = context ) )
return res
2010-07-15 07:33:13 +00:00
model = context . get ( ' active_model ' )
2010-03-22 10:33:41 +00:00
mod_obj = self . pool . get ( model )
res_id = context and context . get ( ' active_ids ' , [ ] ) or [ ]
2010-06-25 17:41:41 +00:00
user_obj = self . pool . get ( ' res.users ' )
user_mail_from = user_obj . _get_email_from ( cr , uid , [ uid ] , context = context ) [ uid ]
2010-11-19 13:48:01 +00:00
for case in mod_obj . browse ( cr , uid , res_id , context = context ) :
2010-03-30 10:40:29 +00:00
if ' email_to ' in fields :
2010-07-14 10:10:31 +00:00
res . update ( { ' email_to ' : case . email_from and tools . ustr ( case . email_from ) or ' ' } )
2010-03-30 10:40:29 +00:00
if ' email_from ' in fields :
2010-07-14 10:10:31 +00:00
res . update ( { ' email_from ' : user_mail_from and tools . ustr ( user_mail_from ) or ' ' } )
if ' reply_to ' in fields :
2010-08-10 11:42:18 +00:00
if hasattr ( case , ' section_id ' ) :
res . update ( { ' reply_to ' : case . section_id and case . section_id . reply_to or False } )
2010-03-22 10:33:41 +00:00
if ' subject ' in fields :
2010-06-30 11:25:27 +00:00
res . update ( { ' subject ' : tools . ustr ( context . get ( ' subject ' , case . name ) or ' ' ) } )
2010-03-30 10:40:29 +00:00
if ' email_cc ' in fields :
2010-06-30 18:11:48 +00:00
res . update ( { ' email_cc ' : tools . ustr ( case . email_cc or ' ' ) } )
2010-07-15 07:33:13 +00:00
if ' body ' in fields :
res . update ( { ' body ' : u ' \n ' + ( tools . ustr ( case . user_id . signature or ' ' ) ) } )
2010-03-22 10:33:41 +00:00
if ' state ' in fields :
2010-06-30 18:11:48 +00:00
res . update ( { ' state ' : u ' pending ' } )
2010-06-25 17:41:41 +00:00
2010-03-22 10:33:41 +00:00
return res
def get_reply_defaults ( self , cr , uid , fields , context = None ) :
"""
This function gets default values for reply mail
"""
2010-05-03 12:30:48 +00:00
hist_obj = self . pool . get ( ' mailgate.message ' )
2010-03-22 10:33:41 +00:00
res_ids = context and context . get ( ' active_ids ' , [ ] ) or [ ]
2010-04-09 14:35:44 +00:00
2010-06-25 17:41:41 +00:00
user_obj = self . pool . get ( ' res.users ' )
user_mail_from = user_obj . _get_email_from ( cr , uid , [ uid ] , context = context ) [ uid ]
2010-04-09 14:35:44 +00:00
include_original = context and context . get ( ' include_original ' , False ) or False
2010-03-22 10:33:41 +00:00
res = { }
2010-05-03 12:30:48 +00:00
for hist in hist_obj . browse ( cr , uid , res_ids , context = context ) :
2010-06-24 13:03:07 +00:00
model = hist . model
2010-05-11 15:40:23 +00:00
# In the case where the crm.case does not exist in the database
if not model :
return { }
2010-03-22 10:33:41 +00:00
model_pool = self . pool . get ( model )
2010-06-25 17:41:41 +00:00
res_id = hist . res_id
2010-05-21 10:26:00 +00:00
case = model_pool . browse ( cr , uid , res_id )
2010-03-30 13:34:13 +00:00
if ' email_to ' in fields :
2010-06-30 18:11:48 +00:00
res . update ( { ' email_to ' : case . email_from and tools . ustr ( case . email_from ) or False } )
2010-03-30 10:40:29 +00:00
if ' email_from ' in fields :
2010-06-30 18:11:48 +00:00
res . update ( { ' email_from ' : user_mail_from and tools . ustr ( user_mail_from ) or False } )
2010-04-09 14:35:44 +00:00
2010-07-01 19:38:16 +00:00
signature = u ' \n ' + ( tools . ustr ( case . user_id . signature or ' ' ) ) + u ' \n '
2010-05-12 08:20:55 +00:00
original = [ signature ]
2010-07-15 07:33:13 +00:00
if include_original == True and ' body ' in fields :
2010-06-30 18:11:48 +00:00
header = u ' -------- Original Message -------- '
sender = u ' From: %s ' % ( tools . ustr ( hist . email_from or ' ' ) )
to = u ' To: %s ' % ( tools . ustr ( hist . email_to or ' ' ) )
sentdate = u ' Date: %s ' % ( tools . ustr ( hist . date ) )
desc = u ' \n %s ' % ( tools . ustr ( hist . description ) )
2010-05-11 15:30:43 +00:00
2010-07-01 19:38:16 +00:00
original = [ signature , header , sender , to , sentdate , desc ]
2010-05-12 08:20:55 +00:00
2010-07-15 07:33:13 +00:00
res [ ' body ' ] = u ' \n ' + u ' \n ' . join ( original )
2010-05-12 08:20:55 +00:00
2010-03-22 10:33:41 +00:00
if ' subject ' in fields :
2010-06-30 18:11:48 +00:00
res . update ( { u ' subject ' : u ' Re: %s ' % ( tools . ustr ( hist . name or ' ' ) ) } )
2010-07-15 07:33:13 +00:00
if ' email_cc ' in fields :
2010-07-16 12:23:07 +00:00
email_cc = ( case . email_cc and tools . ustr ( case . email_cc ) + ' , ' or ' ' ) + ( hist . email_cc or ' ' )
res . update ( { ' email_cc ' : email_cc } )
2010-07-15 07:33:13 +00:00
if ' reply_to ' in fields :
2010-08-10 11:42:18 +00:00
if hasattr ( case , ' section_id ' ) :
2010-08-19 16:28:22 +00:00
res . update ( { ' reply_to ' : case . section_id . reply_to or ' ' } )
2010-03-31 07:35:18 +00:00
if ' state ' in fields :
2010-06-30 18:11:48 +00:00
res [ ' state ' ] = u ' pending '
2010-03-22 10:33:41 +00:00
return res
def view_init ( self , cr , uid , fields_list , context = None ) :
"""
This function checks for precondition before wizard executes
@param self : The object pointer
@param cr : the current row , from the database cursor ,
@param uid : the current user ’ s ID for security checks ,
@param fields : List of fields for default value
@param context : A standard dictionary for contextual values
"""
2010-11-23 07:05:05 +00:00
if context is None :
2010-03-22 10:33:41 +00:00
context = { }
2010-07-15 07:33:13 +00:00
if not context . get ( ' active_model ' ) :
2010-03-22 10:33:41 +00:00
raise osv . except_osv ( _ ( ' Error ' ) , _ ( ' Can not send mail! ' ) )
return True
2010-07-14 10:10:31 +00:00
crm_send_new_email ( )
2010-03-22 10:33:41 +00:00