[FIX] Chatter: fixed suggested recipients, when having no email, unchecking the box...
[odoo/odoo.git] / addons / project / wizard / project_task_delegate.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
24 from openerp import tools
25 from openerp.tools.translate import _
26 from openerp.osv import fields, osv
27
28 class project_task_delegate(osv.osv_memory):
29     _name = 'project.task.delegate'
30     _description = 'Task Delegate'
31
32     _columns = {
33         'name': fields.char('Delegated Title', size=64, required=True, help="New title of the task delegated to the user"),
34         'prefix': fields.char('Your Task Title', size=64, help="Title for your validation task"),
35         'project_id': fields.many2one('project.project', 'Project', help="User you want to delegate this task to"),
36         'user_id': fields.many2one('res.users', 'Assign To', required=True, help="User you want to delegate this task to"),
37         'new_task_description': fields.text('New Task Description', help="Reinclude the description of the task in the task of the user"),
38         'planned_hours': fields.float('Planned Hours',  help="Estimated time to close this task by the delegated user"),
39         'planned_hours_me': fields.float('Hours to Validate', help="Estimated time for you to validate the work done by the user to whom you delegate this task"),
40         'state': fields.selection([('pending','Pending'), ('done','Done'), ], 'Validation State', help="New state of your own task. Pending will be reopened automatically when the delegated task is closed")
41     }
42
43     def onchange_project_id(self, cr, uid, ids, project_id=False, context=None):
44         project_project = self.pool.get('project.project')
45         if not project_id:
46             return {'value':{'user_id': False}}
47         project = project_project.browse(cr, uid, project_id, context=context)
48         return {'value': {'user_id': project.user_id and project.user_id.id or False}}
49         
50
51     def default_get(self, cr, uid, fields, context=None):
52         """
53         This function gets default values
54         """
55         res = super(project_task_delegate, self).default_get(cr, uid, fields, context=context)
56         if context is None:
57             context = {}
58         record_id = context and context.get('active_id', False) or False
59         if not record_id:
60             return res
61         task_pool = self.pool.get('project.task')
62         task = task_pool.browse(cr, uid, record_id, context=context)
63         task_name =tools.ustr(task.name)
64
65         if 'project_id' in fields:
66             res['project_id'] = int(task.project_id.id) if task.project_id else False
67
68         if 'name' in fields:
69             if task_name.startswith(_('CHECK: ')):
70                 newname = tools.ustr(task_name).replace(_('CHECK: '), '')
71             else:
72                 newname = tools.ustr(task_name or '')
73             res['name'] = newname
74         if 'planned_hours' in fields:
75             res['planned_hours'] = task.remaining_hours or 0.0
76         if 'prefix' in fields:
77             if task_name.startswith(_('CHECK: ')):
78                 newname = tools.ustr(task_name).replace(_('CHECK: '), '')
79             else:
80                 newname = tools.ustr(task_name or '')
81             prefix = _('CHECK: %s') % newname
82             res['prefix'] = prefix
83         if 'new_task_description' in fields:
84             res['new_task_description'] = task.description
85         return res
86
87
88     _defaults = {
89        'planned_hours_me': 1.0,
90        'state': 'pending',
91     }
92
93     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
94         res = super(project_task_delegate, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu=submenu)
95         users_pool = self.pool.get('res.users')
96         obj_tm = users_pool.browse(cr, uid, uid, context=context).company_id.project_time_mode_id
97         tm = obj_tm and obj_tm.name or 'Hours'
98         if tm in ['Hours','Hour']:
99             return res
100
101         eview = etree.fromstring(res['arch'])
102         def _check_rec(eview):
103             if eview.attrib.get('widget','') == 'float_time':
104                 eview.set('widget','float')
105             for child in eview:
106                 _check_rec(child)
107             return True
108
109         _check_rec(eview)
110         res['arch'] = etree.tostring(eview)
111         for field in res['fields']:
112             if 'Hours' in res['fields'][field]['string']:
113                 res['fields'][field]['string'] = res['fields'][field]['string'].replace('Hours',tm)
114         return res
115
116     def delegate(self, cr, uid, ids, context=None):
117         if context is None:
118             context = {}
119         task_id = context.get('active_id', False)
120         task_pool = self.pool.get('project.task')
121         delegate_data = self.read(cr, uid, ids, context=context)[0]
122         delegated_tasks = task_pool.do_delegate(cr, uid, [task_id], delegate_data, context=context)
123         models_data = self.pool.get('ir.model.data')
124
125         action_model, action_id = models_data.get_object_reference(cr, uid, 'project', 'action_view_task')
126         view_model, task_view_form_id = models_data.get_object_reference(cr, uid, 'project', 'view_task_form2')
127         view_model, task_view_tree_id = models_data.get_object_reference(cr, uid, 'project', 'view_task_tree2')
128         action = self.pool.get(action_model).read(cr, uid, action_id, context=context)         
129         action['res_id'] = delegated_tasks[task_id]
130         action['view_id'] = False
131         action['views'] = [(task_view_form_id, 'form'), (task_view_tree_id, 'tree')]
132         action['help'] = False    
133         return action
134
135 project_task_delegate()
136
137 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: