[FIX] crm: Avoid to return a None value in the rpc call back
[odoo/odoo.git] / addons / crm / crm_mailgate.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (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 Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import time
23 import re
24 import os
25
26 import binascii
27 import mx.DateTime
28 import base64
29
30 from tools.translate import _
31
32 import tools
33 from osv import fields,osv,orm
34 from osv.orm import except_orm
35
36 from tools import command_re
37
38 class crm_cases(osv.osv):
39     """ crm cases """
40
41     _name = "crm.case"
42     _inherit = "crm.case"    
43
44     def message_new(self, cr, uid, msg, context):
45         """
46         Automatically calls when new email message arrives
47         
48         @param self: The object pointer
49         @param cr: the current row, from the database cursor,
50         @param uid: the current user’s ID for security checks
51         """
52
53         mailgate_pool = self.pool.get('email.server.tools')
54
55         subject = msg.get('subject')
56         body = msg.get('body')
57         msg_from = msg.get('from')
58         priority = msg.get('priority')
59         
60         vals = {
61             'name': subject,
62             'email_from': msg_from,
63             'email_cc': msg.get('cc'),
64             'description': body,
65             'user_id': False,
66         }
67         if msg.get('priority', False):
68             vals['priority'] = priority
69         
70         res = mailgate_pool.get_partner(cr, uid, msg.get('from'))
71         if res:
72             vals.update(res)
73         res = self.create(cr, uid, vals, context)
74         cases = self.browse(cr, uid, [res])
75         self._history(cr, uid, cases, _('Receive'), history=True, details=body, email_from=msg_from, message_id=msg.get('id'))
76         
77         attachents = msg.get('attachments', [])
78         for attactment in attachents or []:
79             data_attach = {
80                 'name': attactment,
81                 'datas':binascii.b2a_base64(str(attachents.get(attactment))),
82                 'datas_fname': attactment,
83                 'description': 'Mail attachment',
84                 'res_model': self._name,
85                 'res_id': res,
86             }
87             self.pool.get('ir.attachment').create(cr, uid, data_attach)
88
89         return res
90
91     def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context={}):
92         """ 
93         @param self: The object pointer
94         @param cr: the current row, from the database cursor,
95         @param uid: the current user’s ID for security checks,
96         @param ids: List of update mail’s IDs 
97         """
98         
99         if isinstance(ids, (str, int, long)):
100             ids = [ids]
101         
102         msg_from = msg['from']
103         vals.update({
104             'description': msg['body']
105         })
106         if msg.get('priority', False):
107             vals['priority'] = msg.get('priority')
108
109         maps = {
110             'cost':'planned_cost',
111             'revenue': 'planned_revenue',
112             'probability':'probability'
113         }
114         vls = { }
115         for line in msg['body'].split('\n'):
116             line = line.strip()
117             res = command_re.match(line)
118             if res and maps.get(res.group(1).lower(), False):
119                 key = maps.get(res.group(1).lower())
120                 vls[key] = res.group(2).lower()
121         
122         vals.update(vls)
123         res = self.write(cr, uid, ids, vals)
124         cases = self.browse(cr, uid, ids)
125         message_id = context.get('references_id', False)
126         self._history(cr, uid, cases, _('Receive'), history=True, details=msg['body'], email_from=msg_from, message_id=message_id)        
127         #getattr(self, act)(cr, uid, select)
128         return res
129
130     def emails_get(self, cr, uid, ids, context={}):
131
132         """ 
133         Get Emails
134         @param self: The object pointer
135         @param cr: the current row, from the database cursor,
136         @param uid: the current user’s ID for security checks,
137         @param ids: List of email’s IDs
138         @param context: A standard dictionary for contextual values
139         """
140         res = []
141         if isinstance(ids, (str, int, long)):
142             select = [ids]
143         else:
144             select = ids
145         for case in self.browse(cr, uid, select):
146             user_email = (case.user_id and case.user_id.address_id and case.user_id.address_id.email) or False
147             res += [(user_email, case.email_from, case.email_cc or False, getattr(case,'priority') and case.priority or False)]
148         if isinstance(ids, (str, int, long)):
149             return len(res) and res[0] or False
150         return res
151
152     def msg_send(self, cr, uid, id, *args, **argv):
153
154         """ Send The Message
155             @param self: The object pointer
156             @param cr: the current row, from the database cursor,
157             @param uid: the current user’s ID for security checks,
158             @param ids: List of email’s IDs
159             @param *args: Return Tuple Value
160             @param **args: Return Dictionary of Keyword Value
161         """
162         return True
163
164 crm_cases()