project_caldav: Fix column inheritance of project.task
[odoo/odoo.git] / addons / project_caldav / project_caldav.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 osv import fields, osv
23 from caldav import calendar
24 from datetime import datetime
25
26 from project.project import task as base_project_task
27
28 class project_task(osv.osv):
29     _name = "project.task"
30     _inherit = ["calendar.todo", "project.task"]
31     _columns = {
32         # force inherit from project.project_task so that 
33         # calendar.todo.active is masked oute
34         'active': base_project_task._columns['active'],
35         'write_date': fields.datetime('Write Date'),
36         'create_date': fields.datetime('Create Date', readonly=True),
37         'attendee_ids': fields.many2many('calendar.attendee', \
38                                          'task_attendee_rel', 'task_id', 'attendee_id', 'Attendees'),
39         'state': fields.selection([('draft', 'Draft'),('open', 'In Progress'),('pending', 'Pending'), ('cancelled', 'Cancelled'), ('done', 'Done')], 'State', readonly=True, required=True,
40                                   help='If the task is created the state is \'Draft\'.\n If the task is started, the state becomes \'In Progress\'.\n If review is needed the task is in \'Pending\' state.\
41                                   \n If the task is over, the states is set to \'Done\'.'),
42     }
43     _defaults = {
44         'state': 'draft',
45     }
46
47     def import_cal(self, cr, uid, data, data_id=None, context=None):
48         todo_obj = self.pool.get('basic.calendar.todo')
49         vals = todo_obj.import_cal(cr, uid, data, context=context)
50         return self.check_import(cr, uid, vals, context=context)
51
52     def check_import(self, cr, uid, vals, context=None):
53         if context is None:
54             context = {}
55         ids = []
56         for val in vals:
57             obj_tm = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.project_time_mode_id
58             if not val.get('planned_hours', False):
59                 # 'Computes duration' in days
60                 plan = 0.0
61                 if val.get('date') and  val.get('date_deadline'):
62                     start = datetime.strptime(val['date'], '%Y-%m-%d %H:%M:%S')
63                     end = datetime.strptime(val['date_deadline'], '%Y-%m-%d %H:%M:%S')
64                     diff = end - start
65                     plan = (diff.seconds/float(86400) + diff.days) * obj_tm.factor
66                 val['planned_hours'] = plan
67             else:
68                 # Converts timedelta into hours
69                 hours = (val['planned_hours'].seconds / float(3600)) + \
70                                         (val['planned_hours'].days * 24)
71                 val['planned_hours'] = hours
72             exists, r_id = calendar.uid2openobjectid(cr, val['id'], self._name, val.get('recurrent_id'))
73             val.pop('id')
74             if exists:
75                 self.write(cr, uid, [exists], val)
76                 ids.append(exists)
77             else:
78                 task_id = self.create(cr, uid, val)
79                 ids.append(task_id)
80         return ids
81
82     def export_cal(self, cr, uid, ids, context=None):
83         if context is None:
84             context = {}
85         task_datas = self.read(cr, uid, ids, [], context ={'read': True})
86         tasks = []
87         for task in task_datas:
88             if task.get('planned_hours', None) and task.get('date_deadline', None):
89                 task.pop('planned_hours')
90             tasks.append(task)
91         todo_obj = self.pool.get('basic.calendar.todo')
92         ical = todo_obj.export_cal(cr, uid, tasks, context={'model': self._name})
93         calendar_val = ical.serialize()
94         return calendar_val.replace('"', '').strip()
95
96 project_task()
97
98 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: