# HG changeset patch # User Goffi # Date 1516784280 -3600 # Node ID f7d45c989c0529e42a8f037b08d0404a76945bbe # Parent 688b52897ba0b8a1bcea69caeec5204c017c10ac pages (common/blog): use more friendly URLs diff -r 688b52897ba0 -r f7d45c989c05 src/pages/common/blog/page_meta.py --- a/src/pages/common/blog/page_meta.py Wed Jan 24 09:58:00 2018 +0100 +++ b/src/pages/common/blog/page_meta.py Wed Jan 24 09:58:00 2018 +0100 @@ -20,6 +20,8 @@ RE_TEXT_URL = re.compile(ur'[^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']] @@ -192,9 +194,11 @@ # if we know the profile, we use it instead of service + blog (nicer url) if target_profile is None: - blog_base_url = self.getPageByName(u'blog_view').getURL(service.full(), node or u'@') + 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') else: - blog_base_url = self.getURLByNames([(u'user', [target_profile]), (u'user_blog', [])]) + 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'])]) # 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']) if bg_img: @@ -210,22 +214,29 @@ template_data[u'items_http_uri'] = items_http_uri = {} template_data[u'tags_http_uri'] = tags_http_uri = {} for item in items: - # we add text from title or body at the end of URL - # to make it more readable - text = item.title or item.content - text = RE_TEXT_URL.sub(u'-', text).lower().strip(u'-') - while len(text) > TEXT_MAX_LEN: - if u'-' in text: - text = text.rsplit(u'-', 1)[0] + blog_canonical_url = u'/'.join([blog_base_url_item, utils.quote(item.id)]) + if len(blog_canonical_url) > URL_LIMIT_MARK: + blog_url = blog_canonical_url + else: + # we add text from title or body at the end of URL + # to make it more readable + text = item.title or item.content + 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]) + while len(text) > TEXT_MAX_LEN: + if u'-' in text: + text = text.rsplit(u'-', 1)[0] + else: + text = text[:TEXT_MAX_LEN] + if text: + blog_url = blog_canonical_url + u'/' + text else: - text = text[:TEXT_MAX_LEN] + blog_url = blog_canonical_url - # now we can compute item URL - blog_url = u'/'.join([blog_base_url, u'id', utils.quote(item.id), text]) 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, u'tag', utils.quote(tag)]) + tag_url = u'/'.join([blog_base_url_tag, utils.quote(tag)]) tags_http_uri[tag] = self.host.getExtBaseURL(request, tag_url) template_data[u'allow_commenting'] = data.get(u'allow_commenting', False)