changeset 1141:02fc28aac2b6

pages: move pagination core from blog to LiberviaPage so it can be reused: 2 new methods are available in LiberviaPage: - getPubsubExtra to generate extra data for bridge when a pubsub get is involved - setPagination to add pagination informations to template_data
author Goffi <goffi@goffi.org>
date Fri, 11 Jan 2019 21:45:59 +0100
parents e1a953512f72
children 003597f895a0
files libervia/pages/blog/view/page_meta.py libervia/server/pages.py
diffstat 2 files changed, 67 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/pages/blog/view/page_meta.py	Fri Jan 11 16:45:01 2019 +0100
+++ b/libervia/pages/blog/view/page_meta.py	Fri Jan 11 21:45:59 2019 +0100
@@ -159,18 +159,10 @@
         profile_connected = True
 
     ## pagination/filtering parameters
-    params = self.getAllPostedData(request, multiple=False)
     if item_id:
         extra = {}
     else:
-        extra = {
-            C.KEY_ORDER_BY: C.ORDER_BY_CREATION,
-            u'rsm_max': unicode(page_max),
-        }
-        if u'after' in params:
-            extra[u'rsm_after'] = params[u'after']
-        elif u'before' in params:
-            extra[u'rsm_before'] = params[u'before']
+        extra = self.getPubsubExtra(request, page_max=page_max)
         tag = data.get('tag')
         if tag:
             extra[u'mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)] = tag
@@ -189,26 +181,14 @@
     target_profile = template_data.get(u'target_profile')
 
     if items:
-        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)
+        if not item_id:
+            self.setPagination(request, items.metadata)
     else:
         if item_id:
             # if item id has been specified in URL and it's not found,
             # we must return an error
             self.pageError(request, C.HTTP_NOT_FOUND)
 
-
     ## identities ##
     # identities are used to show nice nickname or avatars
     identities = template_data[u'identities'] = self.host.getSessionData(request, session_iface.ISATSession).identities
--- a/libervia/server/pages.py	Fri Jan 11 16:45:01 2019 +0100
+++ b/libervia/server/pages.py	Fri Jan 11 21:45:59 2019 +0100
@@ -684,6 +684,70 @@
             else:
                 data[name] = self._filterPathValue(data[name], handler, name, request)
 
+    ## Pagination/Filtering ##
+
+    def getPubsubExtra(self, request, page_max=10, params=None, extra=None,
+        order_by=C.ORDER_BY_CREATION):
+        """Set extra dict to retrieve PubSub items corresponding to URL parameters
+
+        Following parameters are used:
+            - after: set rsm_after with ID of item
+            - before: set rsm_before with ID of item
+        @param request(server.Request): current HTTP request
+        @param page_max(int): required number of items per page
+        @param params(None, dict[unicode, list[unicode]]): params as returned by
+            self.getAllPostedData.
+            None to parse URL automatically
+        @param extra(None, dict): extra dict to use, or None to use a new one
+        @param order_by(unicode, None): key to order by
+            None to not specify order
+        @return (dict): fill extra data
+        """
+        if params is None:
+            params = self.getAllPostedData(request, multiple=False)
+        if extra is None:
+            extra = {}
+        else:
+            assert not {u"rsm_max", u"rsm_after", u"rsm_before",
+                        C.KEY_ORDER_BY}.intersection(extra.keys())
+        extra[u"rsm_max"] = unicode(page_max)
+        if order_by is not None:
+            extra[C.KEY_ORDER_BY] = order_by
+        if u'after' in params:
+            extra[u'rsm_after'] = params[u'after']
+        elif u'before' in params:
+            extra[u'rsm_before'] = params[u'before']
+        return extra
+
+    def setPagination(self, request, pubsub_data):
+        """Add  to template_data if suitable
+
+        "previous_page_url" and "next_page_url" will be added using respectively
+        "before" and "after" URL parameters
+        @param request(server.Request): current HTTP request
+        @param pubsub_data(dict): pubsub metadata parsed with
+            data_objects.parsePubSubMetadata
+        """
+        template_data = request.template_data
+        try:
+            last_id = pubsub_data[u"rsm_last"]
+        except KeyError:
+            # no pagination available
+            return
+
+        if pubsub_data.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 = pubsub_data[u"rsm_first"]
+            template_data['previous_page_url'] = self.getParamURL(request,
+                                                                  before=first_id)
+        if not pubsub_data[u"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['next_page_url'] = self.getParamURL(request, after=last_id)
+
+
     ## Cache handling ##
 
     def _setCacheHeaders(self, request, cache):