Launchpad automatic translations 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 = ['mailgate.thread','project.task']
29     
30     _columns={
31                 'message_ids': fields.one2many('mailgate.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         mailgate_obj = self.pool.get('email.server.tools')
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 = mailgate_obj.get_partner(cr, uid, msg_from)
53         if res:
54             data.update(res)
55         res = self.create(cr, uid, data)    
56         
57         attachments = msg.get('attachments', [])
58         for attachment in attachments or []:
59             data_attach = {
60                 'name': attachment,
61                 'datas':binascii.b2a_base64(str(attachments.get(attachment))),
62                 'datas_fname': attachment,
63                 'description': 'Mail attachment',
64                 'res_model': self._name,
65                 'res_id': res,
66             }
67             self.pool.get('ir.attachment').create(cr, uid, data_attach)
68
69         return res           
70     
71     def message_update(self, cr, uid, id, msg, data={}, default_act='pending'): 
72         mailgate_obj = self.pool.get('email.server.tools')
73         msg_actions, body_data = mailgate_obj.msg_act_get(msg)           
74         data.update({
75             'description': body_data,            
76         })
77         act = 'do_'+default_act
78         if 'state' in msg_actions:
79             if msg_actions['state'] in ['draft','close','cancel','open','pending']:
80                 act = 'do_' + msg_actions['state']
81         
82         for k1,k2 in [('cost','planned_hours')]:
83             try:
84                 data[k2] = float(msg_actions[k1])
85             except:
86                 pass
87
88         if 'priority' in msg_actions:
89             if msg_actions['priority'] in ('1','2','3','4','5'):
90                 data['priority'] = msg_actions['priority']
91         
92         self.write(cr, uid, [id], data)
93         getattr(self,act)(cr, uid, [id])
94         return True
95
96     def message_followers(self, cr, uid, ids, context=None):
97         res = []
98         if isinstance(ids, (str, int, long)):
99             select = [ids]
100         else:
101             select = ids
102         for task in self.browse(cr, uid, select, context=context):
103             user_email = (task.user_id and task.user_id.address_id and task.user_id.address_id.email) or False
104             res += [(user_email, False, False, task.priority)]
105         if isinstance(ids, (str, int, long)):
106             return len(res) and res[0] or False
107         return res
108
109     def msg_send(self, cr, uid, id, *args, **argv):
110         return True
111     
112     def _history(self, cr, uid, cases, keyword, history=False, subject=None, email=False, details=None, email_from=False, message_id=False, attach=[], context=None):
113         mailgate_pool = self.pool.get('mailgate.thread')
114         return mailgate_pool.history(cr, uid, cases, keyword, history=history,\
115                                        subject=subject, email=email, \
116                                        details=details, email_from=email_from,\
117                                        message_id=message_id, attach=attach, \
118                                        context=context)
119         
120     def do_draft(self, cr, uid, ids, *args, **kwargs):
121         res = super(project_tasks, self).do_draft(cr, uid, ids, *args, **kwargs)
122         tasks = self.browse(cr, uid, ids)
123         self._history(cr, uid, tasks, _('Draft'))
124         return res
125     
126     def do_open(self, cr, uid, ids, *args, **kwargs):
127         res = super(project_tasks, self).do_open(cr, uid, ids, *args, **kwargs)
128         tasks = self.browse(cr, uid, ids)
129         self._history(cr, uid, tasks, _('Open'))
130         return res
131     
132     def do_pending(self, cr, uid, ids, *args, **kwargs):
133         res = super(project_tasks, self).do_pending(cr, uid, ids, *args, **kwargs)
134         tasks = self.browse(cr, uid, ids)
135         self._history(cr, uid, tasks, _('Pending'))
136         return res
137     
138     def do_close(self, cr, uid, ids, *args, **kwargs):
139         res = super(project_tasks, self).do_close(cr, uid, ids, *args, **kwargs)
140         tasks = self.browse(cr, uid, ids)
141         for task in tasks:
142             if task.state == 'done':
143                 self._history(cr, uid, tasks, _('Done'))
144         return res
145     
146     def do_cancel(self, cr, uid, ids, *args, **kwargs):
147         res = super(project_tasks, self).do_cancel(cr, uid, ids, *args, **kwargs)
148         tasks = self.browse(cr, uid, ids)
149         self._history(cr, uid, tasks, _('Cancel'))
150         return res
151
152 project_tasks()
153 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: