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