[IMP] Update the copyright to 2009
[odoo/odoo.git] / addons / project_gtd / project_gtd.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from xml import dom
24
25
26 from mx import DateTime
27 from mx.DateTime import now
28 import time
29
30 import netsvc
31 from osv import fields, osv
32 import ir
33
34
35 class one2many_mod(fields.one2many):
36     def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
37         if not context:
38             context = {}
39         res = {}
40         num = name[4]
41         for obj in obj.browse(cr, user, ids, context=context):
42             res[obj.id] = []
43             v = getattr(obj,'context'+num+'_id').id
44             if v:
45                 ids2 = obj.pool.get(self._obj).search(cr, user, [(self._fields_id,'=',obj.id),('context_id','=',v)], limit=self._limit)
46                 for r in obj.pool.get(self._obj)._read_flat(cr, user, ids2, [self._fields_id], context=context, load='_classic_write'):
47                     res[r[self._fields_id]].append( r['id'] )
48         return res
49
50 class project_gtd_context(osv.osv):
51     _name = "project.gtd.context"
52     _columns = {
53         'name': fields.char('Context', size=64, required=True, select=1),
54         'sequence': fields.integer('Sequence'),
55         'project_default_id': fields.many2one('project.project', 'Default Project', required=True),
56     }
57     _defaults = {
58         'sequence': lambda *args: 1
59     }
60     _order = "sequence, name"
61 project_gtd_context()
62
63
64 class project_gtd_timebox(osv.osv):
65     _name = "project.gtd.timebox"
66     _columns = {
67         'name': fields.char('Timebox', size=64, required=True, select=1),
68         'user_id': fields.many2one('res.users', 'User', required=True, select=1),
69         'child_ids': fields.one2many('project.gtd.timebox', 'parent_id', 'Childs Timebox'),
70         'parent_id': fields.many2one('project.gtd.timebox', 'Parent Timebox'),
71         'task_ids': fields.one2many('project.task', 'timebox_id', 'Tasks'),
72         'type': fields.selection([('daily','Daily'),('weekly','Weekly'),('monthly','Monthly'),('other','Other')], 'Type', required=True),
73         'task1_ids': one2many_mod('project.task', 'timebox_id', 'Tasks'),
74         'task2_ids': one2many_mod('project.task', 'timebox_id', 'Tasks'),
75         'task3_ids': one2many_mod('project.task', 'timebox_id', 'Tasks'),
76         'task4_ids': one2many_mod('project.task', 'timebox_id', 'Tasks'),
77         'task5_ids': one2many_mod('project.task', 'timebox_id', 'Tasks'),
78         'task6_ids': one2many_mod('project.task', 'timebox_id', 'Tasks'),
79         'context1_id': fields.many2one('project.gtd.context', 'Context 1', required=True),
80         'context2_id': fields.many2one('project.gtd.context', 'Context 2'),
81         'context3_id': fields.many2one('project.gtd.context', 'Context 3'),
82         'context4_id': fields.many2one('project.gtd.context', 'Context 4'),
83         'context5_id': fields.many2one('project.gtd.context', 'Context 5'),
84         'context6_id': fields.many2one('project.gtd.context', 'Context 6'),
85         'col_project': fields.boolean('Project'),
86         'col_date_start': fields.boolean('Date Start'),
87         'col_priority': fields.boolean('Priority'),
88         'col_deadline': fields.boolean('Deadline'),
89         'col_planned_hours': fields.boolean('Planned Hours'),
90         'col_effective_hours': fields.boolean('Effective Hours'),
91     }
92     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False):
93         res = super(project_gtd_timebox,self).fields_view_get(cr, uid, view_id, view_type, context, toolbar)
94         if (res['type']=='form') and ('record_id' in context):
95             if context['record_id']:
96                 rec = self.browse(cr, uid, int(context['record_id']), context)
97             else:
98                 iids = self.search(cr,uid, [('user_id','=',uid),('parent_id','=',False)], context=context)
99                 if len(iids):
100                     rec = self.browse(cr, uid, int(iids[0]), context=context)
101                 else:
102                     return res
103             res['arch'] = """
104     <form string="Daily Timebox">
105         <field name="name" readonly="1"/>
106         <notebook position="top">
107             """
108             for i in range(1,7):
109                 if not getattr(rec, 'context%d_id'%i):
110                     continue
111                 res['arch']+= """
112             <page string="%s">
113                 <field name="%s" colspan="4" nolabel="1">
114                     <tree editable="bottom" colors="grey:state in ('done','pending');red:state=='cancelled'" string="Tasks">
115                         <field name="name"/>
116                 """ % (getattr(rec, 'context%d_id'%(i,)).name, 'task%d_ids'%(i,))
117                 if rec.col_project:
118                     res['arch'] += '<field name="project_id" required="1"/>\n'
119                 if rec.col_date_start:
120                     res['arch'] += '<field name="date_start"/>\n'
121                 if rec.col_priority:
122                     res['arch'] += '<field name="priority"/>\n'
123                 if rec.col_deadline:
124                     res['arch'] += '<field name="date_deadline"/>\n'
125                 if rec.col_planned_hours:
126                     res['arch'] += '<field name="planned_hours"  widget="float_time" sum="Est. Hours"/>\n'
127                 if rec.col_effective_hours:
128                     res['arch'] += '<field name="effective_hours"  widget="float_time" sum="Eff. Hours"/>\n'
129                 res['arch'] += """
130                         <field name="state" readonly="1"/>
131                     </tree>
132                 </field>
133             </page>
134                 """
135
136             res['arch']+="""
137         </notebook>
138     </form>
139             """
140         doc = dom.minidom.parseString(res['arch'])
141         xarch, xfields = self._view_look_dom_arch(cr, uid, doc, context=context)
142         res['arch'] = xarch
143         res['fields'] = xfields
144         return res
145     _defaults = {
146         'type': lambda *args: 'daily',
147         'col_project': lambda *args: True,
148         'col_date_start': lambda *args: True,
149         'col_priority': lambda *args: True,
150         'col_deadline': lambda *args: False,
151         'col_planned_hours': lambda *args: True,
152         'col_effective_hours': lambda *args: False
153     }
154 project_gtd_timebox()
155
156 class project_task(osv.osv):
157     _inherit = "project.task"
158     _columns = {
159         'timebox_id': fields.many2one('project.gtd.timebox', "Timebox"),
160         'context_id': fields.many2one('project.gtd.context', "Context"),
161      }
162     def copy(self, cr, uid, id, default=None, context=None):
163         if not default:
164             default = {}
165         default['timebox_id']=False
166         default['context_id']=False
167         return super(project_task,self).copy(cr, uid, id, default, context)
168     def _get_context(self,cr, uid, ctx):
169         ids = self.pool.get('project.gtd.context').search(cr, uid, [], context=ctx)
170         return ids and ids[0] or False
171     _defaults = {
172         'context_id': _get_context
173     }
174     # Override read for using this method if context set !!!
175     #_order = "((55-ascii(coalesce(priority,'2')))*2 +  coalesce((date_start::date-current_date)/2,8))"
176 project_task()
177
178 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
179