[IMP]: added crm_todo module
[odoo/odoo.git] / addons / project_gtd / project_gtd.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 import sys
23
24 from osv import fields, osv
25 import tools
26 from tools.translate import _
27
28 class project_gtd_context(osv.osv):
29     _name = "project.gtd.context"
30     _description = "Context"
31     _columns = {
32         'name': fields.char('Context', size=64, required=True, select=1, translate=1),
33         'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of contexts."),
34     }
35     _defaults = {
36         'sequence': 1
37     }
38     _order = "sequence, name"
39
40 project_gtd_context()
41
42
43 class project_gtd_timebox(osv.osv):
44     _name = "project.gtd.timebox"
45     _order = "sequence"
46     _columns = {
47         'name': fields.char('Timebox', size=64, required=True, select=1, translate=1),
48         'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of timebox."),
49         'icon': fields.selection(tools.icons, 'Icon', size=64),
50         'todo_id': fields.many2one('crm.lead','TODO'),
51     }
52
53 project_gtd_timebox()
54
55 class project_task(osv.osv):
56     _inherit = "project.task"
57     _columns = {
58         'timebox_id': fields.many2one('project.gtd.timebox', "Timebox",help="Time-laps during which task has to be treated"),
59         'context_id': fields.many2one('project.gtd.context', "Context",help="The context place where user has to treat task"),
60      }
61
62     def copy_data(self, cr, uid, id, default=None, context=None):
63         if context is None:
64             context = {}
65         if not default:
66             default = {}
67         default['timebox_id'] = False
68         default['context_id'] = False
69         return super(project_task,self).copy_data(cr, uid, id, default, context)
70
71     def _get_context(self,cr, uid, context=None):
72         ids = self.pool.get('project.gtd.context').search(cr, uid, [], context=context)
73         return ids and ids[0] or False
74
75     _defaults = {
76         'context_id': _get_context
77     }
78     def next_timebox(self, cr, uid, ids, *args):
79         timebox_obj = self.pool.get('project.gtd.timebox')
80         timebox_ids = timebox_obj.search(cr,uid,[])
81         if not timebox_ids: return True
82         for task in self.browse(cr,uid,ids):
83             timebox = task.timebox_id.id
84             if not timebox:
85                 self.write(cr, uid, task.id, {'timebox_id': timebox_ids[0]})
86             elif timebox_ids.index(timebox) != len(timebox_ids)-1:
87                 index = timebox_ids.index(timebox)
88                 self.write(cr, uid, task.id, {'timebox_id': timebox_ids[index+1]})
89         return True
90
91     def prev_timebox(self, cr, uid, ids, *args):
92         timebox_obj = self.pool.get('project.gtd.timebox')
93         timebox_ids = timebox_obj.search(cr,uid,[])
94         for task in self.browse(cr,uid,ids):
95             timebox = task.timebox_id.id
96             if timebox:
97                 if timebox_ids.index(timebox):
98                     index = timebox_ids.index(timebox)
99                     self.write(cr, uid, task.id, {'timebox_id': timebox_ids[index - 1]})
100                 else:
101                     self.write(cr, uid, task.id, {'timebox_id': False})
102         return True
103
104     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
105         res = super(project_task,self).fields_view_get(cr, uid, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
106         search_extended = False
107         timebox_obj = self.pool.get('project.gtd.timebox')
108         access_pool = self.pool.get('ir.model.access')
109         if (res['type'] == 'search') and access_pool.check_groups(cr, uid, "project_gtd.group_project_getting"):
110             tt = timebox_obj.browse(cr, uid, timebox_obj.search(cr,uid,[]), context=context)
111             search_extended ='''<newline/><group col="%d" expand="%d" string="%s">''' % (len(tt)+7,1,_('Getting Things Done'))
112             search_extended += '''<filter domain="[('timebox_id','=', False)]" context="{'set_editable':True,'set_visible':True,'gtd_visible':True,'user_invisible':True}" icon="gtk-new" help="Undefined Timebox" string="%s" />''' % (_('Inbox'),)
113             search_extended += '''<filter context="{'set_editable':True,'set_visible':True,'gtd_visible':True,'user_invisible':True}" icon="gtk-new" help="Getting things done" string="%s"/>''' % (_('GTD'),)
114             search_extended += '''<separator orientation="vertical"/>'''
115             for time in tt:
116                 if time.icon:
117                     icon = time.icon
118                 else :
119                     icon=""
120                 search_extended += '''<filter domain="[('timebox_id','=', ''' + str(time.id) + ''')]" icon="''' + icon + '''" string="''' + time.name + '''" context="{'gtd_visible':True, 'user_invisible': True}"/>'''
121             search_extended += '''
122             <separator orientation="vertical"/>
123             <field name="context_id" select="1" widget="selection"/>
124             </group>
125             </search> '''
126         if search_extended:
127             res['arch'] = unicode(res['arch'], 'utf8').replace('</search>', search_extended)
128             attrs_sel = self.pool.get('project.gtd.context').name_search(cr, uid, '', [], context=context)
129             context_id_info = self.pool.get('project.task').fields_get(cr, uid, ['context_id'], context=context)
130             context_id_info['context_id']['selection'] = attrs_sel
131             res['fields'].update(context_id_info)
132         return res
133
134 project_task()
135
136 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: