[REF+IMP] document
[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     
82     def get_etag(self, cr):
83         """ Get a tag, unique per object + modification.
84
85             see. http://tools.ietf.org/html/rfc2616#section-13.3.3 """
86         return self._get_ttag(cr) + ':' + self._get_wtag(cr)
87
88     def _get_wtag(self, cr):
89         """ Return the modification time as a unique, compact string """
90         if self.write_date:
91             wtime = time.mktime(time.strptime(self.write_date, '%Y-%m-%d %H:%M:%S'))
92         else: wtime = time.time()
93         return str(wtime)
94
95     def _get_ttag(self, cr):
96         return 'calendar collection-%d' % self.dir_id
97
98 class node_calendar(nodes.node_class):
99     our_type = 'file'
100     def __init__(self,path, parent, context, calendar):
101         super(node_calendar,self).__init__(path, parent,context)
102         self.calendar_id = calendar.id
103         self.mimetype = 'ics'
104         self.create_date = calendar.create_date
105         self.write_date = calendar.write_date or calendar.create_date
106         self.content_length = 0
107         self.displayname = calendar.name        
108          
109     def open(self, cr, mode=False):
110         uid = self.context.uid        
111         if self.type in ('collection','database'):
112             return False            
113         fobj = self.context._dirobj.pool.get('basic.calendar').browse(cr, uid, self.calendar_id, context=self.context.context)        
114         s = StringIO.StringIO(self.get_data(cr, fobj))        
115         s.name = self
116         return s           
117
118     def get_dav_props(self, cr):
119         res = {}        
120         return res
121
122     def get_dav_eprop(self,cr,ns,prop):        
123         return None
124
125
126     def get_data(self, cr, fil_obj = None):        
127         uid = self.context.uid
128         calendar_obj = self.context._dirobj.pool.get('basic.calendar')
129         return calendar_obj.export_cal(cr, uid, [self.calendar_id])        
130
131     def get_data_len(self, cr, fil_obj = None):        
132         return self.content_length
133
134     def set_data(self, cr, data, fil_obj = None):
135         uid = self.context.uid
136         calendar_obj = self.context._dirobj.pool.get('basic.calendar')
137         return calendar_obj.import_cal(cr, uid, base64.encodestring(data), self.calendar_id)
138
139     def _get_ttag(self,cr):
140         return 'calendar-%d' % self.calendar_id
141
142     
143
144     def get_etag(self, cr):
145         """ Get a tag, unique per object + modification.
146
147             see. http://tools.ietf.org/html/rfc2616#section-13.3.3 """
148         return self._get_ttag(cr) + ':' + self._get_wtag(cr)
149
150     def _get_wtag(self, cr):
151         """ Return the modification time as a unique, compact string """
152         if self.write_date:
153             wtime = time.mktime(time.strptime(self.write_date, '%Y-%m-%d %H:%M:%S'))
154         else: wtime = time.time()
155         return str(wtime)   
156 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4