Launchpad automatic translations update.
[odoo/odoo.git] / addons / caldav / calendar_collection.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 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 caldav_node
25 import logging
26
27 class calendar_collection(osv.osv):
28     _inherit = 'document.directory' 
29     _columns = {
30         'calendar_collection' : fields.boolean('Calendar Collection'),
31         'calendar_ids': fields.one2many('basic.calendar', 'collection_id', 'Calendars'),
32     }
33     _default = {
34         'calendar_collection' : False,
35     }
36     
37     def _get_root_calendar_directory(self, cr, uid, context=None):
38         objid = self.pool.get('ir.model.data')
39         try:
40             mid = objid._get_id(cr, uid, 'document', 'dir_calendars')
41             if not mid:
42                 return False
43             root_id = objid.read(cr, uid, mid, ['res_id'])['res_id']
44             root_cal_dir = self.browse(cr,uid, root_id, context=context) 
45             return root_cal_dir.name
46         except Exception:
47             logger = logging.getLogger('document')
48             logger.warning('Cannot set root directory for Calendars:', exc_info=True)
49             return False
50         return False
51
52     def get_node_class(self, cr, uid, ids, dbro=None, dynamic=False, context=None):
53         if dbro is None:
54             dbro = self.browse(cr, uid, ids, context=context)
55
56         if dbro.calendar_collection:
57             if dynamic:
58                 return caldav_node.node_calendar_res_col
59             else:
60                 return caldav_node.node_calendar_collection
61         else:
62             return super(calendar_collection, self).\
63                     get_node_class(cr, uid, ids, dbro=dbro,dynamic=dynamic, 
64                                     context=context)
65
66     def get_description(self, cr, uid, ids, context=None):
67         #TODO : return description of all calendars
68         return False
69
70     def get_schedule_inbox_URL(self, cr, uid, ids, context=None):
71         calendar_obj = self.pool.get('basic.calendar')
72
73         calendar_ids = calendar_obj.search(cr, uid, [
74             ('user_id', '=', uid), ('collection_id', 'in', ids)
75             ], limit=1, context=context)
76
77         root_cal_dir = self._get_root_calendar_directory(cr, uid, context=context)
78         if not calendar_ids:
79             return root_cal_dir
80         calendar_id = calendar_ids[0]
81         calendar = calendar_obj.browse(cr, uid, calendar_id,
82                 context=context)
83         return '%s/%s' %(root_cal_dir, calendar.name)
84
85     def get_schedule_outbox_URL(self, cr, uid, ids, context=None):
86         return self.get_schedule_inbox_URL(cr, uid, ids, context=context)
87     
88 calendar_collection()