annotate src/pages/g/page_meta.py @ 995:f88325b56a6a

server: dynamic pages first draft: /!\ new dependency: autobahn This patch introduce server part of dynamic pages. Dynamic pages use websockets to establish constant connection with a Libervia page, allowing to receive real time data or update it. The feature is activated by specifying "dynamic = true" in the page. Once activated, page can implement "on_data" method which will be called when data are sent by the page. To send data the other way, the page can use request.sendData. The new "registerSignal" method allows to use an "on_signal" method to be called each time given signal is received, with automatic (and optional) filtering on profile. New renderPartial and renderAndUpdate method allow to append new HTML elements to the dynamic page.
author Goffi <goffi@goffi.org>
date Wed, 03 Jan 2018 01:10:12 +0100
parents 5cdd77190a3b
children 01e95ec9df9e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
930
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2.7
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
3
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from libervia.server.constants import Const as C
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
5 from sat.core.i18n import _
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
6 from twisted.internet import defer
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
7 from libervia.server import session_iface
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
8 from sat.core.log import getLogger
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
9 log = getLogger('pages/g')
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
10
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
11 access = C.PAGES_ACCESS_PUBLIC
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
12 template = u"invitation/welcome.html"
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
13
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
14
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
15 @defer.inlineCallbacks
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
16 def parse_url(self, request):
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
17 """check invitation id in URL and start session if needed
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
18
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
19 if a session already exists for an other guest/profile, it will be purged
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
21 try:
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
22 invitation_id = self.nextPath(request)
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
23 except IndexError:
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
24 self.pageError(request)
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
25
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
26 sat_session, guest_session = self.host.getSessionData(request, session_iface.ISATSession, session_iface.ISATGuestSession)
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
27 current_id = guest_session.id
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
28
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
29 if current_id is not None and current_id != invitation_id:
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
30 log.info(_(u'killing guest session [{old_id}] because it is connecting with an other ID [{new_id}]').format(
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
31 old_id = current_id,
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
32 new_id = invitation_id))
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
33 self.host.purgeSession(request)
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
34 sat_session, guest_session = self.host.getSessionData(request, session_iface.ISATSession, session_iface.ISATGuestSession)
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
35 current_id = None # FIXME: id non mis à zéro ici
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
36 profile = None
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
37
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
38 profile = sat_session.profile
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
39 if profile is not None and current_id is None:
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
40 log.info(_(u'killing current profile session [{profile}] because a guest id is used').format(
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
41 profile = profile))
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
42 self.host.purgeSession(request)
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
43 sat_session, guest_session = self.host.getSessionData(request, session_iface.ISATSession, session_iface.ISATGuestSession)
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
44 profile = None
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
45
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
46 if current_id is None:
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
47 log.debug(_(u"checking invitation [{id}]").format(id=invitation_id))
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
48 try:
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
49 data = yield self.host.bridge.invitationGet(invitation_id)
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
50 except Exception:
938
9be057e23ce6 pages (g): use new constants
Goffi <goffi@goffi.org>
parents: 930
diff changeset
51 self.pageError(request, C.HTTP_UNAUTHORIZED)
930
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
52 else:
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
53 guest_session.id = invitation_id
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
54 guest_session.data = data
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
55 else:
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
56 data = guest_session.data
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
57
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
58 if profile is None:
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
59 log.debug(_(u"connecting profile [{}]").format(profile))
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
60 # we need to connect the profile
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
61 profile = data['guest_profile']
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
62 password = data['password']
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
63 try:
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
64 yield self.host.bridge.connect(profile, password, {})
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
65 except Exception as e:
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
66 log.warning(_(u"Can't connect profile: {msg}").format(
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
67 msg=e))
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
68 # FIXME: no good error code correspond
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
69 # maybe use a custom one?
938
9be057e23ce6 pages (g): use new constants
Goffi <goffi@goffi.org>
parents: 930
diff changeset
70 self.pageError(request, code=C.HTTP_SERVICE_UNAVAILABLE)
930
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
71
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
72 log.info(_(u"guest session started, connected with profile [{profile}]".format(
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
73 profile = profile)))
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
74 sat_session.profile = profile
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
75
941
aba7208d9d50 pages (g): guest name is added to template data
Goffi <goffi@goffi.org>
parents: 938
diff changeset
76 # we copy data useful in templates
aba7208d9d50 pages (g): guest name is added to template data
Goffi <goffi@goffi.org>
parents: 938
diff changeset
77 template_data = request.template_data
953
5cdd77190a3b pages(g): use norobots in guest pages
Goffi <goffi@goffi.org>
parents: 951
diff changeset
78 template_data['norobots'] = True
951
43df2bf38787 pages (g): use language in data for locale
Goffi <goffi@goffi.org>
parents: 945
diff changeset
79 if u'name' in data:
43df2bf38787 pages (g): use language in data for locale
Goffi <goffi@goffi.org>
parents: 945
diff changeset
80 template_data[u'name'] = data[u'name']
43df2bf38787 pages (g): use language in data for locale
Goffi <goffi@goffi.org>
parents: 945
diff changeset
81 if u'language' in data:
43df2bf38787 pages (g): use language in data for locale
Goffi <goffi@goffi.org>
parents: 945
diff changeset
82 template_data[u'locale'] = data[u'language']
941
aba7208d9d50 pages (g): guest name is added to template data
Goffi <goffi@goffi.org>
parents: 938
diff changeset
83
930
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
84
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
85 def prepare_render(self, request):
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
86 template_data = request.template_data
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
87 guest_session = self.host.getSessionData(request, session_iface.ISATGuestSession)
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
88 main_uri = guest_session.data.get('main_uri')
b5490fa65348 pages (g): added g (for guest) page to handle invitations
Goffi <goffi@goffi.org>
parents:
diff changeset
89 template_data[u"include_url"] = self.getPagePathFromURI(main_uri)