[BUG/FIX] project : return action close from web compability
[odoo/odoo.git] / addons / project / wizard / project_task_reevaluate.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 lxml import etree
23 from osv import fields, osv
24 from tools.translate import _
25
26 class project_task_reevaluate(osv.osv_memory):
27     _name = 'project.task.reevaluate'
28
29     def _get_remaining(self,cr, uid, context=None):
30         if context is None:
31             context = {}
32         active_id = context.get('active_id', False)
33         res = False
34         if active_id:
35             res = self.pool.get('project.task').browse(cr, uid, active_id, context=context).remaining_hours
36         return res
37
38     _columns = {
39         'remaining_hours' : fields.float('Remaining Hours', digits=(16,2), help="Put here the remaining hours required to close the task."),
40     }
41
42     _defaults = {
43         'remaining_hours': _get_remaining,
44     }
45
46     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
47         res = super(project_task_reevaluate, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu=submenu)
48         users_pool = self.pool.get('res.users')
49         time_mode = users_pool.browse(cr, uid, uid, context).company_id.project_time_mode_id
50         time_mode_name = time_mode and time_mode.name or 'Hours'
51         if time_mode_name in ['Hours','Hour']:
52             return res
53
54         eview = etree.fromstring(res['arch'])
55
56         def _check_rec(eview):
57             if eview.attrib.get('widget','') == 'float_time':
58                 eview.set('widget','float')
59             for child in eview:
60                 _check_rec(child)
61             return True
62
63         _check_rec(eview)
64
65         res['arch'] = etree.tostring(eview)
66
67         for field in res['fields']:
68             if 'Hours' in res['fields'][field]['string']:
69                 res['fields'][field]['string'] = res['fields'][field]['string'].replace('Hours',time_mode_name)
70         return res
71
72     def compute_hours(self, cr, uid, ids, context=None):
73         if context is None:
74             context = {}
75         data = self.browse(cr, uid, ids, context=context)[0]
76         task_pool = self.pool.get('project.task')
77         task_id = context.get('active_id', False)
78         if task_id:
79             task_pool.write(cr, uid, task_id, {'remaining_hours': data.remaining_hours})
80             if context.get('button_reactivate', False):
81                 task_pool.do_reopen(cr, uid, [task_id], context=context)
82         return {'type': 'ir.actions.act_window_close'}
83 project_task_reevaluate()