Mercurial > libervia-web
annotate src/pages/common/blog/page_meta.py @ 1016:fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
- item_id can now be specified in URL parsing, and a filter_keyword is used so future variants can be implemented (e.g. tags)
- if item_id is specified, rsm is discarded and only the requested item is retrieved
- new template data "items_http_uri" is filled, with links to HTTP versions of items
- blog_new is now a simple redirection to common/blog as the later should be generic enough to handle all use cases.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 19 Jan 2018 18:09:39 +0100 |
parents | fe08a5c95b27 |
children | 2ae3b6291456 |
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): |
1016
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
26 """URL is /[service]/[node]/[filter_keyword]/[item]|[other] |
928
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
1016
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
28 if [node] is '@', default namespace is used |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
29 if a value is unset, default one will be used |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
30 keyword can be one of: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
31 id: next value is a item id |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
32 tag: next value is a blog tag |
928
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 """ |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 data = self.getRData(request) |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 try: |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 service = self.nextPath(request) |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 except IndexError: |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 data['service'] = u'' |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 else: |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 try: |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 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
|
43 except Exception: |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 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
|
45 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
|
46 |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 try: |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 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
|
49 except IndexError: |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 data['node'] = u'' |
1016
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
51 else: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
52 if data['node'] == u'@': |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
53 data['node'] = u'' |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
54 |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
55 try: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
56 filter_kw = data['filter_keyword'] = self.nextPath(request) |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
57 except IndexError: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
58 pass |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
59 else: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
60 if filter_kw == u'id': |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
61 try: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
62 data['item'] = self.nextPath(request) |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
63 except IndexError: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
64 self.pageError(request, C.HTTP_BAD_REQUEST) |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
65 else: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
66 # invalid filter keyword |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
67 log.warning(_(u"invalid filter keyword: {filter_kw}").format(filter_kw=filter_kw)) |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
68 self.pageError(request, C.HTTP_BAD_REQUEST) |
928
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 @defer.inlineCallbacks |
947
92f0eeb6dc72
pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents:
939
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 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
|
76 if author not in identities: |
996
d821c112e656
pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
949
diff
changeset
|
77 identities[author] = yield self.host.bridgeCall(u'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
|
78 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
|
79 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
|
80 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
|
81 try: |
c20ac29d869f
pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents:
947
diff
changeset
|
82 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
|
83 service, |
c20ac29d869f
pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents:
947
diff
changeset
|
84 node, |
c20ac29d869f
pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents:
947
diff
changeset
|
85 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
|
86 [], |
c20ac29d869f
pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents:
947
diff
changeset
|
87 {}, |
c20ac29d869f
pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents:
947
diff
changeset
|
88 profile) |
c20ac29d869f
pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents:
947
diff
changeset
|
89 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
|
90 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
|
91 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
|
92 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
|
93 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
|
94 continue |
c20ac29d869f
pages (common/blog): don't fail if an error is raised while getting comment
Goffi <goffi@goffi.org>
parents:
947
diff
changeset
|
95 |
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
|
96 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
|
97 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
|
98 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
|
99 |
1011
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
100 @defer.inlineCallbacks |
1016
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
101 def getBlogData(self, request, service, node, item_id, extra, profile): |
1011
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
102 try: |
1016
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
103 if item_id: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
104 items_id = [item_id] |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
105 else: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
106 items_id = [] |
1011
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
107 blog_data = yield self.host.bridge.mbGet( |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
108 service.userhost(), |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
109 node, |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
110 C.NO_LIMIT, |
1016
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
111 items_id, |
1011
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
112 extra, |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
113 profile) |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
114 except Exception as e: |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
115 # FIXME: need a better way to test errors in bridge errback |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
116 if u"forbidden" in unicode(e): |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
117 self.pageError(request, 401) |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
118 else: |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
119 raise e |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
120 |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
121 items = data_objects.BlogItems(blog_data) |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
122 defer.returnValue((blog_data, items)) |
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
|
123 |
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
|
124 @defer.inlineCallbacks |
928
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 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
|
126 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
|
127 # if the comments are not explicitly hidden, we show them |
1016
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
128 service, node, item_id, show_comments = data.get(u'service', u''), data.get(u'node', u''), data.get(u'item'), 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
|
129 profile = self.getProfile(request) |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 if profile is None: |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 profile = C.SERVICE_PROFILE |
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 |
1011
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
133 params = self.getAllPostedData(request, multiple=False) |
1016
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
134 if item_id: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
135 extra = {} |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
136 else: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
137 extra = {u'rsm_max': u'10'} |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
138 if u'after' in params: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
139 extra[u'rsm_after'] = params[u'after'] |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
140 elif u'before' in params: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
141 extra[u'rsm_before'] = params[u'before'] |
928
ee243d48100e
pages (common): added common hierarchy for reusable pages (blog is the first)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 |
1016
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
143 blog_data, items = yield getBlogData(self, request, service, node, item_id, extra, profile) |
947
92f0eeb6dc72
pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents:
939
diff
changeset
|
144 template_data = request.template_data |
1011
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
145 if items: |
1016
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
146 if not item_id: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
147 last_id = items[-1].id |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
148 template_data['older_url'] = self.getParamURL(request, after=last_id) |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
149 if u'before' in params or u'after' in params: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
150 first_id = items[0].id |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
151 template_data['newer_url'] = self.getParamURL(request, before=first_id) |
1011
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
152 else: |
1016
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
153 if item_id: |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
154 # if item id has been specified in URL and it's not found, |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
155 # we must return an error |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
156 self.pageError(request, C.HTTP_NOT_FOUND) |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
157 |
1011
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
158 # no items, we have requested items before last post, or blog is empty |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
159 extra = {u'rsm_max': u'10'} |
1016
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
160 blog_data, items = yield getBlogData(self, request, service, node, None, extra, profile) |
1011
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
161 if items: |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
162 last_id = items[-1].id |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
163 template_data['older_url'] = self.getParamURL(request, after=last_id) |
fe08a5c95b27
pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
Goffi <goffi@goffi.org>
parents:
996
diff
changeset
|
164 |
947
92f0eeb6dc72
pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents:
939
diff
changeset
|
165 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
|
166 |
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
|
167 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
|
168 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
|
169 |
1016
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
170 blog_view = self.getPageByName(u'blog_view') |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
171 template_data[u'items'] = data[u'items'] = items |
fc1c913cc9d1
pages (blog_new, common/blog): various blog improvments:
Goffi <goffi@goffi.org>
parents:
1011
diff
changeset
|
172 template_data[u'items_http_uri'] = {item.id: self.host.getExtBaseURL(request, blog_view.getURL(service.full(), node or '@', u'id', item.id)) for item in items} |
949
36e9747520fd
pages (common/blog): use request data to indicate if comments are allowed
Goffi <goffi@goffi.org>
parents:
948
diff
changeset
|
173 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
|
174 |
e4c13a995e0b
pages (common/blog): data post (i.e. new comments) is now handled
Goffi <goffi@goffi.org>
parents:
932
diff
changeset
|
175 |
e4c13a995e0b
pages (common/blog): data post (i.e. new comments) is now handled
Goffi <goffi@goffi.org>
parents:
932
diff
changeset
|
176 @defer.inlineCallbacks |
e4c13a995e0b
pages (common/blog): data post (i.e. new comments) is now handled
Goffi <goffi@goffi.org>
parents:
932
diff
changeset
|
177 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
|
178 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
|
179 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
|
180 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
|
181 type_ = self.getPostedData(request, u'type') |
1375b96f4309
pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents:
933
diff
changeset
|
182 if type_ == u'comment': |
1375b96f4309
pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents:
933
diff
changeset
|
183 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
|
184 |
1375b96f4309
pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents:
933
diff
changeset
|
185 if not body: |
1375b96f4309
pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents:
933
diff
changeset
|
186 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
|
187 comment_data = {u"content": body} |
1375b96f4309
pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents:
933
diff
changeset
|
188 try: |
1375b96f4309
pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents:
933
diff
changeset
|
189 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
|
190 except Exception as e: |
1375b96f4309
pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents:
933
diff
changeset
|
191 if u"forbidden" in unicode(e): |
1375b96f4309
pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents:
933
diff
changeset
|
192 self.pageError(request, 401) |
1375b96f4309
pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents:
933
diff
changeset
|
193 else: |
1375b96f4309
pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents:
933
diff
changeset
|
194 raise e |
1375b96f4309
pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents:
933
diff
changeset
|
195 else: |
1375b96f4309
pages (common/blog): check posted data type for comments
Goffi <goffi@goffi.org>
parents:
933
diff
changeset
|
196 log.warning(_(u"Unhandled data type: {}").format(type_)) |