1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from lxml import etree
24 from openerp import tools
25 from openerp.tools.translate import _
26 from openerp.osv import fields, osv
28 class project_task_delegate(osv.osv_memory):
29 _name = 'project.task.delegate'
30 _description = 'Task Delegate'
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")
43 def onchange_project_id(self, cr, uid, ids, project_id=False, context=None):
44 project_project = self.pool.get('project.project')
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}}
51 def default_get(self, cr, uid, fields, context=None):
53 This function gets default values
55 res = super(project_task_delegate, self).default_get(cr, uid, fields, context=context)
58 record_id = context and context.get('active_id', False) or False
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)
65 if 'project_id' in fields:
66 res['project_id'] = int(task.project_id.id) if task.project_id else False
69 if task_name.startswith(_('CHECK: ')):
70 newname = tools.ustr(task_name).replace(_('CHECK: '), '')
72 newname = tools.ustr(task_name or '')
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: '), '')
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
89 'planned_hours_me': 1.0,
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']:
101 eview = etree.fromstring(res['arch'])
102 def _check_rec(eview):
103 if eview.attrib.get('widget','') == 'float_time':
104 eview.set('widget','float')
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)
116 def delegate(self, cr, uid, ids, context=None):
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')
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
135 project_task_delegate()
137 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: