[MERGE]
[odoo/odoo.git] / addons / caldav / caldav_node.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 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, fields
23 from tools.translate import _
24 import pooler
25 import tools
26 import time
27 import base64
28
29 class node_calendar(object):    
30     def __init__(self, path, context, calendar):
31         self.path = path
32         self.context = context
33         self.calendar_id =  calendar.id  
34         self.mimetype = 'ics'        
35         self.create_date = calendar.create_date
36         self.write_date = calendar.write_date or calendar.create_date
37         self.content_length = 0
38         self.displayname = calendar.name
39         
40     
41     def get_data(self, cr, uid):        
42         calendar_obj = pooler.get_pool(cr.dbname).get('basic.calendar')
43         return calendar_obj.export_cal(cr, uid, [self.calendar_id])               
44         
45     def get_data_len(self, cr):        
46         return self.content_length
47
48     def set_data(self, cr, uid, data):        
49         calendar_obj = pooler.get_pool(cr.dbname).get('basic.calendar')        
50         return calendar_obj.import_cal(cr, uid, base64.encodestring(data), self.calendar_id)
51
52     def get_etag(self,cr):
53         """ Get a tag, unique per object + modification.
54     
55             see. http://tools.ietf.org/html/rfc2616#section-13.3.3 """
56         return self._get_ttag(cr) + ':' + self._get_wtag(cr)
57
58     def _get_wtag(self,cr):
59         """ Return the modification time as a unique, compact string """
60         if self.write_date:
61             wtime = time.mktime(time.strptime(self.write_date,'%Y-%m-%d %H:%M:%S'))
62         else: wtime = time.time()
63         return str(wtime)
64
65     def _get_ttag(self,cr):
66         return 'calendar-%d' % self.calendar_id
67
68 class Calendar(osv.osv):
69     _inherit = 'basic.calendar'
70
71     def get_calendar_object(self, cr, uid, uri, context=None):
72         if not uri:
73             return None
74         if len(uri) > 1:
75             return None
76         name, file_type = tuple(uri[0].split('.'))
77         res = self.name_search(cr, uid, name)
78         if not res:
79             return None
80         calendar_id, calendar_name = res[0]
81         calendar = self.browse(cr, uid, calendar_id)
82         return node_calendar(uri, context, calendar) 
83 Calendar()