[FIX]: changes in message_update()
[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 binascii
25
26 class project_tasks(osv.osv):
27     _name = "project.task"
28     _inherit = ['email.thread','project.task']
29
30     _columns={
31                 'message_ids': fields.one2many('email.message', 'res_id', 'Messages', domain=[('model','=',_name)], readonly=True),
32               }
33     def message_new(self, cr, uid, msg, context=None):
34 #        """
35 #        Automatically calls when new email message arrives
36 #
37 #        @param self: The object pointer
38 #        @param cr: the current row, from the database cursor,
39 #        @param uid: the current user’s ID for security checks
40 #        """
41         thread_obj = self.pool.get('email.thread')
42         subject = msg.get('subject')
43         body = msg.get('body')
44         msg_from = msg.get('from')
45         priority = msg.get('priority')
46
47         data = {
48             'name': subject,
49             'description': body,
50             'planned_hours' : 0.0,
51         }
52         res = thread_obj.get_partner(cr, uid, msg_from)
53         if res:
54             data.update(res)
55         res_id = self.create(cr, uid, vals, context)
56
57         attachments = msg.get('attachments', {})
58         self.history(cr, uid, [res_id], _('receive'), history=True,
59                             subject = msg.get('subject'),
60                             email = msg.get('to'),
61                             details = msg.get('body'),
62                             email_from = msg.get('from'),
63                             email_cc = msg.get('cc'),
64                             message_id = msg.get('message-id'),
65                             references = msg.get('references', False) or msg.get('in-reply-to', False),
66                             attach = attachments,
67                             email_date = msg.get('date'),
68                             context = context)
69
70         return res_id
71
72     def message_update(self, cr, uid, ids, msg, data={}, default_act='pending'):
73         thread_obj = self.pool.get('email.thread')
74         msg_actions, body_data = thread_obj.msg_act_get(msg)
75         data.update({
76             'description': body_data,
77         })
78         act = 'do_'+default_act
79         if 'state' in msg_actions:
80             if msg_actions['state'] in ['draft','close','cancel','open','pending']:
81                 act = 'do_' + msg_actions['state']
82
83         for k1,k2 in [('cost','planned_hours')]:
84             try:
85                 data[k2] = float(msg_actions[k1])
86             except:
87                 pass
88
89         if 'priority' in msg_actions:
90             if msg_actions['priority'] in ('1','2','3','4','5'):
91                 data['priority'] = msg_actions['priority']
92
93         self.write(cr, uid, ids, data)
94         getattr(self,act)(cr, uid, ids)
95
96         attachments = msg.get('attachments', {})
97         self.history(cr, uid, ids, _('receive'), history=True,
98                             subject = msg.get('subject'),
99                             email = msg.get('to'),
100                             details = msg.get('body'),
101                             email_from = msg.get('from'),
102                             email_cc = msg.get('cc'),
103                             message_id = msg.get('message-id'),
104                             references = msg.get('references', False) or msg.get('in-reply-to', False),
105                             attach = attachments,
106                             email_date = msg.get('date'),
107                             context = context)
108         return True
109
110     def thread_followers(self, cr, uid, ids, context=None):
111         res = []
112         if isinstance(ids, (str, int, long)):
113             select = [ids]
114         else:
115             select = ids
116         for task in self.browse(cr, uid, select, context=context):
117             user_email = (task.user_id and task.user_id.address_id and task.user_id.address_id.email) or False
118             res += [(user_email, False, False, task.priority)]
119         if isinstance(ids, (str, int, long)):
120             return len(res) and res[0] or False
121         return res
122
123     def _history(self, cr, uid, cases, keyword, history=False, subject=None, email=False, details=None, email_from=False, message_id=False, attach=[], context=None):
124         thread_pool = self.pool.get('email.thread')
125         return thread_pool.history(cr, uid, cases, keyword, history=history,\
126                                        subject=subject, email=email, \
127                                        details=details, email_from=email_from,\
128                                        message_id=message_id, attach=attach, \
129                                        context=context)
130
131     def do_draft(self, cr, uid, ids, *args, **kwargs):
132         res = super(project_tasks, self).do_draft(cr, uid, ids, *args, **kwargs)
133         tasks = self.browse(cr, uid, ids)
134         self._history(cr, uid, tasks, _('Draft'))
135         return res
136
137     def do_open(self, cr, uid, ids, *args, **kwargs):
138         res = super(project_tasks, self).do_open(cr, uid, ids, *args, **kwargs)
139         tasks = self.browse(cr, uid, ids)
140         self._history(cr, uid, tasks, _('Open'))
141         return res
142
143     def do_pending(self, cr, uid, ids, *args, **kwargs):
144         res = super(project_tasks, self).do_pending(cr, uid, ids, *args, **kwargs)
145         tasks = self.browse(cr, uid, ids)
146         self._history(cr, uid, tasks, _('Pending'))
147         return res
148
149     def do_close(self, cr, uid, ids, *args, **kwargs):
150         res = super(project_tasks, self).do_close(cr, uid, ids, *args, **kwargs)
151         tasks = self.browse(cr, uid, ids)
152         for task in tasks:
153             if task.state == 'done':
154                 self._history(cr, uid, tasks, _('Done'))
155         return res
156
157     def do_cancel(self, cr, uid, ids, *args, **kwargs):
158         res = super(project_tasks, self).do_cancel(cr, uid, ids, *args, **kwargs)
159         tasks = self.browse(cr, uid, ids)
160         self._history(cr, uid, tasks, _('Cancel'))
161         return res
162
163 project_tasks()
164 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: