Mercurial > libervia-web
annotate libervia/pages/blog/view/page_meta.py @ 1318:38875bc292ff
pages: add `media_path` and `build_path` in template data for error pages.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 01 Aug 2020 16:47:24 +0200 |
parents | 04e7dd6b6f4d |
children | a0954b6610aa |
rev | line source |
---|---|
1216 | 1 #!/usr/bin/env python3 |
1239 | 2 |
1156
3048bd137aaf
server, browser: changed blog items serialisation following changes in backend
Goffi <goffi@goffi.org>
parents:
1145
diff
changeset
|
3 import unicodedata |
3048bd137aaf
server, browser: changed blog items serialisation following changes in backend
Goffi <goffi@goffi.org>
parents:
1145
diff
changeset
|
4 import re |
1229
acec8e9185db
pages: replaced deprecated `cgi.escape` by `html.escape`
Goffi <goffi@goffi.org>
parents:
1219
diff
changeset
|
5 import html |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 from libervia.server.constants import Const as C |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 from twisted.words.protocols.jabber import jid |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 from twisted.internet import defer |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 from sat.tools.common import data_objects |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 from libervia.server import session_iface |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 from sat.core.i18n import _ |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 from sat.tools.common.template import safe |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 from sat.tools.common import uri |
1156
3048bd137aaf
server, browser: changed blog items serialisation following changes in backend
Goffi <goffi@goffi.org>
parents:
1145
diff
changeset
|
14 from sat.tools.common import data_format |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 from libervia.server import utils |
1171
469d0de8da0e
pages (blog, u): added atom feed link in "links" template data.
Goffi <goffi@goffi.org>
parents:
1156
diff
changeset
|
16 from libervia.server.utils import SubPage |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 from sat.core.log import getLogger |
1156
3048bd137aaf
server, browser: changed blog items serialisation following changes in backend
Goffi <goffi@goffi.org>
parents:
1145
diff
changeset
|
18 |
1145
29eb15062416
pages: set __name__ for imported pages
Goffi <goffi@goffi.org>
parents:
1141
diff
changeset
|
19 log = getLogger(__name__) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 """generic blog (with service/node provided)""" |
1216 | 22 name = 'blog_view' |
23 template = "blog/articles.html" | |
24 uri_handlers = {('pubsub', 'microblog'): 'microblog_uri'} | |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 |
1216 | 26 RE_TEXT_URL = re.compile(r'[^a-zA-Z,_]+') |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 TEXT_MAX_LEN = 60 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 TEXT_WORD_MIN_LENGHT = 4 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 URL_LIMIT_MARK = 90 # if canonical URL is longer than that, text will not be appended |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 def microblog_uri(self, uri_data): |
1216 | 33 args = [uri_data['path'], uri_data['node']] |
34 if 'item' in uri_data: | |
35 args.extend(['id', uri_data['item']]) | |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 return self.getURL(*args) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 def parse_url(self, request): |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 """URL is /[service]/[node]/[filter_keyword]/[item]|[other] |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 if [node] is '@', default namespace is used |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 if a value is unset, default one will be used |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 keyword can be one of: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 id: next value is a item id |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 tag: next value is a blog tag |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 """ |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 data = self.getRData(request) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 try: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 service = self.nextPath(request) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 except IndexError: |
1216 | 52 data['service'] = '' |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 else: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 try: |
1216 | 55 data["service"] = jid.JID(service) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 except Exception: |
1216 | 57 log.warning(_("bad service entered: {}").format(service)) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 self.pageError(request, C.HTTP_BAD_REQUEST) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 try: |
1171
469d0de8da0e
pages (blog, u): added atom feed link in "links" template data.
Goffi <goffi@goffi.org>
parents:
1156
diff
changeset
|
61 node = self.nextPath(request) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 except IndexError: |
1216 | 63 node = '@' |
64 data['node'] = '' if node == '@' else node | |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 try: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 filter_kw = data['filter_keyword'] = self.nextPath(request) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 except IndexError: |
1216 | 69 filter_kw = '@' |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 else: |
1216 | 71 if filter_kw == '@': |
1171
469d0de8da0e
pages (blog, u): added atom feed link in "links" template data.
Goffi <goffi@goffi.org>
parents:
1156
diff
changeset
|
72 # No filter, this is used when a subpage is needed, notably Atom feed |
469d0de8da0e
pages (blog, u): added atom feed link in "links" template data.
Goffi <goffi@goffi.org>
parents:
1156
diff
changeset
|
73 pass |
1216 | 74 elif filter_kw == 'id': |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 try: |
1216 | 76 data['item'] = self.nextPath(request) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 except IndexError: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 self.pageError(request, C.HTTP_BAD_REQUEST) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 # we get one more argument in case text has been added to have a nice URL |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 try: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 self.nextPath(request) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 except IndexError: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 pass |
1216 | 84 elif filter_kw == 'tag': |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 try: |
1216 | 86 data['tag'] = self.nextPath(request) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 except IndexError: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 self.pageError(request, C.HTTP_BAD_REQUEST) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 else: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 # invalid filter keyword |
1216 | 91 log.warning(_("invalid filter keyword: {filter_kw}").format( |
1171
469d0de8da0e
pages (blog, u): added atom feed link in "links" template data.
Goffi <goffi@goffi.org>
parents:
1156
diff
changeset
|
92 filter_kw=filter_kw)) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 self.pageError(request, C.HTTP_BAD_REQUEST) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 |
1171
469d0de8da0e
pages (blog, u): added atom feed link in "links" template data.
Goffi <goffi@goffi.org>
parents:
1156
diff
changeset
|
95 # if URL is parsed here, we'll have atom.xml available and we need to |
469d0de8da0e
pages (blog, u): added atom feed link in "links" template data.
Goffi <goffi@goffi.org>
parents:
1156
diff
changeset
|
96 # add the link to the page |
469d0de8da0e
pages (blog, u): added atom feed link in "links" template data.
Goffi <goffi@goffi.org>
parents:
1156
diff
changeset
|
97 atom_url = self.getURLByPath( |
1216 | 98 SubPage('blog_view'), |
1171
469d0de8da0e
pages (blog, u): added atom feed link in "links" template data.
Goffi <goffi@goffi.org>
parents:
1156
diff
changeset
|
99 service, |
469d0de8da0e
pages (blog, u): added atom feed link in "links" template data.
Goffi <goffi@goffi.org>
parents:
1156
diff
changeset
|
100 node, |
469d0de8da0e
pages (blog, u): added atom feed link in "links" template data.
Goffi <goffi@goffi.org>
parents:
1156
diff
changeset
|
101 filter_kw, |
1216 | 102 SubPage('blog_feed_atom'), |
1171
469d0de8da0e
pages (blog, u): added atom feed link in "links" template data.
Goffi <goffi@goffi.org>
parents:
1156
diff
changeset
|
103 ) |
1216 | 104 request.template_data['atom_url'] = atom_url |
105 request.template_data.setdefault('links', []).append({ | |
106 "href": atom_url, | |
107 "type": "application/atom+xml", | |
108 "rel": "alternate", | |
109 "title": "{service}'s blog".format(service=service)}) | |
1171
469d0de8da0e
pages (blog, u): added atom feed link in "links" template data.
Goffi <goffi@goffi.org>
parents:
1156
diff
changeset
|
110 |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 @defer.inlineCallbacks |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 def appendComments(self, blog_items, identities, profile): |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
114 for blog_item in blog_items['items']: |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 if identities is not None: |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
116 author = blog_item['author_jid'] |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 if not author: |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
118 log.warning(_("no author found for item {item_id}").format( |
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
119 item_id=blog_item['id'])) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 else: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 if author not in identities: |
1243
8aff742d0dd0
pages: updated `identityGet` call, following backend changes
Goffi <goffi@goffi.org>
parents:
1239
diff
changeset
|
122 id_raw = yield self.host.bridgeCall( |
8aff742d0dd0
pages: updated `identityGet` call, following backend changes
Goffi <goffi@goffi.org>
parents:
1239
diff
changeset
|
123 'identityGet', author, [], True, profile) |
8aff742d0dd0
pages: updated `identityGet` call, following backend changes
Goffi <goffi@goffi.org>
parents:
1239
diff
changeset
|
124 identities[author] = data_format.deserialise(id_raw) |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
125 for comment_data in blog_item['comments']: |
1216 | 126 service = comment_data['service'] |
127 node = comment_data['node'] | |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 try: |
1216 | 129 comments_data = yield self.host.bridgeCall('mbGet', |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 service, |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 node, |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 C.NO_LIMIT, |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 [], |
1137
dfd6545a205a
pages (blog/view, tickets): use of new "order_by" feature.
Goffi <goffi@goffi.org>
parents:
1133
diff
changeset
|
134 {C.KEY_ORDER_BY: C.ORDER_BY_CREATION}, |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 profile) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 except Exception as e: |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
137 log.warning( |
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
138 _("Can't get comments at {node} (service: {service}): {msg}").format( |
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
139 service=service, |
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
140 node=node, |
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
141 msg=e)) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 continue |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
144 comments = data_format.deserialise(comments_data) |
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
145 comment_data['items'] = comments['items'] |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 yield appendComments(self, comments, identities, profile) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 @defer.inlineCallbacks |
1140
e1a953512f72
pages (blog/view): pagination improvments:
Goffi <goffi@goffi.org>
parents:
1137
diff
changeset
|
149 def getBlogItems(self, request, service, node, item_id, extra, profile): |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 try: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 if item_id: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 items_id = [item_id] |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 else: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 items_id = [] |
1216 | 155 blog_data = yield self.host.bridgeCall('mbGet', |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 service.userhost(), |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 node, |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 C.NO_LIMIT, |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 items_id, |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 extra, |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 profile) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 except Exception as e: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 # FIXME: need a better way to test errors in bridge errback |
1216 | 164 if "forbidden" in str(e): |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 self.pageError(request, 401) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 else: |
1216 | 167 log.warning(_("can't retrieve blog for [{service}]: {msg}".format( |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 service = service.userhost(), msg=e))) |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
169 blog_data = {"items": []} |
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
170 else: |
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
171 blog_data = data_format.deserialise(blog_data) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
173 defer.returnValue(blog_data) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 @defer.inlineCallbacks |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 def prepare_render(self, request): |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 data = self.getRData(request) |
1216 | 178 page_max = data.get("page_max", 10) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 # if the comments are not explicitly hidden, we show them |
1216 | 180 service, node, item_id, show_comments = data.get('service', ''), data.get('node', ''), data.get('item'), data.get('show_comments', True) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 profile = self.getProfile(request) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 if profile is None: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 profile = C.SERVICE_PROFILE |
1133
122dd136d1ab
pages (blog/view): allow commenting when profile is connected
Goffi <goffi@goffi.org>
parents:
1124
diff
changeset
|
184 profile_connected = False |
122dd136d1ab
pages (blog/view): allow commenting when profile is connected
Goffi <goffi@goffi.org>
parents:
1124
diff
changeset
|
185 else: |
122dd136d1ab
pages (blog/view): allow commenting when profile is connected
Goffi <goffi@goffi.org>
parents:
1124
diff
changeset
|
186 profile_connected = True |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 ## pagination/filtering parameters |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 if item_id: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 extra = {} |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 else: |
1141
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1140
diff
changeset
|
192 extra = self.getPubsubExtra(request, page_max=page_max) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 tag = data.get('tag') |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 if tag: |
1216 | 195 extra['mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)] = tag |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 ## main data ## |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 # we get data from backend/XMPP here |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
199 blog_items = yield getBlogItems(self, request, service, node, item_id, extra, profile) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 ## navigation ## |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 # no let's fill service, node and pagination URLs |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 template_data = request.template_data |
1216 | 204 if 'service' not in template_data: |
205 template_data['service'] = service | |
206 if 'node' not in template_data: | |
207 template_data['node'] = node | |
208 target_profile = template_data.get('target_profile') | |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
210 if blog_items: |
1141
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1140
diff
changeset
|
211 if not item_id: |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
212 self.setPagination(request, blog_items) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 else: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 if item_id: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 # if item id has been specified in URL and it's not found, |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 # we must return an error |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 self.pageError(request, C.HTTP_NOT_FOUND) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 ## identities ## |
1133
122dd136d1ab
pages (blog/view): allow commenting when profile is connected
Goffi <goffi@goffi.org>
parents:
1124
diff
changeset
|
220 # identities are used to show nice nickname or avatars |
1266
6b7f9c3558cc
server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents:
1243
diff
changeset
|
221 identities = self.host.getSessionData(request, session_iface.ISATSession).identities |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 ## Comments ## |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 # if comments are requested, we need to take them |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 if show_comments: |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
226 yield appendComments(self, blog_items, identities, profile) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 ## URLs ## |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 # We will fill items_http_uri and tags_http_uri in template_data with suitable urls |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 # if we know the profile, we use it instead of service + blog (nicer url) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 if target_profile is None: |
1216 | 232 blog_base_url_item = self.getPageByName('blog_view').getURL(service.full(), node or '@', 'id') |
233 blog_base_url_tag = self.getPageByName('blog_view').getURL(service.full(), node or '@', 'tag') | |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 else: |
1216 | 235 blog_base_url_item = self.getURLByNames([('user', [target_profile]), ('user_blog', ['id'])]) |
236 blog_base_url_tag = self.getURLByNames([('user', [target_profile]), ('user_blog', ['tag'])]) | |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 # we also set the background image if specified by user |
1216 | 238 bg_img = yield self.host.bridgeCall('asyncGetParamA', 'Background', 'Blog page', 'value', -1, template_data['target_profile']) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 if bg_img: |
1216 | 240 template_data['dynamic_style'] = safe(""" |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 :root { |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 --bg-img: url("%s"); |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 } |
1229
acec8e9185db
pages: replaced deprecated `cgi.escape` by `html.escape`
Goffi <goffi@goffi.org>
parents:
1219
diff
changeset
|
244 """ % html.escape(bg_img, True)) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
246 template_data['blog_items'] = data['blog_items'] = blog_items |
1219
0f0c36992f3c
pages (blog/view, forums/view): fixed encoding of request.args following Python3 port
Goffi <goffi@goffi.org>
parents:
1216
diff
changeset
|
247 if request.args.get(b'reverse') == ['1']: |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
248 template_data['blog_items'].items.reverse() |
1216 | 249 template_data['items_http_uri'] = items_http_uri = {} |
250 template_data['tags_http_uri'] = tags_http_uri = {} | |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
253 for item in blog_items['items']: |
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
254 blog_canonical_url = '/'.join([blog_base_url_item, utils.quote(item['id'])]) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
255 if len(blog_canonical_url) > URL_LIMIT_MARK: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 blog_url = blog_canonical_url |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
257 else: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
258 # we add text from title or body at the end of URL |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
259 # to make it more human readable |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
260 text = item.get('title', item['content']) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
261 # we change special chars to ascii one, trick found at https://stackoverflow.com/a/3194567 |
1216 | 262 text = unicodedata.normalize('NFD', text).encode('ascii', 'ignore').decode('utf-8') |
263 text = RE_TEXT_URL.sub(' ', text).lower() | |
264 text = '-'.join([t for t in text.split() if t and len(t)>=TEXT_WORD_MIN_LENGHT]) | |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
265 while len(text) > TEXT_MAX_LEN: |
1216 | 266 if '-' in text: |
267 text = text.rsplit('-', 1)[0] | |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
268 else: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
269 text = text[:TEXT_MAX_LEN] |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
270 if text: |
1216 | 271 blog_url = blog_canonical_url + '/' + text |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
272 else: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
273 blog_url = blog_canonical_url |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
274 |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
275 items_http_uri[item['id']] = self.host.getExtBaseURL(request, blog_url) |
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
276 for tag in item['tags']: |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
277 if tag not in tags_http_uri: |
1216 | 278 tag_url = '/'.join([blog_base_url_tag, utils.quote(tag)]) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
279 tags_http_uri[tag] = self.host.getExtBaseURL(request, tag_url) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
280 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
281 # if True, page should display a comment box |
1216 | 282 template_data['allow_commenting'] = data.get('allow_commenting', profile_connected) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
283 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
284 # last but not least, we add a xmpp: link to the node |
1216 | 285 uri_args = {'path': service.full()} |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
286 if node: |
1216 | 287 uri_args['node'] = node |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
288 if item_id: |
1216 | 289 uri_args['item'] = item_id |
290 template_data['xmpp_uri'] = uri.buildXMPPUri('pubsub', subtype='microblog', **uri_args) | |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
291 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
292 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
293 @defer.inlineCallbacks |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
294 def on_data_post(self, request): |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
295 profile = self.getProfile(request) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
296 if profile is None: |
1173
0f37b65fe7c2
server: replaced wrong usage of C.HTTP_UNAUTHORIZED by C.HTTP_FORBIDDEN
Goffi <goffi@goffi.org>
parents:
1171
diff
changeset
|
297 self.pageError(request, C.HTTP_FORBIDDEN) |
1216 | 298 type_ = self.getPostedData(request, 'type') |
299 if type_ == 'comment': | |
300 service, node, body = self.getPostedData(request, ('service', 'node', 'body')) | |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
302 if not body: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
303 self.pageError(request, C.HTTP_BAD_REQUEST) |
1216 | 304 comment_data = {"content": body} |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
305 try: |
1216 | 306 yield self.host.bridgeCall('mbSend', |
1156
3048bd137aaf
server, browser: changed blog items serialisation following changes in backend
Goffi <goffi@goffi.org>
parents:
1145
diff
changeset
|
307 service, |
3048bd137aaf
server, browser: changed blog items serialisation following changes in backend
Goffi <goffi@goffi.org>
parents:
1145
diff
changeset
|
308 node, |
3048bd137aaf
server, browser: changed blog items serialisation following changes in backend
Goffi <goffi@goffi.org>
parents:
1145
diff
changeset
|
309 data_format.serialise(comment_data), |
3048bd137aaf
server, browser: changed blog items serialisation following changes in backend
Goffi <goffi@goffi.org>
parents:
1145
diff
changeset
|
310 profile) |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
311 except Exception as e: |
1216 | 312 if "forbidden" in str(e): |
1077
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
313 self.pageError(request, 401) |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
314 else: |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
315 raise e |
880ea673aaff
blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
316 else: |
1216 | 317 log.warning(_("Unhandled data type: {}").format(type_)) |