diff src/server/blog.py @ 708:e9a6cbb924e6

server_side: fixes static blog navigation links
author souliane <souliane@mailoo.org>
date Mon, 08 Jun 2015 08:51:41 +0200
parents 531eacb82e9f
children bf562fb9c273
line wrap: on
line diff
--- a/src/server/blog.py	Sun Jun 07 13:17:05 2015 +0200
+++ b/src/server/blog.py	Mon Jun 08 08:51:41 2015 +0200
@@ -60,7 +60,7 @@
                  }
         if data:
             data_.update(data)
-        return (getattr(theme, tpl.upper()).format(**data_)).encode('utf-8')
+        return getattr(theme, tpl.upper()).encode('utf-8').format(**data_)
 
 
 class MicroBlog(Resource, TemplateProcessor):
@@ -149,23 +149,12 @@
     def gotJID(self, pub_jid_s, request, profile):
         pub_jid = JID(pub_jid_s)
         d = defer.Deferred()
-        item_id = None
-        atom = None
 
-        if len(request.postpath) > 1:
-            if request.postpath[1] == 'atom.xml':  # return the atom feed
-                atom = True
-            else:
-                try:  # check if the given path is a valid UUID
-                    uuid.UUID(request.postpath[1])
-                    item_id = request.postpath[1]
-                except ValueError:
-                    pass
-
-        rsm_ = self.parseURLParams(request, item_id)
+        self.parseURLParams(request)
+        item_id, rsm_ = request.item_id, request.rsm_data
         max_items = int(rsm_['max_'])
 
-        if atom is not None:
+        if request.atom:
             d.addCallbacks(self.render_atom_feed, self.render_error_blog, [request], None, [request, profile], None)
             self.host.bridge.getGroupBlogsAtom(pub_jid.userhost(), rsm_, C.SERVICE_PROFILE, d.callback, d.errback)
             return
@@ -182,13 +171,37 @@
             else:  # display the last messages, count their comments
                 self.host.bridge.getGroupBlogs(pub_jid.userhost(), [], rsm_, True, C.SERVICE_PROFILE, d.callback, d.errback)
 
-    def parseURLParams(self, request, item_id):
-        # retrieve RSM request data from URL parameters
+    def parseURLParams(self, request):
+        """Parse the request URL parameters.
+
+        @param request: HTTP request
+        """
+        request.item_id = None
+        request.atom = False
+
+        if len(request.postpath) > 1:
+            if request.postpath[1] == 'atom.xml':  # return the atom feed
+                request.atom = True
+            else:
+                try:  # check if the given path is a valid UUID
+                    uuid.UUID(request.postpath[1])
+                    request.item_id = request.postpath[1]
+                except ValueError:
+                    pass
+
+        self.parseURLParamsRSM(request)
+        request.display_single = (request.item_id is not None) or int(request.rsm_data['max_']) == 1
+
+    def parseURLParamsRSM(self, request):
+        """Parse RSM request data from the URL parameters.
+
+        @param request: HTTP request
+        """
         rsm_ = {}
         try:
             rsm_['max_'] = request.args['max'][0]
         except (ValueError, KeyError):
-            rsm_['max_'] = unicode(C.RSM_MAX_ITEMS if item_id else C.RSM_MAX_COMMENTS)
+            rsm_['max_'] = unicode(C.RSM_MAX_ITEMS if request.item_id else C.RSM_MAX_COMMENTS)
         try:
             rsm_['index'] = request.args['index'][0]
         except (ValueError, KeyError):
@@ -199,7 +212,7 @@
                     rsm_['after'] = request.args['after'][0]
                 except KeyError:
                     pass
-        return rsm_
+        request.rsm_data = rsm_
 
     def render_html_blog(self, mblog_data, request, profile):
         """Retrieve the user parameters before actually rendering the static blog
@@ -300,9 +313,8 @@
         data = {}
         for key in ('later_message', 'later_messages', 'older_message', 'older_messages'):
             count = int(rsm_data.get('count', 0))
-            display_single = len(mblog_data) == 1
             data[key] = ''  # key must exist when using the template
-            if count <= 0 or (display_single == key.endswith('s')):
+            if count <= 0 or (request.display_single == key.endswith('s')):
                 continue
 
             index = int(rsm_data['index'])
@@ -320,7 +332,7 @@
                 link_data['item_id'] = rsm_data['last']
                 link_data['post_arg'] = 'after'
 
-            if display_single:
+            if request.display_single:
                 link_data['suffix'] = '&max=1'
 
             link = "%(base_url)s?%(post_arg)s=%(item_id)s%(suffix)s" % link_data
@@ -379,13 +391,13 @@
             author = (_("from %s") % entry['author']).encode('utf-8')
         else:
             author = '&nbsp;'
-            message_link = ("%s/%s" % (self.base_url, entry['id'])).encode('utf-8')
+            message_link = (u"%s/%s" % (self.base_url, entry['id'])).encode('utf-8')
 
             count_text = lambda count: D_('comments') if count > 1 else D_('comment')
 
             comments_count = int(entry['comments_count'])
             delta = comments_count - len(comments)
-            if len(self.mblog_data) == 1 and delta > 0:
+            if self.request.display_single and delta > 0:
                 data['comments_link'] = ("%s?max=%s" % (message_link, entry['comments_count']))
                 data['previous_comments'] = D_("Show %(count)d previous %(comments)s") % \
                     {'count': delta, 'comments': count_text(delta)}