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