annotate src/pages/common/blog/page_meta.py @ 979:1d558dfb32ca

server: pages redirection: when using a redirection dict, a new "page" key can be used to redirect to a named page. "args" can be added to specified named arguments to set (will be put in request.args, in addition to existing ones). The redirection is done dynamically, during the request workflow.
author Goffi <goffi@goffi.org>
date Sun, 12 Nov 2017 12:56:46 +0100
parents 36e9747520fd
children d821c112e656
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
928
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2.7
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
3 from libervia.server.constants import Const as C
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from twisted.words.protocols.jabber import jid
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
5 from twisted.internet import defer
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
6 from sat.tools.common import data_objects
947
92f0eeb6dc72 pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents: 939
diff changeset
7 from libervia.server import session_iface
928
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
8 from sat.core.i18n import _
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
9 from sat.core.log import getLogger
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
10 import urllib
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
11 log = getLogger('pages/common/blog')
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
12
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
13 """generic blog (with service/node provided)"""
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
14 name = u'blog'
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
15 template = u"blog/articles.html"
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
16 uri_handlers = {(u'pubsub', u'microblog'): 'microblog_uri'}
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
17
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
18
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
19 def microblog_uri(self, uri_data):
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
20 service = urllib.quote_plus(uri_data[u'path'])
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
21 node = urllib.quote_plus(uri_data[u'node'])
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
22 return service + u'/' + node
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
23
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
24
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
25 def parse_url(self, request):
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
26 """URL is /[service]/[node]
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
27
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
28 if [node] is not found, default namespace is used
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
29 if both [service] and [node] are not found, default service is used too
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
30 """
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
31 data = self.getRData(request)
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
32
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
33 try:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
34 service = self.nextPath(request)
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
35 except IndexError:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
36 data['service'] = u''
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
37 else:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
38 try:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
39 data[u"service"] = jid.JID(service)
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
40 except Exception:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
41 log.warning(_(u"bad service entered: {}").format(service))
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
42 self.pageError(request, C.HTTP_BAD_REQUEST)
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
43
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
44 try:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
45 data['node'] = self.nextPath(request)
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
46 except IndexError:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
47 data['node'] = u''
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
48
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
49
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
50 @defer.inlineCallbacks
947
92f0eeb6dc72 pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents: 939
diff changeset
51 def appendComments(self, blog_items, identities, profile):
932
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
52 for blog_item in blog_items:
947
92f0eeb6dc72 pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents: 939
diff changeset
53 if identities is not None:
92f0eeb6dc72 pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents: 939
diff changeset
54 author = blog_item.author_jid
92f0eeb6dc72 pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents: 939
diff changeset
55 if author not in identities:
92f0eeb6dc72 pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents: 939
diff changeset
56 identities[author] = yield self.host.bridge.identityGet(author, profile)
932
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
57 for comment_data in blog_item.comments:
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
58 service = comment_data[u'service']
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
59 node = comment_data[u'node']
948
c20ac29d869f pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents: 947
diff changeset
60 try:
c20ac29d869f pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents: 947
diff changeset
61 comments_data = yield self.host.bridge.mbGet(
c20ac29d869f pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents: 947
diff changeset
62 service,
c20ac29d869f pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents: 947
diff changeset
63 node,
c20ac29d869f pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents: 947
diff changeset
64 C.NO_LIMIT,
c20ac29d869f pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents: 947
diff changeset
65 [],
c20ac29d869f pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents: 947
diff changeset
66 {},
c20ac29d869f pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents: 947
diff changeset
67 profile)
c20ac29d869f pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents: 947
diff changeset
68 except Exception as e:
c20ac29d869f pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents: 947
diff changeset
69 log.warning(_(u"Can't get comments at {node} (service: {service}): {msg}").format(
c20ac29d869f pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents: 947
diff changeset
70 service=service,
c20ac29d869f pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents: 947
diff changeset
71 node=node,
c20ac29d869f pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents: 947
diff changeset
72 msg=e))
c20ac29d869f pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents: 947
diff changeset
73 continue
c20ac29d869f pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents: 947
diff changeset
74
932
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
75 comments = data_objects.BlogItems(comments_data)
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
76 blog_item.appendCommentsItems(comments)
947
92f0eeb6dc72 pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents: 939
diff changeset
77 yield appendComments(self, comments, identities, profile)
932
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
78
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
79
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
80 @defer.inlineCallbacks
928
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
81 def prepare_render(self, request):
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
82 data = self.getRData(request)
932
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
83 # if the comments are not explicitly hidden, we show them
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
84 service, node, show_comments = data.get(u'service', u''), data.get(u'node', u''), data.get(u'show_comments', True)
928
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
85 profile = self.getProfile(request)
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
86 if profile is None:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
87 profile = C.SERVICE_PROFILE
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
88
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
89 try:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
90 blog_data = yield self.host.bridge.mbGet(
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
91 service.userhost(),
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
92 node,
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
93 10,
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
94 [],
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
95 {},
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
96 profile)
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
97 except Exception as e:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
98 # FIXME: need a better way to test errors in bridge errback
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
99 if u"forbidden" in unicode(e):
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
100 self.pageError(request, 401)
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
101 else:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
102 raise e
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
103
932
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
104 items = data_objects.BlogItems(blog_data)
947
92f0eeb6dc72 pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents: 939
diff changeset
105 template_data = request.template_data
92f0eeb6dc72 pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents: 939
diff changeset
106 identities = template_data[u'identities'] = self.host.getSessionData(request, session_iface.ISATSession).identities
932
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
107
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
108 if show_comments:
947
92f0eeb6dc72 pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents: 939
diff changeset
109 yield appendComments(self, items, identities, profile)
932
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
110
af6a62e21053 pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
Goffi <goffi@goffi.org>
parents: 928
diff changeset
111 template_data[u'items'] = items
949
36e9747520fd pages (common/blog): use request data to indicate if comments are allowed
Goffi <goffi@goffi.org>
parents: 948
diff changeset
112 template_data[u'allow_commenting'] = data.get(u'allow_commenting', False)
933
e4c13a995e0b pages (common/blog): data post (i.e. new comments) is now handled
Goffi <goffi@goffi.org>
parents: 932
diff changeset
113
e4c13a995e0b pages (common/blog): data post (i.e. new comments) is now handled
Goffi <goffi@goffi.org>
parents: 932
diff changeset
114
e4c13a995e0b pages (common/blog): data post (i.e. new comments) is now handled
Goffi <goffi@goffi.org>
parents: 932
diff changeset
115 @defer.inlineCallbacks
e4c13a995e0b pages (common/blog): data post (i.e. new comments) is now handled
Goffi <goffi@goffi.org>
parents: 932
diff changeset
116 def on_data_post(self, request):
e4c13a995e0b pages (common/blog): data post (i.e. new comments) is now handled
Goffi <goffi@goffi.org>
parents: 932
diff changeset
117 profile = self.getProfile(request)
e4c13a995e0b pages (common/blog): data post (i.e. new comments) is now handled
Goffi <goffi@goffi.org>
parents: 932
diff changeset
118 if profile is None:
e4c13a995e0b pages (common/blog): data post (i.e. new comments) is now handled
Goffi <goffi@goffi.org>
parents: 932
diff changeset
119 self.pageError(request, C.HTTP_UNAUTHORIZED)
939
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
120 type_ = self.getPostedData(request, u'type')
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
121 if type_ == u'comment':
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
122 service, node, body = self.getPostedData(request, (u'service', u'node', u'body'))
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
123
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
124 if not body:
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
125 self.pageError(request, C.HTTP_BAD_REQUEST)
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
126 comment_data = {u"content": body}
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
127 try:
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
128 yield self.host.bridge.mbSend(service, node, comment_data, profile)
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
129 except Exception as e:
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
130 if u"forbidden" in unicode(e):
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
131 self.pageError(request, 401)
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
132 else:
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
133 raise e
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
134 else:
1375b96f4309 pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents: 933
diff changeset
135 log.warning(_(u"Unhandled data type: {}").format(type_))