annotate src/pages/common/blog/page_meta.py @ 932:af6a62e21053

pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
author Goffi <goffi@goffi.org>
date Mon, 17 Apr 2017 20:43:12 +0200
parents ee243d48100e
children e4c13a995e0b
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
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
7 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
8 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
9 import urllib
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
10 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
11
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
12 """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
13 name = u'blog'
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
14 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
15 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
16
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 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
19 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
20 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
21 return service + u'/' + node
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
22
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 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
25 """URL is /[service]/[node]
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
26
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
27 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
28 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
29 """
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
30 data = self.getRData(request)
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
31
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
32 try:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
33 service = self.nextPath(request)
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
34 except IndexError:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
35 data['service'] = u''
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
36 else:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
37 try:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
38 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
39 except Exception:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
40 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
41 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
42
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
43 try:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
44 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
45 except IndexError:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
46 data['node'] = u''
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
47
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 @defer.inlineCallbacks
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
50 def appendComments(self, blog_items, profile):
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
51 for blog_item in blog_items:
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 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
53 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
54 node = comment_data[u'node']
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
55 comments_data = yield self.host.bridge.mbGet(
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
56 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
57 node,
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 C.NO_LIMIT,
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 [],
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
60 {},
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
61 profile)
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
62 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
63 blog_item.appendCommentsItems(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
64 yield appendComments(self, comments, profile)
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
65
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
66
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
67 @defer.inlineCallbacks
928
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
68 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
69 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
70 # 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
71 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
72 profile = self.getProfile(request)
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
73 if profile is None:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
74 profile = C.SERVICE_PROFILE
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
75
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
76 try:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
77 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
78 service.userhost(),
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
79 node,
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
80 10,
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
81 [],
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
82 {},
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
83 profile)
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
84 except Exception as e:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
85 # 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
86 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
87 self.pageError(request, 401)
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
88 else:
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
89 raise e
ee243d48100e pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff changeset
90
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
91 items = data_objects.BlogItems(blog_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
92
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
93 if show_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
94 yield appendComments(self, items, profile)
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
95
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
96 template_data = request.template_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
97 template_data[u'items'] = items