changeset 1011:fe08a5c95b27

pages (blog): added pagination using RSM and blog now fill older_url and newer_url in template_data
author Goffi <goffi@goffi.org>
date Fri, 12 Jan 2018 22:04:33 +0100
parents 4de970de87d7
children 6b573b4b5bb0
files src/pages/common/blog/page_meta.py
diffstat 1 files changed, 40 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/pages/common/blog/page_meta.py	Fri Jan 12 22:04:30 2018 +0100
+++ b/src/pages/common/blog/page_meta.py	Fri Jan 12 22:04:33 2018 +0100
@@ -76,6 +76,25 @@
             blog_item.appendCommentsItems(comments)
             yield appendComments(self, comments, identities, profile)
 
+@defer.inlineCallbacks
+def getBlogData(self, request, service, node, extra, profile):
+    try:
+        blog_data = yield self.host.bridge.mbGet(
+                              service.userhost(),
+                              node,
+                              C.NO_LIMIT,
+                              [],
+                              extra,
+                              profile)
+    except Exception as e:
+        # FIXME: need a better way to test errors in bridge errback
+        if u"forbidden" in unicode(e):
+            self.pageError(request, 401)
+        else:
+            raise e
+
+    items = data_objects.BlogItems(blog_data)
+    defer.returnValue((blog_data, items))
 
 @defer.inlineCallbacks
 def prepare_render(self, request):
@@ -86,23 +105,29 @@
     if profile is None:
         profile = C.SERVICE_PROFILE
 
-    try:
-        blog_data = yield self.host.bridge.mbGet(
-                              service.userhost(),
-                              node,
-                              10,
-                              [],
-                              {},
-                              profile)
-    except Exception as e:
-        # FIXME: need a better way to test errors in bridge errback
-        if u"forbidden" in unicode(e):
-            self.pageError(request, 401)
-        else:
-            raise e
+    params = self.getAllPostedData(request, multiple=False)
+    extra = {u'rsm_max': u'10'}
+    if u'after' in params:
+        extra[u'rsm_after'] = params[u'after']
+    elif u'before' in params:
+        extra[u'rsm_before'] = params[u'before']
 
-    items = data_objects.BlogItems(blog_data)
+    blog_data, items = yield getBlogData(self, request, service, node, extra, profile)
     template_data = request.template_data
+    if items:
+        last_id = items[-1].id
+        template_data['older_url'] = self.getParamURL(request, after=last_id)
+        if u'before' in params or u'after' in params:
+            first_id = items[0].id
+            template_data['newer_url']  = self.getParamURL(request, before=first_id)
+    else:
+        # no items, we have requested items before last post, or blog is empty
+        extra = {u'rsm_max': u'10'}
+        blog_data, items = yield getBlogData(self, request, service, node, extra, profile)
+        if items:
+            last_id = items[-1].id
+            template_data['older_url'] = self.getParamURL(request, after=last_id)
+
     identities = template_data[u'identities'] = self.host.getSessionData(request, session_iface.ISATSession).identities
 
     if show_comments: