Mercurial > libervia-web
diff libervia/pages/blog/view/page_meta.py @ 1216:b2d067339de3
python 3 port:
/!\ Python 3.6+ is now needed to use libervia
/!\ instability may occur and features may not be working anymore, this will improve with time
/!\ TxJSONRPC dependency has been removed
The same procedure as in backend has been applied (check backend commit ab2696e34d29 logs
for details). Removed now deprecated code (Pyjamas compiled browser part, legacy blog,
JSON RPC related code).
Adapted code to work without `html` and `themes` dirs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 13 Aug 2019 19:12:31 +0200 |
parents | 67ec22356457 |
children | 0f0c36992f3c |
line wrap: on
line diff
--- a/libervia/pages/blog/view/page_meta.py Tue Aug 13 09:39:33 2019 +0200 +++ b/libervia/pages/blog/view/page_meta.py Tue Aug 13 19:12:31 2019 +0200 @@ -1,4 +1,4 @@ -#!/usr/bin/env python2.7 +#!/usr/bin/env python3 # -*- coding: utf-8 -*- import unicodedata import re @@ -19,20 +19,20 @@ log = getLogger(__name__) """generic blog (with service/node provided)""" -name = u'blog_view' -template = u"blog/articles.html" -uri_handlers = {(u'pubsub', u'microblog'): 'microblog_uri'} +name = 'blog_view' +template = "blog/articles.html" +uri_handlers = {('pubsub', 'microblog'): 'microblog_uri'} -RE_TEXT_URL = re.compile(ur'[^a-zA-Z,_]+') +RE_TEXT_URL = re.compile(r'[^a-zA-Z,_]+') TEXT_MAX_LEN = 60 TEXT_WORD_MIN_LENGHT = 4 URL_LIMIT_MARK = 90 # if canonical URL is longer than that, text will not be appended def microblog_uri(self, uri_data): - args = [uri_data[u'path'], uri_data[u'node']] - if u'item' in uri_data: - args.extend([u'id', uri_data[u'item']]) + args = [uri_data['path'], uri_data['node']] + if 'item' in uri_data: + args.extend(['id', uri_data['item']]) return self.getURL(*args) def parse_url(self, request): @@ -49,31 +49,31 @@ try: service = self.nextPath(request) except IndexError: - data['service'] = u'' + data['service'] = '' else: try: - data[u"service"] = jid.JID(service) + data["service"] = jid.JID(service) except Exception: - log.warning(_(u"bad service entered: {}").format(service)) + log.warning(_("bad service entered: {}").format(service)) self.pageError(request, C.HTTP_BAD_REQUEST) try: node = self.nextPath(request) except IndexError: - node = u'@' - data['node'] = u'' if node == u'@' else node + node = '@' + data['node'] = '' if node == '@' else node try: filter_kw = data['filter_keyword'] = self.nextPath(request) except IndexError: - filter_kw = u'@' + filter_kw = '@' else: - if filter_kw == u'@': + if filter_kw == '@': # No filter, this is used when a subpage is needed, notably Atom feed pass - elif filter_kw == u'id': + elif filter_kw == 'id': try: - data[u'item'] = self.nextPath(request) + data['item'] = self.nextPath(request) except IndexError: self.pageError(request, C.HTTP_BAD_REQUEST) # we get one more argument in case text has been added to have a nice URL @@ -81,32 +81,32 @@ self.nextPath(request) except IndexError: pass - elif filter_kw == u'tag': + elif filter_kw == 'tag': try: - data[u'tag'] = self.nextPath(request) + data['tag'] = self.nextPath(request) except IndexError: self.pageError(request, C.HTTP_BAD_REQUEST) else: # invalid filter keyword - log.warning(_(u"invalid filter keyword: {filter_kw}").format( + log.warning(_("invalid filter keyword: {filter_kw}").format( filter_kw=filter_kw)) self.pageError(request, C.HTTP_BAD_REQUEST) # if URL is parsed here, we'll have atom.xml available and we need to # add the link to the page atom_url = self.getURLByPath( - SubPage(u'blog_view'), + SubPage('blog_view'), service, node, filter_kw, - SubPage(u'blog_feed_atom'), + SubPage('blog_feed_atom'), ) - request.template_data[u'atom_url'] = atom_url - request.template_data.setdefault(u'links', []).append({ - u"href": atom_url, - u"type": "application/atom+xml", - u"rel": "alternate", - u"title": "{service}'s blog".format(service=service)}) + request.template_data['atom_url'] = atom_url + request.template_data.setdefault('links', []).append({ + "href": atom_url, + "type": "application/atom+xml", + "rel": "alternate", + "title": "{service}'s blog".format(service=service)}) @defer.inlineCallbacks @@ -115,15 +115,15 @@ if identities is not None: author = blog_item.author_jid if not author: - log.warning(_(u"no author found for item {item_id}").format(item_id=blog_item.id)) + log.warning(_("no author found for item {item_id}").format(item_id=blog_item.id)) else: if author not in identities: - identities[author] = yield self.host.bridgeCall(u'identityGet', author, profile) + identities[author] = yield self.host.bridgeCall('identityGet', author, profile) for comment_data in blog_item.comments: - service = comment_data[u'service'] - node = comment_data[u'node'] + service = comment_data['service'] + node = comment_data['node'] try: - comments_data = yield self.host.bridgeCall(u'mbGet', + comments_data = yield self.host.bridgeCall('mbGet', service, node, C.NO_LIMIT, @@ -131,7 +131,7 @@ {C.KEY_ORDER_BY: C.ORDER_BY_CREATION}, profile) except Exception as e: - log.warning(_(u"Can't get comments at {node} (service: {service}): {msg}").format( + log.warning(_("Can't get comments at {node} (service: {service}): {msg}").format( service=service, node=node, msg=e)) @@ -148,7 +148,7 @@ items_id = [item_id] else: items_id = [] - blog_data = yield self.host.bridgeCall(u'mbGet', + blog_data = yield self.host.bridgeCall('mbGet', service.userhost(), node, C.NO_LIMIT, @@ -157,10 +157,10 @@ profile) except Exception as e: # FIXME: need a better way to test errors in bridge errback - if u"forbidden" in unicode(e): + if "forbidden" in str(e): self.pageError(request, 401) else: - log.warning(_(u"can't retrieve blog for [{service}]: {msg}".format( + log.warning(_("can't retrieve blog for [{service}]: {msg}".format( service = service.userhost(), msg=e))) blog_data = ([], {}) @@ -169,9 +169,9 @@ @defer.inlineCallbacks def prepare_render(self, request): data = self.getRData(request) - page_max = data.get(u"page_max", 10) + page_max = data.get("page_max", 10) # if the comments are not explicitly hidden, we show them - 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) + service, node, item_id, show_comments = data.get('service', ''), data.get('node', ''), data.get('item'), data.get('show_comments', True) profile = self.getProfile(request) if profile is None: profile = C.SERVICE_PROFILE @@ -186,7 +186,7 @@ extra = self.getPubsubExtra(request, page_max=page_max) tag = data.get('tag') if tag: - extra[u'mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)] = tag + extra['mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)] = tag ## main data ## # we get data from backend/XMPP here @@ -195,11 +195,11 @@ ## navigation ## # no let's fill service, node and pagination URLs template_data = request.template_data - if u'service' not in template_data: - template_data[u'service'] = service - if u'node' not in template_data: - template_data[u'node'] = node - target_profile = template_data.get(u'target_profile') + if 'service' not in template_data: + template_data['service'] = service + if 'node' not in template_data: + template_data['node'] = node + target_profile = template_data.get('target_profile') if items: if not item_id: @@ -212,7 +212,7 @@ ## identities ## # identities are used to show nice nickname or avatars - identities = template_data[u'identities'] = self.host.getSessionData(request, session_iface.ISATSession).identities + identities = template_data['identities'] = self.host.getSessionData(request, session_iface.ISATSession).identities ## Comments ## # if comments are requested, we need to take them @@ -223,29 +223,29 @@ # We will fill items_http_uri and tags_http_uri in template_data with suitable urls # if we know the profile, we use it instead of service + blog (nicer url) if target_profile is None: - blog_base_url_item = self.getPageByName(u'blog_view').getURL(service.full(), node or u'@', u'id') - blog_base_url_tag = self.getPageByName(u'blog_view').getURL(service.full(), node or u'@', u'tag') + blog_base_url_item = self.getPageByName('blog_view').getURL(service.full(), node or '@', 'id') + blog_base_url_tag = self.getPageByName('blog_view').getURL(service.full(), node or '@', 'tag') else: - blog_base_url_item = self.getURLByNames([(u'user', [target_profile]), (u'user_blog', [u'id'])]) - blog_base_url_tag = self.getURLByNames([(u'user', [target_profile]), (u'user_blog', [u'tag'])]) + blog_base_url_item = self.getURLByNames([('user', [target_profile]), ('user_blog', ['id'])]) + blog_base_url_tag = self.getURLByNames([('user', [target_profile]), ('user_blog', ['tag'])]) # we also set the background image if specified by user - bg_img = yield self.host.bridgeCall(u'asyncGetParamA', u'Background', u'Blog page', u'value', -1, template_data[u'target_profile']) + bg_img = yield self.host.bridgeCall('asyncGetParamA', 'Background', 'Blog page', 'value', -1, template_data['target_profile']) if bg_img: - template_data['dynamic_style'] = safe(u""" + template_data['dynamic_style'] = safe(""" :root { --bg-img: url("%s"); } """ % cgi.escape(bg_img, True)) - template_data[u'items'] = data[u'items'] = items + template_data['items'] = data['items'] = items if request.args.get('reverse') == ['1']: - template_data[u'items'].items.reverse() - template_data[u'items_http_uri'] = items_http_uri = {} - template_data[u'tags_http_uri'] = tags_http_uri = {} + template_data['items'].items.reverse() + template_data['items_http_uri'] = items_http_uri = {} + template_data['tags_http_uri'] = tags_http_uri = {} for item in items: - blog_canonical_url = u'/'.join([blog_base_url_item, utils.quote(item.id)]) + blog_canonical_url = '/'.join([blog_base_url_item, utils.quote(item.id)]) if len(blog_canonical_url) > URL_LIMIT_MARK: blog_url = blog_canonical_url else: @@ -253,35 +253,35 @@ # to make it more human readable text = item.title or item.content # we change special chars to ascii one, trick found at https://stackoverflow.com/a/3194567 - text = unicodedata.normalize('NFD', text).encode('ascii', 'ignore') - text = RE_TEXT_URL.sub(u' ', text).lower() - text = u'-'.join([t for t in text.split() if t and len(t)>=TEXT_WORD_MIN_LENGHT]) + text = unicodedata.normalize('NFD', text).encode('ascii', 'ignore').decode('utf-8') + text = RE_TEXT_URL.sub(' ', text).lower() + text = '-'.join([t for t in text.split() if t and len(t)>=TEXT_WORD_MIN_LENGHT]) while len(text) > TEXT_MAX_LEN: - if u'-' in text: - text = text.rsplit(u'-', 1)[0] + if '-' in text: + text = text.rsplit('-', 1)[0] else: text = text[:TEXT_MAX_LEN] if text: - blog_url = blog_canonical_url + u'/' + text + blog_url = blog_canonical_url + '/' + text else: blog_url = blog_canonical_url items_http_uri[item.id] = self.host.getExtBaseURL(request, blog_url) for tag in item.tags: if tag not in tags_http_uri: - tag_url = u'/'.join([blog_base_url_tag, utils.quote(tag)]) + tag_url = '/'.join([blog_base_url_tag, utils.quote(tag)]) tags_http_uri[tag] = self.host.getExtBaseURL(request, tag_url) # if True, page should display a comment box - template_data[u'allow_commenting'] = data.get(u'allow_commenting', profile_connected) + template_data['allow_commenting'] = data.get('allow_commenting', profile_connected) # last but not least, we add a xmpp: link to the node - uri_args = {u'path': service.full()} + uri_args = {'path': service.full()} if node: - uri_args[u'node'] = node + uri_args['node'] = node if item_id: - uri_args[u'item'] = item_id - template_data[u'xmpp_uri'] = uri.buildXMPPUri(u'pubsub', subtype='microblog', **uri_args) + uri_args['item'] = item_id + template_data['xmpp_uri'] = uri.buildXMPPUri('pubsub', subtype='microblog', **uri_args) @defer.inlineCallbacks @@ -289,23 +289,23 @@ profile = self.getProfile(request) if profile is None: self.pageError(request, C.HTTP_FORBIDDEN) - type_ = self.getPostedData(request, u'type') - if type_ == u'comment': - service, node, body = self.getPostedData(request, (u'service', u'node', u'body')) + type_ = self.getPostedData(request, 'type') + if type_ == 'comment': + service, node, body = self.getPostedData(request, ('service', 'node', 'body')) if not body: self.pageError(request, C.HTTP_BAD_REQUEST) - comment_data = {u"content": body} + comment_data = {"content": body} try: - yield self.host.bridgeCall(u'mbSend', + yield self.host.bridgeCall('mbSend', service, node, data_format.serialise(comment_data), profile) except Exception as e: - if u"forbidden" in unicode(e): + if "forbidden" in str(e): self.pageError(request, 401) else: raise e else: - log.warning(_(u"Unhandled data type: {}").format(type_)) + log.warning(_("Unhandled data type: {}").format(type_))