Merge branch 'master' of https://github.com/odoo/odoo
[odoo/odoo.git] / addons / calendar / controllers / main.py
1 import simplejson
2 import openerp
3 import openerp.http as http
4 from openerp.http import request
5 import openerp.addons.web.controllers.main as webmain
6 import json
7
8
9 class meeting_invitation(http.Controller):
10
11     @http.route('/calendar/meeting/accept', type='http', auth="calendar")
12     def accept(self, db, token, action, id, **kwargs):
13         registry = openerp.modules.registry.RegistryManager.get(db)
14         attendee_pool = registry.get('calendar.attendee')
15         with registry.cursor() as cr:
16             attendee_id = attendee_pool.search(cr, openerp.SUPERUSER_ID, [('access_token', '=', token), ('state', '!=', 'accepted')])
17             if attendee_id:
18                 attendee_pool.do_accept(cr, openerp.SUPERUSER_ID, attendee_id)
19         return self.view(db, token, action, id, view='form')
20
21     @http.route('/calendar/meeting/decline', type='http', auth="calendar")
22     def declined(self, db, token, action, id):
23         registry = openerp.modules.registry.RegistryManager.get(db)
24         attendee_pool = registry.get('calendar.attendee')
25         with registry.cursor() as cr:
26             attendee_id = attendee_pool.search(cr, openerp.SUPERUSER_ID, [('access_token', '=', token), ('state', '!=', 'declined')])
27             if attendee_id:
28                 attendee_pool.do_decline(cr, openerp.SUPERUSER_ID, attendee_id)
29         return self.view(db, token, action, id, view='form')
30
31     @http.route('/calendar/meeting/view', type='http', auth="calendar")
32     def view(self, db, token, action, id, view='calendar'):
33         registry = openerp.modules.registry.RegistryManager.get(db)
34         meeting_pool = registry.get('calendar.event')
35         attendee_pool = registry.get('calendar.attendee')
36         partner_pool = registry.get('res.partner')
37         with registry.cursor() as cr:
38             attendee = attendee_pool.search_read(cr, openerp.SUPERUSER_ID, [('access_token', '=', token)], [])
39
40             if attendee and attendee[0] and attendee[0].get('partner_id'):
41                 partner_id = int(attendee[0].get('partner_id')[0])
42                 tz = partner_pool.read(cr, openerp.SUPERUSER_ID, partner_id, ['tz'])['tz']
43             else:
44                 tz = False
45
46             attendee_data = meeting_pool.get_attendee(cr, openerp.SUPERUSER_ID, id, dict(tz=tz))
47
48         if attendee:
49             attendee_data['current_attendee'] = attendee[0]
50
51         values = dict(init="s.calendar.event('%s', '%s', '%s', '%s' , '%s');" % (db, action, id, 'form', json.dumps(attendee_data)))
52         return request.render('web.webclient_bootstrap', values)
53
54     # Function used, in RPC to check every 5 minutes, if notification to do for an event or not
55     @http.route('/calendar/notify', type='json', auth="none")
56     def notify(self):
57         registry = request.registry
58         uid = request.session.uid
59         context = request.session.context
60         with registry.cursor() as cr:
61             res = registry.get("calendar.alarm_manager").get_next_notif(cr, uid, context=context)
62             return res
63
64     @http.route('/calendar/notify_ack', type='json', auth="none")
65     def notify_ack(self, type=''):
66         registry = request.registry
67         uid = request.session.uid
68         context = request.session.context
69         with registry.cursor() as cr:
70             res = registry.get("res.partner")._set_calendar_last_notif_ack(cr, uid, context=context)
71             return res