annotate libervia/pages/g/e/page_meta.py @ 1125:56ace2d45783

server (twisted): install reactor before doing anything else
author Goffi <goffi@goffi.org>
date Fri, 31 Aug 2018 18:05:15 +0200
parents 28e3eb3bb217
children 29eb15062416
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
940
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2.7
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from libervia.server.constants import Const as C
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 from sat.core.i18n import _
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 from twisted.internet import defer
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7 from twisted.words.protocols.jabber import jid
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 from libervia.server import session_iface
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 from sat.tools.common import uri
1028
ad97d7e7de3b pages (g/e): replaced background_image by new dynamic_style template variable
Goffi <goffi@goffi.org>
parents: 949
diff changeset
10 from sat.tools.common.template import safe
946
d076b722ca52 pages (g/e): added days left before the event as a template variable
Goffi <goffi@goffi.org>
parents: 943
diff changeset
11 import time
1028
ad97d7e7de3b pages (g/e): replaced background_image by new dynamic_style template variable
Goffi <goffi@goffi.org>
parents: 949
diff changeset
12 import cgi
940
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 from sat.core.log import getLogger
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
14
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
15 log = getLogger("pages/g/e")
940
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17 access = C.PAGES_ACCESS_PROFILE
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 template = u"event/invitation.html"
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21 @defer.inlineCallbacks
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 def prepare_render(self, request):
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 template_data = request.template_data
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 guest_session = self.host.getSessionData(request, session_iface.ISATGuestSession)
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 try:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
26 event_uri = guest_session.data["event_uri"]
940
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 except KeyError:
943
aacda981c348 pages (g/e): use blog_uri from event instead of guest's main uri
Goffi <goffi@goffi.org>
parents: 942
diff changeset
28 log.warning(_(u"event URI not found, can't render event page"))
940
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 self.pageError(request, C.HTTP_SERVICE_UNAVAILABLE)
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 data = self.getRData(request)
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 ## Event ##
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 event_uri_data = uri.parseXMPPUri(event_uri)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
36 if event_uri_data[u"type"] != u"pubsub":
940
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 self.pageError(request, C.HTTP_SERVICE_UNAVAILABLE)
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
39 event_service = template_data[u"event_service"] = jid.JID(event_uri_data[u"path"])
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
40 event_node = template_data[u"event_node"] = event_uri_data[u"node"]
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
41 event_id = template_data[u"event_id"] = event_uri_data.get(u"item", "")
940
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 profile = self.getProfile(request)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
43 event_timestamp, event_data = yield self.host.bridgeCall(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
44 u"eventGet", event_service.userhost(), event_node, event_id, profile
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
45 )
942
6b55d038b121 pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents: 940
diff changeset
46 try:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
47 background_image = event_data.pop("background-image")
942
6b55d038b121 pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents: 940
diff changeset
48 except KeyError:
6b55d038b121 pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents: 940
diff changeset
49 pass
6b55d038b121 pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents: 940
diff changeset
50 else:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
51 template_data["dynamic_style"] = safe(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
52 u"""
1028
ad97d7e7de3b pages (g/e): replaced background_image by new dynamic_style template variable
Goffi <goffi@goffi.org>
parents: 949
diff changeset
53 html {
ad97d7e7de3b pages (g/e): replaced background_image by new dynamic_style template variable
Goffi <goffi@goffi.org>
parents: 949
diff changeset
54 background-image: url("%s");
ad97d7e7de3b pages (g/e): replaced background_image by new dynamic_style template variable
Goffi <goffi@goffi.org>
parents: 949
diff changeset
55 background-size: 15em;
ad97d7e7de3b pages (g/e): replaced background_image by new dynamic_style template variable
Goffi <goffi@goffi.org>
parents: 949
diff changeset
56 }
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
57 """
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
58 % cgi.escape(background_image, True)
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
59 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
60 template_data["event"] = event_data
942
6b55d038b121 pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents: 940
diff changeset
61 event_invitee_data = yield self.host.bridgeCall(
6b55d038b121 pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents: 940
diff changeset
62 u"eventInviteeGet",
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
63 event_data["invitees_service"],
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
64 event_data["invitees_node"],
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
65 profile,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
66 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
67 template_data["invitee"] = event_invitee_data
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
68 template_data["days_left"] = int((event_timestamp - time.time()) / (60 * 60 * 24))
940
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 ## Blog ##
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
72 data[u"service"] = jid.JID(event_data[u"blog_service"])
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
73 data[u"node"] = event_data[u"blog_node"]
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
74 data[u"allow_commenting"] = u"simple"
940
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 # we now need blog items, using blog common page
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 # this will fill the "items" template data
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
78 blog_page = self.getPageByName(u"blog_view")
940
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 yield blog_page.prepare_render(self, request)
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
81
940
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 @defer.inlineCallbacks
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 def on_data_post(self, request):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
84 type_ = self.getPostedData(request, u"type")
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
85 if type_ == u"comment":
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
86 blog_page = self.getPageByName(u"blog_view")
940
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 yield blog_page.on_data_post(self, request)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
88 elif type_ == u"attendance":
940
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89 profile = self.getProfile(request)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
90 service, node, attend, guests = self.getPostedData(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
91 request, (u"service", u"node", u"attend", u"guests")
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
92 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1085
diff changeset
93 data = {u"attend": attend, u"guests": guests}
942
6b55d038b121 pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents: 940
diff changeset
94 yield self.host.bridgeCall(u"eventInviteeSet", service, node, data, profile)
940
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95 else:
e30a4b7eff09 page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 log.warning(_(u"Unhandled data type: {}").format(type_))