[IMP]: base_calendar caldav crm_caldav: Apply Doc String + quality check
[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
30 class node_calendar(object):
31     def __init__(self, path, context, calendar):
32         self.path = path
33         self.context = context
34         self.calendar_id =  calendar.id
35         self.mimetype = 'ics'
36         self.create_date = calendar.create_date
37         self.write_date = calendar.write_date or calendar.create_date
38         self.content_length = 0
39         self.displayname = calendar.name
40
41
42     def get_data(self, cr, uid):
43         calendar_obj = pooler.get_pool(cr.dbname).get('basic.calendar')
44         return calendar_obj.export_cal(cr, uid, [self.calendar_id])
45
46     def get_data_len(self, cr):
47         return self.content_length
48
49     def set_data(self, cr, uid, data):
50         calendar_obj = pooler.get_pool(cr.dbname).get('basic.calendar')
51         return calendar_obj.import_cal(cr, uid, base64.encodestring(data), self.calendar_id)
52
53     def get_etag(self, cr):
54         """ Get a tag, unique per object + modification.
55
56             see. http://tools.ietf.org/html/rfc2616#section-13.3.3 """
57         return self._get_ttag(cr) + ':' + self._get_wtag(cr)
58
59     def _get_wtag(self, cr):
60         """ Return the modification time as a unique, compact string """
61         if self.write_date:
62             wtime = time.mktime(time.strptime(self.write_date, '%Y-%m-%d %H:%M:%S'))
63         else: wtime = time.time()
64         return str(wtime)
65
66     def _get_ttag(self, cr):
67         return 'calendar-%d' % self.calendar_id
68
69
70 class Calendar(osv.osv):
71     _inherit = 'basic.calendar'
72
73     def get_calendar_object(self, cr, uid, uri, context=None):
74         if not uri:
75             return None
76         if len(uri) > 1:
77             return None
78         name, file_type = tuple(uri[0].split('.'))
79         res = self.name_search(cr, uid, name)
80         if not res:
81             return None
82         calendar_id, calendar_name = res[0]
83         calendar = self.browse(cr, uid, calendar_id)
84         return node_calendar(uri, context, calendar)
85 Calendar()
86
87 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4