[REF] purchase: search view of purchase order and form view of merge order wizard
[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 caldav
24
25 class project_task(osv.osv):
26     _name = "project.task"
27     _inherit = ["calendar.todo", "project.task"]
28     _columns = {
29                 'attendee_ids': fields.many2many('calendar.attendee', \
30                     'task_attendee_rel', 'task_id', 'attendee_id', 'Attendees'),
31                 }
32     def import_cal(self, cr, uid, data, data_id=None, context={}):
33         todo_obj = self.pool.get('basic.calendar.todo')
34         vals = todo_obj.import_cal(cr, uid, data, context=context)
35         return self.check_import(cr, uid, vals, context=context)
36     
37     def check_import(self, cr, uid, vals, context={}):
38         ids = []
39         for val in vals:
40             obj_tm = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.project_time_mode_id
41             if not val.has_key('planned_hours'):
42                 # 'Computes duration' in days
43                 plan = 0.0
44                 if val.get('date') and  val.get('date_deadline'):
45                     start = datetime.strptime(val['date'], '%Y-%m-%d %H:%M:%S')
46                     end = datetime.strptime(val['date_deadline'], '%Y-%m-%d %H:%M:%S')
47                     diff = end - start
48                     plan = (diff.seconds/float(86400) + diff.days) * obj_tm.factor
49                 val['planned_hours'] = plan
50             else:
51                 # Converts timedelta into hours
52                 hours = (val['planned_hours'].seconds / float(3600)) + \
53                                         (val['planned_hours'].days * 24)
54                 val['planned_hours'] = hours
55             exists, r_id = caldav.uid2openobjectid(cr, val['id'], self._name, val.get('recurrent_id'))
56             val.pop('id')
57             if exists:
58                 self.write(cr, uid, [exists], val)
59                 ids.append(exists)
60             else:
61                 task_id = self.create(cr, uid, val)
62                 ids.append(task_id)
63         return ids
64         
65     def export_cal(self, cr, uid, ids, context={}):
66         task_datas = self.read(cr, uid, ids, [], context ={'read': True})
67         tasks = []
68         for task in task_datas:
69             if task.get('planned_hours', None) and task.get('date_deadline', None):
70                 task.pop('planned_hours')
71             tasks.append(task)
72         todo_obj = self.pool.get('basic.calendar.todo')
73         ical = todo_obj.export_cal(cr, uid, tasks, context={'model': self._name})
74         calendar_val = ical.serialize()
75         calendar_val = calendar_val.replace('"', '').strip()
76         return calendar_val
77
78
79 project_task()
80
81 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: