[MERGE]: Merge with lp:~openerp-dev/openobject-addons/trunk-dev-addons1
[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 osv
23 from base_calendar import base_calendar
24 from caldav import calendar
25 from datetime import datetime
26 import re
27
28 class crm_meeting(osv.osv):
29     _inherit = 'crm.meeting'
30
31     def export_cal(self, cr, uid, ids, context=None):
32         """
33             @param self: The object pointer
34             @param cr: the current row, from the database cursor,
35             @param uid: the current user’s ID for security checks,
36             @param ids: List of CRM Meeting’s IDs
37             @param context: A standard dictionary for contextual values
38         """
39         if context is None: 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
48     def import_cal(self, cr, uid, data, data_id=None, context=None):
49         """
50             @param self: The object pointer
51             @param cr: the current row, from the database cursor,
52             @param uid: the current user’s ID for security checks,
53             @param data: Get Data of CRM Meetings
54             @param data_id: calendar's Id
55             @param context: A standard dictionary for contextual values
56         """
57         event_obj = self.pool.get('basic.calendar.event')
58         vals = event_obj.import_cal(cr, uid, data, context=context)
59         return self.check_import(cr, uid, vals, context=context)
60
61     def check_import(self, cr, uid, vals, context=None):
62         """
63             @param self: The object pointer
64             @param cr: the current row, from the database cursor,
65             @param uid: the current user’s ID for security checks,
66             @param vals: Get Values
67             @param context: A standard dictionary for contextual values
68         """
69         if context is None:
70             context = {}
71         ids = []
72         model_obj = self.pool.get(context.get('model'))
73         recur_pool = {}
74         try:
75             for val in vals:
76                 # Compute value of duration
77                 if val.get('date_deadline', False) and 'duration' not in val:
78                     start = datetime.strptime(val['date'], '%Y-%m-%d %H:%M:%S')
79                     end = datetime.strptime(val['date_deadline'], '%Y-%m-%d %H:%M:%S')
80                     diff = end - start
81                     val['duration'] = (diff.seconds/float(86400) + diff.days) * 24
82                 exists, r_id = calendar.uid2openobjectid(cr, val['id'], context.get('model'), \
83                                                                  val.get('recurrent_id'))
84                 if val.has_key('create_date'):
85                     val.pop('create_date')
86                 u_id = val.get('id', None)
87                 val.pop('id')
88                 if exists and r_id:
89                     val.update({'recurrent_uid': exists})
90                     model_obj.write(cr, uid, [r_id], val)
91                     ids.append(r_id)
92                     
93                 elif exists:
94                     model_obj.write(cr, uid, [exists], val)
95                     ids.append(exists)
96                 else:
97                     if u_id in recur_pool and val.get('recurrent_id'):
98                         val.update({'recurrent_uid': recur_pool[u_id]})
99                         revent_id = model_obj.create(cr, uid, val)
100                         ids.append(revent_id)
101                     else:
102                         __rege = re.compile(r'OpenObject-([\w|\.]+)_([0-9]+)@(\w+)$')
103                         wematch = __rege.match(u_id.encode('utf8'))
104                         if wematch:
105                             model, recur_id, dbname = wematch.groups()
106                             val.update({'recurrent_uid': recur_id})
107                         event_id = model_obj.create(cr, uid, val)
108                         recur_pool[u_id] = event_id
109                         ids.append(event_id)
110         except Exception:
111             raise
112         return ids
113     
114 crm_meeting()
115
116 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
117