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 def get_dav_props(self, cr):
65 def get_dav_eprop(self,cr, ns, propname):
66 if self.M_NS.has_key(ns):
67 prefix = self.M_NS[ns]
69 print "No namespace:",ns, "( for prop:", propname,")"
72 mname = prefix + "_" + propname
74 if not hasattr(self, mname):
78 m = getattr(self, mname)
81 except AttributeError, e:
82 print 'Property %s not supported' % propname
86 def _file_get(self,cr, nodename=False):
90 def get_domain(self, cr, filters):
92 dirobj = self.context._dirobj
93 uid = self.context.uid
94 ctx = self.context.context.copy()
96 calendar_obj = dirobj.pool.get('basic.calendar')
99 if filters.localName == 'calendar-query':
101 for filter_child in filters.childNodes:
102 if filter_child.nodeType == filter_child.TEXT_NODE:
104 if filter_child.localName == 'filter':
105 for vcalendar_filter in filter_child.childNodes:
106 if vcalendar_filter.nodeType == vcalendar_filter.TEXT_NODE:
108 if vcalendar_filter.localName == 'comp-filter':
109 if vcalendar_filter.getAttribute('name') == 'VCALENDAR':
110 for vevent_filter in vcalendar_filter.childNodes:
111 if vevent_filter.nodeType == vevent_filter.TEXT_NODE:
113 if vevent_filter.localName == 'comp-filter':
114 if vevent_filter.getAttribute('name') == 'VEVENT':
115 res = [('type','=','vevent')]
116 if vevent_filter.getAttribute('name') == 'VTODO':
117 res = [('type','=','vtodo')]
120 elif filters.localName == 'calendar-multiget':
122 for filter_child in filters.childNodes:
123 if filter_child.nodeType == filter_child.TEXT_NODE:
125 if filter_child.localName == 'href':
126 if not filter_child.firstChild:
128 uri = filter_child.firstChild.data
129 caluri = uri.split('/')
132 calendar = calendar_obj.name_search(cr, uid, caluri)
134 calendar_id, calendar_name = calendar[0]
135 ids.append(calendar_id)
136 return [('id', 'in', ids)]
139 def _child_get(self, cr, name=False, parent_id=False, domain=None):
140 dirobj = self.context._dirobj
141 uid = self.context.uid
142 ctx = self.context.context.copy()
143 ctx.update(self.dctx)
144 where = [('collection_id','=',self.dir_id)]
146 where.append(('name','=',name))
149 where = where + domain
150 fil_obj = dirobj.pool.get('basic.calendar')
151 ids = fil_obj.search(cr,uid,where,context=ctx)
154 for fil in fil_obj.browse(cr,uid,ids,context=ctx):
155 res.append(node_calendar(fil.name,self,self.context,fil))
158 def _get_dav_owner(self, cr):
162 def get_etag(self, cr):
163 """ Get a tag, unique per object + modification.
165 see. http://tools.ietf.org/html/rfc2616#section-13.3.3 """
166 return self._get_ttag(cr) + ':' + self._get_wtag(cr)
168 def _get_wtag(self, cr):
169 """ Return the modification time as a unique, compact string """
171 wtime = time.mktime(time.strptime(self.write_date, '%Y-%m-%d %H:%M:%S'))
172 else: wtime = time.time()
175 def _get_ttag(self, cr):
176 return 'calendar collection-%d' % self.dir_id
178 def _get_dav_getctag(self, cr):
179 result = self.get_etag(cr)
183 class node_calendar(nodes.node_class):
186 "http://calendarserver.org/ns/" : ('getctag'),
187 "urn:ietf:params:xml:ns:caldav" : (
188 'calendar-description',
191 'calendar-user-address-set',
192 'schedule-inbox-URL',
193 'schedule-outbox-URL',)}
195 "http://calendarserver.org/ns/" : '_get_dav',
196 "urn:ietf:params:xml:ns:caldav" : '_get_caldav'}
198 def __init__(self,path, parent, context, calendar):
199 super(node_calendar,self).__init__(path, parent,context)
200 self.calendar_id = calendar.id
201 self.mimetype = 'ics'
202 self.create_date = calendar.create_date
203 self.write_date = calendar.write_date or calendar.create_date
204 self.content_length = 0
205 self.displayname = calendar.name
207 def open(self, cr, mode=False):
208 uid = self.context.uid
209 if self.type in ('collection','database'):
211 fobj = self.context._dirobj.pool.get('basic.calendar').browse(cr, uid, self.calendar_id, context=self.context.context)
212 s = StringIO.StringIO(self.get_data(cr, fobj))
218 def get_dav_props(self, cr):
221 def get_dav_eprop(self,cr, ns, propname):
222 if self.M_NS.has_key(ns):
223 prefix = self.M_NS[ns]
225 print "No namespace:",ns, "( for prop:", propname,")"
227 propname = propname.replace('-','_')
228 mname = prefix + "_" + propname
229 if not hasattr(self, mname):
233 m = getattr(self, mname)
236 except AttributeError, e:
237 print 'Property %s not supported' % propname
238 print "Exception:", e
242 def get_data(self, cr, fil_obj = None):
243 uid = self.context.uid
244 calendar_obj = self.context._dirobj.pool.get('basic.calendar')
245 res = calendar_obj.export_cal(cr, uid, [self.calendar_id])
248 def get_data_len(self, cr, fil_obj = None):
249 return self.content_length
251 def set_data(self, cr, data, fil_obj = None):
252 uid = self.context.uid
253 calendar_obj = self.context._dirobj.pool.get('basic.calendar')
254 return calendar_obj.import_cal(cr, uid, base64.encodestring(data), self.calendar_id)
256 def _get_ttag(self,cr):
257 return 'calendar-%d' % self.calendar_id
261 def _get_caldav_calendar_data(self, cr):
262 return self.get_data(cr)
265 def _get_caldav_calendar_description(self, cr):
266 uid = self.context.uid
267 calendar_obj = self.context._dirobj.pool.get('basic.calendar')
268 ctx = self.context.context.copy()
269 ctx.update(self.dctx)
270 calendar = calendar_obj.browse(cr, uid, self.calendar_id, context=ctx)
271 return calendar.description
274 def _get_caldav_calendar_home_set(self, cr):
275 import xml.dom.minidom
277 uid = self.context.uid
278 ctx = self.context.context.copy()
279 ctx.update(self.dctx)
280 doc = xml.dom.minidom.getDOMImplementation().createDocument(None, 'href', None)
282 calendar_obj = self.context._dirobj.pool.get('basic.calendar')
283 calendar = calendar_obj.browse(cr, uid, self.calendar_id, context=ctx)
284 huri = doc.createTextNode(urllib.quote('/%s/%s' % (cr.dbname, calendar.collection_id.name)))
285 href = doc.documentElement
286 href.tagName = 'D:href'
287 href.appendChild(huri)
290 def _get_caldav_calendar_user_address_set(self, cr):
291 import xml.dom.minidom
292 dirobj = self.context._dirobj
293 uid = self.context.uid
294 ctx = self.context.context.copy()
295 ctx.update(self.dctx)
296 user_obj = self.context._dirobj.pool.get('res.users')
297 user = user_obj.browse(cr, uid, uid, context=ctx)
298 doc = xml.dom.minidom.getDOMImplementation().createDocument(None, 'href', None)
299 href = doc.documentElement
300 href.tagName = 'D:href'
301 huri = doc.createTextNode('MAILTO:' + user.email)
302 href.appendChild(huri)
306 def _get_caldav_schedule_inbox_URL(self, cr):
307 import xml.dom.minidom
309 uid = self.context.uid
310 ctx = self.context.context.copy()
311 ctx.update(self.dctx)
312 calendar_obj = self.context._dirobj.pool.get('basic.calendar')
313 calendar = calendar_obj.browse(cr, uid, self.calendar_id, context=ctx)
314 res = '%s/%s' %(calendar.name, calendar.collection_id.name)
315 doc = xml.dom.minidom.getDOMImplementation().createDocument(None, 'href', None)
316 href = doc.documentElement
317 href.tagName = 'D:href'
318 huri = doc.createTextNode(urllib.quote('/%s/%s' % (cr.dbname, res)))
319 href.appendChild(huri)
324 def _get_caldav_schedule_outbox_URL(self, cr):
325 return _get_caldav_schedule_inbox_URL(cr)
328 def get_etag(self, cr):
329 """ Get a tag, unique per object + modification.
331 see. http://tools.ietf.org/html/rfc2616#section-13.3.3 """
332 return self._get_ttag(cr) + ':' + self._get_wtag(cr)
334 def _get_wtag(self, cr):
335 """ Return the modification time as a unique, compact string """
337 wtime = time.mktime(time.strptime(self.write_date, '%Y-%m-%d %H:%M:%S'))
338 else: wtime = time.time()
340 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4