Mercurial > libervia-web
annotate src/pages/g/e/page_meta.py @ 1019:34240d08f682
pages: HTTP cache headers handling:
when checkCache is used, HTTP headers handling cache are now used:
- ETag is first checked, using a hash of the rendered content
- Last-Modified is used as a fallback is client is not handling ETag
When suitable, a HTTP 304 code (Not Modified) wihtout body is returned instead of the whole page.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 21 Jan 2018 13:14:06 +0100 |
parents | 36e9747520fd |
children | ad97d7e7de3b |
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 |
946
d076b722ca52
pages (g/e): added days left before the event as a template variable
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
10 import time |
940
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 from sat.core.log import getLogger |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 log = getLogger('pages/g/e') |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 access = C.PAGES_ACCESS_PROFILE |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 template = u"event/invitation.html" |
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 |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 @defer.inlineCallbacks |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 def prepare_render(self, request): |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 template_data = request.template_data |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 guest_session = self.host.getSessionData(request, session_iface.ISATGuestSession) |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 try: |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 event_uri = guest_session.data['event_uri'] |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 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
|
25 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
|
26 self.pageError(request, C.HTTP_SERVICE_UNAVAILABLE) |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 data = self.getRData(request) |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 ## Event ## |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 event_uri_data = uri.parseXMPPUri(event_uri) |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 if event_uri_data[u'type'] != u'pubsub': |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 self.pageError(request, C.HTTP_SERVICE_UNAVAILABLE) |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 event_service = template_data[u'event_service'] = jid.JID(event_uri_data[u'path']) |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 event_node = template_data[u'event_node'] = event_uri_data[u'node'] |
942
6b55d038b121
pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents:
940
diff
changeset
|
38 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
|
39 profile = self.getProfile(request) |
942
6b55d038b121
pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents:
940
diff
changeset
|
40 event_timestamp, event_data = yield self.host.bridgeCall(u"eventGet", event_service.userhost(), event_node, event_id, profile) |
6b55d038b121
pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents:
940
diff
changeset
|
41 try: |
6b55d038b121
pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents:
940
diff
changeset
|
42 background_image = event_data.pop('background-image') |
6b55d038b121
pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents:
940
diff
changeset
|
43 except KeyError: |
6b55d038b121
pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents:
940
diff
changeset
|
44 pass |
6b55d038b121
pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents:
940
diff
changeset
|
45 else: |
6b55d038b121
pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents:
940
diff
changeset
|
46 template_data['background_image'] = background_image |
940
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 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
|
48 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
|
49 u"eventInviteeGet", |
6b55d038b121
pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents:
940
diff
changeset
|
50 event_data['invitees_service'], |
6b55d038b121
pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents:
940
diff
changeset
|
51 event_data['invitees_node'], |
6b55d038b121
pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents:
940
diff
changeset
|
52 profile) |
6b55d038b121
pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents:
940
diff
changeset
|
53 template_data['invitee'] = event_invitee_data |
946
d076b722ca52
pages (g/e): added days left before the event as a template variable
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
54 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
|
55 |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 ## Blog ## |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 |
943
aacda981c348
pages (g/e): use blog_uri from event instead of guest's main uri
Goffi <goffi@goffi.org>
parents:
942
diff
changeset
|
58 data[u'service'] = jid.JID(event_data[u'blog_service']) |
aacda981c348
pages (g/e): use blog_uri from event instead of guest's main uri
Goffi <goffi@goffi.org>
parents:
942
diff
changeset
|
59 data[u'node'] = event_data[u'blog_node'] |
949
36e9747520fd
pages (common/blog): use request data to indicate if comments are allowed
Goffi <goffi@goffi.org>
parents:
946
diff
changeset
|
60 data[u'allow_commenting'] = u'simple' |
940
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 # we now need blog items, using blog common page |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 # this will fill the "items" template data |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 blog_page = self.getPageByName(u'blog') |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 yield blog_page.prepare_render(self, request) |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 @defer.inlineCallbacks |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 def on_data_post(self, request): |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 type_ = self.getPostedData(request, u'type') |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 if type_ == u'comment': |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 blog_page = self.getPageByName(u'blog') |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 yield blog_page.on_data_post(self, request) |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 elif type_ == u'attendance': |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 profile = self.getProfile(request) |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 service, node, attend, guests = self.getPostedData(request, (u'service', u'node', u'attend', u'guests')) |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 data = {u'attend': attend, |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 u'guests': guests} |
942
6b55d038b121
pages (g/e): adapted event handling to changes in backend
Goffi <goffi@goffi.org>
parents:
940
diff
changeset
|
78 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
|
79 else: |
e30a4b7eff09
page (g/e): events invitations first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 log.warning(_(u"Unhandled data type: {}").format(type_)) |