1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import osv, fields
23 from tools.translate import _
28 from document import nodes
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()
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)]
44 where.append(('name','=',name))
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))
54 class node_calendar_collection(nodes.node_dir):
56 "http://calendarserver.org/ns/" : ('getctag'),
59 "http://calendarserver.org/ns/" : '_get_dav',
62 http_options = { 'DAV': ['calendar-access'] }
64 def _file_get(self,cr, nodename=False):
67 def _child_get(self, cr, name=False, parent_id=False, domain=None):
68 dirobj = self.context._dirobj
69 uid = self.context.uid
70 ctx = self.context.context.copy()
72 where = [('collection_id','=',self.dir_id)]
75 res = name.split('.ics')
80 where.append(('name','=',name))
83 where = where + domain
84 fil_obj = dirobj.pool.get('basic.calendar')
85 ids = fil_obj.search(cr,uid,where,context=ctx)
87 for calender in fil_obj.browse(cr, uid, ids, context=ctx):
89 res.append(node_calendar(calender.name, self, self.context, calender))
91 res.append(res_node_calendar(name, self, self.context, calender))
94 def _get_dav_owner(self, cr):
98 def get_etag(self, cr):
99 """ Get a tag, unique per object + modification.
101 see. http://tools.ietf.org/html/rfc2616#section-13.3.3 """
102 return self._get_ttag(cr) + ':' + self._get_wtag(cr)
104 def _get_wtag(self, cr):
105 """ Return the modification time as a unique, compact string """
107 wtime = time.mktime(time.strptime(self.write_date, '%Y-%m-%d %H:%M:%S'))
108 else: wtime = time.time()
111 def _get_ttag(self, cr):
112 return 'calendar collection-%d' % self.dir_id
114 def _get_dav_getctag(self, cr):
115 result = self.get_etag(cr)
119 class node_calendar(nodes.node_class):
120 our_type = 'collection'
122 "http://calendarserver.org/ns/" : ('getctag'),
123 "urn:ietf:params:xml:ns:caldav" : (
124 'calendar-description',
127 'calendar-user-address-set',
128 'schedule-inbox-URL',
129 'schedule-outbox-URL',)}
132 "http://calendarserver.org/ns/" : '_get_dav',
133 "urn:ietf:params:xml:ns:caldav" : '_get_caldav'}
135 http_options = { 'DAV': ['calendar-access'] }
137 def __init__(self,path, parent, context, calendar):
138 super(node_calendar,self).__init__(path, parent,context)
139 self.calendar_id = calendar.id
140 self.mimetype = 'application/x-directory'
141 self.create_date = calendar.create_date
142 self.write_date = calendar.write_date or calendar.create_date
143 self.content_length = 0
144 self.displayname = calendar.name
145 self.cal_type = calendar.type
147 def _get_dav_getctag(self, cr):
148 result = self._get_ttag(cr) + ':' + str(time.time())
151 def removeme_match_dav_eprop(self, cr, match, ns, prop):
153 if ns == "DAV:" and prop == "getetag":
154 dirobj = self.context._dirobj
155 uid = self.context.uid
156 ctx = self.context.context.copy()
157 tem, dav_time = tuple(match.split(':'))
158 model, res_id = tuple(tem.split('_'))
159 model_obj = dirobj.pool.get(model)
160 model = model_obj.browse(cr, uid, res_id, context=ctx)
161 write_time = model.write_date or model.create_date
162 wtime = time.mktime(time.strptime(write_time,'%Y-%m-%d %H:%M:%S'))
163 if float(dav_time) == float(wtime):
166 res = super(node_calendar, self).match_dav_eprop(cr, match, ns, prop)
169 def get_domain(self, cr, filters):
171 dirobj = self.context._dirobj
172 uid = self.context.uid
173 ctx = self.context.context.copy()
174 ctx.update(self.dctx)
175 calendar_obj = dirobj.pool.get('basic.calendar')
178 if filters.localName == 'calendar-query':
180 for filter_child in filters.childNodes:
181 if filter_child.nodeType == filter_child.TEXT_NODE:
183 if filter_child.localName == 'filter':
184 for vcalendar_filter in filter_child.childNodes:
185 if vcalendar_filter.nodeType == vcalendar_filter.TEXT_NODE:
187 if vcalendar_filter.localName == 'comp-filter':
188 if vcalendar_filter.getAttribute('name') == 'VCALENDAR':
189 for vevent_filter in vcalendar_filter.childNodes:
190 if vevent_filter.nodeType == vevent_filter.TEXT_NODE:
192 if vevent_filter.localName == 'comp-filter':
193 if vevent_filter.getAttribute('name') == 'VEVENT':
194 res = [('type','=','vevent')]
195 if vevent_filter.getAttribute('name') == 'VTODO':
196 res = [('type','=','vtodo')]
198 elif filters.localName == 'calendar-multiget':
200 for filter_child in filters.childNodes:
201 if filter_child.nodeType == filter_child.TEXT_NODE:
203 if filter_child.localName == 'href':
204 if not filter_child.firstChild:
206 uri = filter_child.firstChild.data
207 caluri = uri.split('/')
210 if caluri not in names : names.append(caluri)
211 res = [('name','in',names)]
215 def children(self, cr, domain=None):
216 return self._child_get(cr, domain=domain)
218 def child(self,cr, name, domain=None):
219 res = self._child_get(cr, name, domain=domain)
225 def _child_get(self, cr, name=False, parent_id=False, domain=None):
226 dirobj = self.context._dirobj
227 uid = self.context.uid
228 ctx = self.context.context.copy()
229 ctx.update(self.dctx)
232 where.append(('id','=',int(name)))
235 #for opr1, opt, opr2 in domain:
236 # if opr1 == 'type' and opr2 != self.cal_type:
239 fil_obj = dirobj.pool.get('basic.calendar')
240 ids = fil_obj.search(cr, uid, domain)
242 if self.calendar_id in ids:
243 res = fil_obj.get_calendar_objects(cr, uid, [self.calendar_id], self, domain=where, context=ctx)
246 def create_child(self,cr,path,data):
247 """ API function to create a child file object and node
248 Return the node_* created
250 return self.set_data(cr, data)
253 def set_data(self, cr, data, fil_obj = None):
254 uid = self.context.uid
255 calendar_obj = self.context._dirobj.pool.get('basic.calendar')
256 return calendar_obj.import_cal(cr, uid, base64.encodestring(data), self.calendar_id)
258 def get_data_len(self, cr, fil_obj = None):
259 return self.content_length
262 def _get_ttag(self,cr):
263 return 'calendar-%d' % (self.calendar_id,)
267 def get_etag(self, cr):
268 """ Get a tag, unique per object + modification.
270 see. http://tools.ietf.org/html/rfc2616#section-13.3.3 """
271 return self._get_ttag(cr) + ':' + self._get_wtag(cr)
273 def _get_wtag(self, cr):
274 """ Return the modification time as a unique, compact string """
276 wtime = time.mktime(time.strptime(self.write_date, '%Y-%m-%d %H:%M:%S'))
277 else: wtime = time.time()
284 class res_node_calendar(nodes.node_class):
287 "http://calendarserver.org/ns/" : ('getctag'),
288 "urn:ietf:params:xml:ns:caldav" : (
289 'calendar-description',
292 'calendar-user-address-set',
293 'schedule-inbox-URL',
294 'schedule-outbox-URL',)}
296 "http://calendarserver.org/ns/" : '_get_dav',
297 "urn:ietf:params:xml:ns:caldav" : '_get_caldav'}
299 http_options = { 'DAV': ['calendar-access'] }
301 def __init__(self,path, parent, context, res_obj, res_model=None, res_id=None):
302 super(res_node_calendar,self).__init__(path, parent, context)
303 self.mimetype = 'text/calendar'
304 self.create_date = parent.create_date
305 self.write_date = parent.write_date or parent.create_date
306 self.calendar_id = hasattr(parent, 'calendar_id') and parent.calendar_id or False
308 if not self.calendar_id: self.calendar_id = res_obj.id
309 self.create_date = res_obj.create_date
310 self.write_date = res_obj.write_date or res_obj.create_date
311 self.displayname = res_obj.name
313 self.content_length = 0
315 self.model = res_model
318 def open(self, cr, mode=False):
319 uid = self.context.uid
320 if self.type in ('collection','database'):
322 s = StringIO.StringIO(self.get_data(cr))
326 def get_data(self, cr, fil_obj = None):
327 uid = self.context.uid
328 calendar_obj = self.context._dirobj.pool.get('basic.calendar')
329 context = self.context.context.copy()
330 context.update({'model': self.model, 'res_id':self.res_id})
331 res = calendar_obj.export_cal(cr, uid, [self.calendar_id], context=context)
334 def get_data_len(self, cr, fil_obj = None):
335 return self.content_length
337 def set_data(self, cr, data, fil_obj = None):
338 uid = self.context.uid
339 calendar_obj = self.context._dirobj.pool.get('basic.calendar')
340 return calendar_obj.import_cal(cr, uid, base64.encodestring(data), self.calendar_id)
342 def _get_ttag(self,cr):
344 if self.model and self.res_id:
345 res = '%s_%d' % (self.model, self.res_id)
346 elif self.calendar_id:
347 res = '%d' % (self.calendar_id)
350 def _get_caldav_calendar_data(self, cr):
351 return self.get_data(cr)
354 def _get_caldav_calendar_description(self, cr):
355 uid = self.context.uid
356 calendar_obj = self.context._dirobj.pool.get('basic.calendar')
357 ctx = self.context.context.copy()
358 ctx.update(self.dctx)
359 calendar = calendar_obj.browse(cr, uid, self.calendar_id, context=ctx)
360 return calendar.description
363 def _get_caldav_calendar_home_set(self, cr):
364 import xml.dom.minidom
366 uid = self.context.uid
367 ctx = self.context.context.copy()
368 ctx.update(self.dctx)
369 doc = xml.dom.minidom.getDOMImplementation().createDocument(None, 'href', None)
371 calendar_obj = self.context._dirobj.pool.get('basic.calendar')
372 calendar = calendar_obj.browse(cr, uid, self.calendar_id, context=ctx)
373 huri = doc.createTextNode(urllib.quote('/%s/%s' % (cr.dbname, calendar.collection_id.name)))
374 href = doc.documentElement
375 href.tagName = 'D:href'
376 href.appendChild(huri)
379 def _get_caldav_calendar_user_address_set(self, cr):
380 import xml.dom.minidom
381 dirobj = self.context._dirobj
382 uid = self.context.uid
383 ctx = self.context.context.copy()
384 ctx.update(self.dctx)
385 user_obj = self.context._dirobj.pool.get('res.users')
386 user = user_obj.browse(cr, uid, uid, context=ctx)
387 doc = xml.dom.minidom.getDOMImplementation().createDocument(None, 'href', None)
388 href = doc.documentElement
389 href.tagName = 'D:href'
390 huri = doc.createTextNode('MAILTO:' + user.email)
391 href.appendChild(huri)
395 def _get_caldav_schedule_inbox_URL(self, cr):
396 import xml.dom.minidom
398 uid = self.context.uid
399 ctx = self.context.context.copy()
400 ctx.update(self.dctx)
401 calendar_obj = self.context._dirobj.pool.get('basic.calendar')
402 calendar = calendar_obj.browse(cr, uid, self.calendar_id, context=ctx)
403 res = '%s/%s' %(calendar.name, calendar.collection_id.name)
404 doc = xml.dom.minidom.getDOMImplementation().createDocument(None, 'href', None)
405 href = doc.documentElement
406 href.tagName = 'D:href'
407 huri = doc.createTextNode(urllib.quote('/%s/%s' % (cr.dbname, res)))
408 href.appendChild(huri)
413 uid = self.context.uid
415 if self.type in ('collection','database'):
417 if self.model and self.res_id:
418 document_obj = self.context._dirobj.pool.get(self.model)
421 #res = document_obj.unlink(cr, uid, [self.res_id]) #TOFIX
427 def _get_caldav_schedule_outbox_URL(self, cr):
428 return self._get_caldav_schedule_inbox_URL(cr)
431 def get_etag(self, cr):
432 """ Get a tag, unique per object + modification.
434 see. http://tools.ietf.org/html/rfc2616#section-13.3.3 """
435 return self._get_ttag(cr) + ':' + self._get_wtag(cr)
437 def _get_wtag(self, cr):
438 """ Return the modification time as a unique, compact string """
440 wtime = time.mktime(time.strptime(self.write_date, '%Y-%m-%d %H:%M:%S'))
441 else: wtime = time.time()
444 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4