[Merge]: merge with lp:~openerp-dev/openobject-addons/emails-framework-addons
[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 = 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         thread_obj = self.pool.get('email.thread')
73         msg_actions, body_data = thread_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 thread_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 _history(self, cr, uid, cases, keyword, history=False, subject=None, email=False, details=None, email_from=False, message_id=False, attach=[], context=None):
110         mailgate_pool = self.pool.get('email.thread')
111         return mailgate_pool.history(cr, uid, cases, keyword, history=history,\
112                                        subject=subject, email=email, \
113                                        details=details, email_from=email_from,\
114                                        message_id=message_id, attach=attach, \
115                                        context=context)
116
117     def do_draft(self, cr, uid, ids, *args, **kwargs):
118         res = super(project_tasks, self).do_draft(cr, uid, ids, *args, **kwargs)
119         tasks = self.browse(cr, uid, ids)
120         self._history(cr, uid, tasks, _('Draft'))
121         return res
122
123     def do_open(self, cr, uid, ids, *args, **kwargs):
124         res = super(project_tasks, self).do_open(cr, uid, ids, *args, **kwargs)
125         tasks = self.browse(cr, uid, ids)
126         self._history(cr, uid, tasks, _('Open'))
127         return res
128
129     def do_pending(self, cr, uid, ids, *args, **kwargs):
130         res = super(project_tasks, self).do_pending(cr, uid, ids, *args, **kwargs)
131         tasks = self.browse(cr, uid, ids)
132         self._history(cr, uid, tasks, _('Pending'))
133         return res
134
135     def do_close(self, cr, uid, ids, *args, **kwargs):
136         res = super(project_tasks, self).do_close(cr, uid, ids, *args, **kwargs)
137         tasks = self.browse(cr, uid, ids)
138         for task in tasks:
139             if task.state == 'done':
140                 self._history(cr, uid, tasks, _('Done'))
141         return res
142
143     def do_cancel(self, cr, uid, ids, *args, **kwargs):
144         res = super(project_tasks, self).do_cancel(cr, uid, ids, *args, **kwargs)
145         tasks = self.browse(cr, uid, ids)
146         self._history(cr, uid, tasks, _('Cancel'))
147         return res
148
149 project_tasks()
150 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: