diff libervia/server/pages.py @ 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 6414fd795df4
children 2af117bfe6cc
line wrap: on
line diff
--- 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):