changeset 1036:f5661761b1b9

pages (common/blog): URL improvments: - if target_profile is set, user_blog is used for links instead of blog, making the URL nicer (by using profile instead of service and node) - added part of title or content to URL, to make it more explicit
author Goffi <goffi@goffi.org>
date Mon, 22 Jan 2018 22:25:37 +0100
parents 09f5a5d649df
children dc6c8c4d8ff6
files src/pages/common/blog/page_meta.py
diffstat 1 files changed, 42 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/pages/common/blog/page_meta.py	Mon Jan 22 22:22:11 2018 +0100
+++ b/src/pages/common/blog/page_meta.py	Mon Jan 22 22:25:37 2018 +0100
@@ -8,6 +8,8 @@
 from sat.core.i18n import _
 from sat.core.log import getLogger
 from sat.tools.common.template import safe
+from server import utils
+import re
 import urllib
 import cgi
 log = getLogger('pages/common/blog')
@@ -17,6 +19,8 @@
 template = u"blog/articles.html"
 uri_handlers = {(u'pubsub', u'microblog'): 'microblog_uri'}
 
+RE_TEXT_URL = re.compile(ur'[^a-zA-Zéèêôà,_]+')
+TEXT_MAX_LEN = 60
 
 def microblog_uri(self, uri_data):
     service = urllib.quote_plus(uri_data[u'path'])
@@ -64,6 +68,11 @@
                 data[u'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
+            try:
+                self.nextPath(request)
+            except IndexError:
+                pass
         elif filter_kw == u'tag':
             try:
                 data[u'tag'] = self.nextPath(request)
@@ -152,6 +161,8 @@
 
     blog_data, items = yield getBlogData(self, request, service, node, item_id, extra, profile)
     template_data = request.template_data
+    target_profile = template_data.get(u'target_profile')
+
     if items:
         if not item_id:
             last_id = items[-1].id
@@ -177,20 +188,12 @@
     if show_comments:
         yield appendComments(self, items, identities, profile)
 
-    blog_view = self.getPageByName(u'blog_view')
-    template_data[u'items'] = data[u'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 = {}
-    for item in items:
-        service_s = service.full()
-        items_http_uri[item.id] = self.host.getExtBaseURL(request, blog_view.getURL(service_s, node or '@', u'id', item.id))
-        for tag in item.tags:
-            if tag not in tags_http_uri:
-                tags_http_uri[tag] = self.host.getExtBaseURL(request, blog_view.getURL(service_s, node or '@', u'tag', tag))
-    template_data[u'allow_commenting'] = data.get(u'allow_commenting', False)
-    if u'target_profile' in template_data:
+    # 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'@')
+    else:
+        blog_base_url = self.getURLByNames([(u'user', [target_profile]), (u'user_blog', [])])
+        # 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:
             template_data['dynamic_style'] = safe(u"""
@@ -199,6 +202,31 @@
                 }
                 """ % cgi.escape(bg_img, True))
 
+    template_data[u'items'] = data[u'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 = {}
+    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]
+            else:
+                text = text[:TEXT_MAX_LEN]
+
+        # 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)])
+                tags_http_uri[tag] = self.host.getExtBaseURL(request, tag_url)
+    template_data[u'allow_commenting'] = data.get(u'allow_commenting', False)
+
 
 @defer.inlineCallbacks
 def on_data_post(self, request):