changeset 1140:e1a953512f72

pages (blog/view): pagination improvments: - getBlogData renamed to getBlogItems and only returns BlogItems objects, blog_data are not returned anymore as they are already included in the metadata attribute - desired page size can be specified using "page_max" key in request data - better use of RSM data to know if we should set previous or next page URLs - don't display anymore the first page is no items are found, this was confusing and not really making sense
author Goffi <goffi@goffi.org>
date Fri, 11 Jan 2019 16:45:01 +0100
parents e45480b6ba24
children 02fc28aac2b6
files libervia/pages/blog/view/page_meta.py
diffstat 1 files changed, 17 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/pages/blog/view/page_meta.py	Fri Jan 11 16:39:46 2019 +0100
+++ b/libervia/pages/blog/view/page_meta.py	Fri Jan 11 16:45:01 2019 +0100
@@ -121,7 +121,7 @@
             yield appendComments(self, comments, identities, profile)
 
 @defer.inlineCallbacks
-def getBlogData(self, request, service, node, item_id, extra, profile):
+def getBlogItems(self, request, service, node, item_id, extra, profile):
     try:
         if item_id:
             items_id = [item_id]
@@ -143,12 +143,12 @@
                 service = service.userhost(), msg=e)))
             blog_data = ([], {})
 
-    items = data_objects.BlogItems(blog_data)
-    defer.returnValue((blog_data, items))
+    defer.returnValue(data_objects.BlogItems(blog_data))
 
 @defer.inlineCallbacks
 def prepare_render(self, request):
     data = self.getRData(request)
+    page_max = data.get(u"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)
     profile = self.getProfile(request)
@@ -165,7 +165,7 @@
     else:
         extra = {
             C.KEY_ORDER_BY: C.ORDER_BY_CREATION,
-            u'rsm_max': u'10',
+            u'rsm_max': unicode(page_max),
         }
         if u'after' in params:
             extra[u'rsm_after'] = params[u'after']
@@ -177,7 +177,7 @@
 
     ## main data ##
     # we get data from backend/XMPP here
-    blog_data, items = yield getBlogData(self, request, service, node, item_id, extra, profile)
+    items = yield getBlogItems(self, request, service, node, item_id, extra, profile)
 
     ## navigation ##
     # no let's fill service, node and pagination URLs
@@ -189,11 +189,18 @@
     target_profile = template_data.get(u'target_profile')
 
     if items:
-        if not item_id:
-            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
+        if items.with_rsm and not item_id:
+            last_id = items.rsm_last
+
+            if not items.complete:
+                # we also show the page next button if complete is None because we
+                # can't know where we are in the feed in this case.
+                template_data['older_url'] = self.getParamURL(request, after=last_id)
+            if items.metadata.get("rsm_index", 1) > 0:
+                # We only show previous button if it's not the first page already.
+                # If we have no index, we default to display the button anyway
+                # as we can't know if we are on the first page or not.
+                first_id = items.rsm_first
                 template_data['newer_url']  = self.getParamURL(request, before=first_id)
     else:
         if item_id:
@@ -201,15 +208,6 @@
             # we must return an error
             self.pageError(request, C.HTTP_NOT_FOUND)
 
-        # no items, we have requested items before last post, or blog is empty
-        extra = {
-            C.KEY_ORDER_BY: C.ORDER_BY_CREATION,
-            u'rsm_max': u'10',
-        }
-        blog_data, items = yield getBlogData(self, request, service, node, None, extra, profile)
-        if items:
-            last_id = items[-1].id
-            template_data['older_url'] = self.getParamURL(request, after=last_id)
 
     ## identities ##
     # identities are used to show nice nickname or avatars