[MERGE] Merged with server/trunk.
[odoo/odoo.git] / addons / project_mailgate / project_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 binascii
23 from osv import fields, osv
24 from tools.translate import _
25 import tools
26
27 class project_tasks(osv.osv):
28     _inherit = 'project.task'
29
30     def message_new(self, cr, uid, msg, custom_values=None, context=None):
31         """ Overrides mail_thread message_new that is called by the mailgateway
32             through message_process.
33             This override updates the document according to the email.
34         """
35         if custom_values is None: custom_values = {}
36         custom_values.update({
37             'name': subject,
38             'planned_hours': 0.0,
39             'subject': msg.get('subject'),
40         })
41         custom_values.update(self.message_partner_by_email(cr, uid, msg.get('from', False), context=context))
42         return super(project_tasks,self).message_new(cr, uid, msg, custom_values=custom_values, context=context)
43
44     def message_update(self, cr, uid, ids, msg, update_vals=None, context=None):
45         """ Overrides mail_thread message_update that is called by the mailgateway
46             through message_process.
47             This method updates the task according to the email.
48         """
49         if update_vals is None: update_vals = {}
50         act = False
51         maps = {
52             'cost':'planned_hours',
53         }
54         for line in msg['body_text'].split('\n'):
55             line = line.strip()
56             res = tools.misc.command_re.match(line)
57             if res:
58                 match = res.group(1).lower()
59                 field = maps.get(match)
60                 if field:
61                     try:
62                         update_vals[field] = float(res.group(2).lower())
63                     except (ValueError, TypeError):
64                         pass
65                 elif match.lower() == 'state' \
66                         and res.group(2).lower() in ['cancel','close','draft','open','pending']:
67                     act = 'do_%s' % res.group(2).lower()
68         if act:
69             getattr(self,act)(cr, uid, ids, context=context)
70         return super(project_tasks,self).message_update(cr, uid, msg, update_vals=update_vals, context=context)
71
72     def message_thread_followers(self, cr, uid, ids, context=None):
73         followers = super(project_tasks,self).message_thread_followers(cr, uid, ids, context=context)
74         for task in self.browse(cr, uid, followers.keys(), context=context):
75             task_followers = set(followers[task.id])
76             task_followers.add(task.user_id.user_email)
77             followers[task.id] = filter(None, task_followers)
78         return followers
79
80
81 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: