[FIX] error reporting in _.sprintf
[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 from osv import fields, osv
23 from tools.translate import _
24 import tools
25 import binascii
26
27
28 class project_tasks(osv.osv):
29     _name = "project.task"
30     _inherit = ['mail.thread','project.task']
31
32     _columns = {
33          'message_ids': fields.one2many('mail.message', 'res_id', 'Messages', domain=[('model','=',_name)], readonly=True),
34     }
35
36     def message_new(self, cr, uid, msg, custom_values=None, context=None):
37         res_id = super(project_tasks,self).message_new(cr, uid, msg, custom_values=custom_values, context=context)
38         subject = msg.get('subject')
39         body = msg.get('body_text')
40         msg_from = msg.get('from')
41         data = {
42             'name': subject,
43             'description': body,
44             'planned_hours': 0.0,
45         }
46         data.update(self.message_partner_by_email(cr, uid, msg_from))
47         self.write(cr, uid, [res_id], data, context)
48         return res_id
49
50     def message_update(self, cr, uid, ids, msg, data={}, default_act='pending'):
51         data.update({
52             'description': msg['body_text'],
53         })
54         act = 'do_'+default_act
55
56         maps = { 
57             'cost':'planned_hours',
58         }
59         for line in msg['body_text'].split('\n'):
60             line = line.strip()
61             res = tools.misc.command_re.match(line)
62             if res:
63                 match = res.group(1).lower()
64                 field = maps.get(match)
65                 if field:
66                     try:
67                         data[field] = float(res.group(2).lower())
68                     except (ValueError, TypeError):
69                         pass
70                 elif match.lower() == 'state' \
71                         and res.group(2).lower() in ['cancel','close','draft','open','pending']:
72                     act = 'do_%s' % res.group(2).lower()
73
74         self.write(cr, uid, ids, data, context=context)
75         getattr(self,act)(cr, uid, ids, context=context)
76         self.message_append_dict(cr, uid, [res_id], msg, context=context)
77         return True
78
79     def message_thread_followers(self, cr, uid, ids, context=None):
80         followers = super(project_tasks,self).message_thread_followers(cr, uid, ids, context=context)
81         for task in self.browse(cr, uid, followers.keys(), context=context):
82             task_followers = set(followers[task.id])
83             task_followers.add(task.user_id.user_email)
84             followers[task.id] = filter(None, task_followers)
85         return followers
86
87     def do_draft(self, cr, uid, ids, context=None):
88         res = super(project_tasks, self).do_draft(cr, uid, ids, context)
89         tasks = self.browse(cr, uid, ids, context=context)
90         self.message_append(cr, uid, tasks, _('Draft'), context=context)
91         return res
92
93     def do_open(self, cr, uid, ids, context=None):
94         res = super(project_tasks, self).do_open(cr, uid, ids, context)
95         tasks = self.browse(cr, uid, ids, context=context)
96         self.message_append(cr, uid, tasks, _('Open'), context=context)
97         return res
98
99     def do_pending(self, cr, uid, ids, context=None):
100         res = super(project_tasks, self).do_pending(cr, uid, ids, context)
101         tasks = self.browse(cr, uid, ids, context=context)
102         self.message_append(cr, uid, tasks, _('Pending'), context=context)
103         return res
104
105     def do_close(self, cr, uid, ids, context=None):
106         res = super(project_tasks, self).do_close(cr, uid, ids, context)
107         tasks = self.browse(cr, uid, ids, context=context)
108         for task in tasks:
109             if task.state == 'done':
110                 self.message_append(cr, uid, tasks, _('Done'), context=context)
111         return res
112
113     def do_cancel(self, cr, uid, ids, context=None):
114         res = super(project_tasks, self).do_cancel(cr, uid, ids, context=context)
115         tasks = self.browse(cr, uid, ids, context=context)
116         self.message_append(cr, uid, tasks, _('Cancel'), context=context)
117         return res
118
119 project_tasks()
120 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: