comparison src/pages/g/page_meta.py @ 930:b5490fa65348

pages (g): added g (for guest) page to handle invitations
author Goffi <goffi@goffi.org>
date Sun, 16 Apr 2017 18:26:31 +0200
parents
children 9be057e23ce6
comparison
equal deleted inserted replaced
929:2345577da5ca 930:b5490fa65348
1 #!/usr/bin/env python2.7
2 # -*- coding: utf-8 -*-
3
4 from libervia.server.constants import Const as C
5 from sat.core.i18n import _
6 from twisted.internet import defer
7 from libervia.server import session_iface
8 from sat.core.log import getLogger
9 log = getLogger('pages/g')
10
11 access = C.PAGES_ACCESS_PUBLIC
12 template = u"invitation/welcome.html"
13
14
15 @defer.inlineCallbacks
16 def parse_url(self, request):
17 """check invitation id in URL and start session if needed
18
19 if a session already exists for an other guest/profile, it will be purged
20 """
21 try:
22 invitation_id = self.nextPath(request)
23 except IndexError:
24 self.pageError(request)
25
26 sat_session, guest_session = self.host.getSessionData(request, session_iface.ISATSession, session_iface.ISATGuestSession)
27 current_id = guest_session.id
28
29 if current_id is not None and current_id != invitation_id:
30 log.info(_(u'killing guest session [{old_id}] because it is connecting with an other ID [{new_id}]').format(
31 old_id = current_id,
32 new_id = invitation_id))
33 self.host.purgeSession(request)
34 sat_session, guest_session = self.host.getSessionData(request, session_iface.ISATSession, session_iface.ISATGuestSession)
35 current_id = None # FIXME: id non mis à zéro ici
36 profile = None
37
38 profile = sat_session.profile
39 if profile is not None and current_id is None:
40 log.info(_(u'killing current profile session [{profile}] because a guest id is used').format(
41 profile = profile))
42 self.host.purgeSession(request)
43 sat_session, guest_session = self.host.getSessionData(request, session_iface.ISATSession, session_iface.ISATGuestSession)
44 profile = None
45
46 if current_id is None:
47 log.debug(_(u"checking invitation [{id}]").format(id=invitation_id))
48 try:
49 data = yield self.host.bridge.invitationGet(invitation_id)
50 except Exception:
51 self.pageError(request, 401)
52 else:
53 guest_session.id = invitation_id
54 guest_session.data = data
55 else:
56 data = guest_session.data
57
58 if profile is None:
59 log.debug(_(u"connecting profile [{}]").format(profile))
60 # we need to connect the profile
61 profile = data['guest_profile']
62 password = data['password']
63 try:
64 yield self.host.bridge.connect(profile, password, {})
65 except Exception as e:
66 log.warning(_(u"Can't connect profile: {msg}").format(
67 msg=e))
68 # FIXME: no good error code correspond
69 # maybe use a custom one?
70 self.pageError(request, code=503)
71
72 log.info(_(u"guest session started, connected with profile [{profile}]".format(
73 profile = profile)))
74 sat_session.profile = profile
75
76
77 def prepare_render(self, request):
78 template_data = request.template_data
79 guest_session = self.host.getSessionData(request, session_iface.ISATGuestSession)
80 main_uri = guest_session.data.get('main_uri')
81 template_data[u"include_url"] = self.getPagePathFromURI(main_uri)