[IMP] caldav
[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 from document import nodes
29 import StringIO
30
31 class node_database(nodes.node_database):
32     def _child_get(self, cr, name=False, parent_id=False, domain=None):
33         dirobj = self.context._dirobj
34         uid = self.context.uid
35         ctx = self.context.context.copy()
36         ctx.update(self.dctx)
37         if not domain:
38             domain = []
39         domain2 = domain + [('calendar_collection','=', False)]
40         res = super(node_database, self)._child_get(cr, name=name, parent_id=parent_id, domain=domain2)
41         where = [('parent_id','=',parent_id)] 
42         domain2 = domain + [('calendar_collection','=', True)]                             
43         if name:
44             where.append(('name','=',name))
45         if domain2:
46             where += domain2
47
48         where2 = where + [('type', '=', 'directory')]
49         ids = dirobj.search(cr, uid, where2, context=ctx)              
50         for dirr in dirobj.browse(cr,uid,ids,context=ctx):            
51             res.append(node_calendar_collection(dirr.name,self,self.context,dirr))
52         return res
53
54 class node_calendar_collection(nodes.node_dir):     
55     def get_dav_props(self, cr):
56         res = {}        
57         return res
58
59     def get_dav_eprop(self,cr,ns,prop):        
60         return None
61
62     def _child_get(self, cr, name=False, parent_id=False, domain=None):
63         dirobj = self.context._dirobj
64         uid = self.context.uid
65         ctx = self.context.context.copy()
66         ctx.update(self.dctx)
67         where = [('collection_id','=',self.dir_id)]                              
68         if name:
69             where.append(('name','=',name))
70         if not domain:
71             domain = []       
72         
73         fil_obj = dirobj.pool.get('basic.calendar')        
74         ids = fil_obj.search(cr,uid,where,context=ctx)
75         res = []
76         if ids:
77             for fil in fil_obj.browse(cr,uid,ids,context=ctx):
78                 res.append(node_calendar(fil.name,self,self.context,fil))
79         return res
80
81     def get_owner(self, cr):
82         return False
83
84     
85     def get_etag(self, cr):
86         """ Get a tag, unique per object + modification.
87
88             see. http://tools.ietf.org/html/rfc2616#section-13.3.3 """
89         return self._get_ttag(cr) + ':' + self._get_wtag(cr)
90
91     def _get_wtag(self, cr):
92         """ Return the modification time as a unique, compact string """
93         if self.write_date:
94             wtime = time.mktime(time.strptime(self.write_date, '%Y-%m-%d %H:%M:%S'))
95         else: wtime = time.time()
96         return str(wtime)
97
98     def _get_ttag(self, cr):
99         return 'calendar collection-%d' % self.dir_id
100     
101     def get_ctag(self, cr):
102         return self.get_etag(cr)
103         
104
105 class node_calendar(nodes.node_class):
106     our_type = 'file'
107     def __init__(self,path, parent, context, calendar):
108         super(node_calendar,self).__init__(path, parent,context)
109         self.calendar_id = calendar.id
110         self.mimetype = 'ics'
111         self.create_date = calendar.create_date
112         self.write_date = calendar.write_date or calendar.create_date
113         self.content_length = 0
114         self.displayname = calendar.name        
115          
116     def open(self, cr, mode=False):
117         uid = self.context.uid        
118         if self.type in ('collection','database'):
119             return False            
120         fobj = self.context._dirobj.pool.get('basic.calendar').browse(cr, uid, self.calendar_id, context=self.context.context)        
121         s = StringIO.StringIO(self.get_data(cr, fobj))        
122         s.name = self
123         return s           
124
125     def get_dav_props(self, cr):
126         res = {}        
127         return res
128
129     def get_dav_eprop(self,cr,ns,prop):        
130         return None
131
132
133     def get_data(self, cr, fil_obj = None):        
134         uid = self.context.uid
135         calendar_obj = self.context._dirobj.pool.get('basic.calendar')
136         return calendar_obj.export_cal(cr, uid, [self.calendar_id])        
137
138     def get_data_len(self, cr, fil_obj = None):        
139         return self.content_length
140
141     def set_data(self, cr, data, fil_obj = None):
142         uid = self.context.uid
143         calendar_obj = self.context._dirobj.pool.get('basic.calendar')
144         return calendar_obj.import_cal(cr, uid, base64.encodestring(data), self.calendar_id)
145
146     def _get_ttag(self,cr):
147         return 'calendar-%d' % self.calendar_id
148
149     
150
151     def get_etag(self, cr):
152         """ Get a tag, unique per object + modification.
153
154             see. http://tools.ietf.org/html/rfc2616#section-13.3.3 """
155         return self._get_ttag(cr) + ':' + self._get_wtag(cr)
156
157     def _get_wtag(self, cr):
158         """ Return the modification time as a unique, compact string """
159         if self.write_date:
160             wtime = time.mktime(time.strptime(self.write_date, '%Y-%m-%d %H:%M:%S'))
161         else: wtime = time.time()
162         return str(wtime)   
163 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4