[REF]: caldav: Code improvement regarding context
[odoo/odoo.git] / addons / crm_caldav / crm_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 crm import crm
24 from caldav import calendar
25 from datetime import datetime
26
27 class crm_meeting(osv.osv):
28     _inherit = 'crm.meeting'
29
30     def export_cal(self, cr, uid, ids, context=None):
31         """
32             @param self: The object pointer
33             @param cr: the current row, from the database cursor,
34             @param uid: the current user’s ID for security checks,
35             @param ids: List of CRM Meeting’s IDs
36             @param context: A standard dictionary for contextual values
37         """
38         if not context:
39             context = {}
40         ids = map(lambda x: base_calendar.base_calendar_id2real_id(x), ids)
41         event_data = self.read(cr, uid, ids, context=context)
42         event_obj = self.pool.get('basic.calendar.event')
43         context.update({'model': self._name})
44         ical = event_obj.export_cal(cr, uid, event_data, context=context)
45         return ical.serialize()
46
47     def import_cal(self, cr, uid, data, data_id=None, context=None):
48         """
49             @param self: The object pointer
50             @param cr: the current row, from the database cursor,
51             @param uid: the current user’s ID for security checks,
52             @param data: Get Data of CRM Meetings
53             @param data_id: calendar's Id
54             @param context: A standard dictionary for contextual values
55         """
56         if not context:
57             context = {}
58         event_obj = self.pool.get('basic.calendar.event')
59         vals = event_obj.import_cal(cr, uid, data, context=context)
60         return self.check_import(cr, uid, vals, context=context)
61
62     def check_import(self, cr, uid, vals, context=None):
63         """
64             @param self: The object pointer
65             @param cr: the current row, from the database cursor,
66             @param uid: the current user’s ID for security checks,
67             @param vals: Get Values
68             @param context: A standard dictionary for contextual values
69         """
70         if not context:
71             context = {}
72         ids = []
73         model_obj = self.pool.get(context.get('model'))
74         recur_pool = {}
75         try:
76             for val in vals:
77                 exists, r_id = calendar.uid2openobjectid(cr, val['id'], context.get('model'), \
78                                                                  val.get('recurrent_id'))
79                 if val.has_key('create_date'): val.pop('create_date')
80                 u_id = val.get('id', None)
81                 val.pop('id')
82                 if exists and r_id:
83                     val.update({'recurrent_uid': exists})
84                     model_obj.write(cr, uid, [r_id], val)
85                     ids.append(r_id)
86                 elif exists:
87                     # Compute value of duration
88                     if 'date_deadline' in val and 'duration' not in val:
89                         start = datetime.strptime(val['date'], '%Y-%m-%d %H:%M:%S')
90                         end = datetime.strptime(val['date_deadline'], '%Y-%m-%d %H:%M:%S')
91                         diff = end - start
92                         val['duration'] = (diff.seconds/float(86400) + diff.days) * 24
93                     model_obj.write(cr, uid, [exists], val)
94                     ids.append(exists)
95                 else:
96                     if u_id in recur_pool and val.get('recurrent_id'):
97                         val.update({'recurrent_uid': recur_pool[u_id]})
98                         revent_id = model_obj.create(cr, uid, val)
99                         ids.append(revent_id)
100                     else:
101                         event_id = model_obj.create(cr, uid, val)
102                         recur_pool[u_id] = event_id
103                         ids.append(event_id)
104         except Exception, e:
105             raise osv.except_osv(('Error !'), (str(e)))
106         return ids
107     
108 crm_meeting()
109
110 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
111