comparison 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
comparison
equal deleted inserted replaced
1140:e1a953512f72 1141:02fc28aac2b6
682 self._filterPathValue(v, handler, name, request) for v in data[name] 682 self._filterPathValue(v, handler, name, request) for v in data[name]
683 ] 683 ]
684 else: 684 else:
685 data[name] = self._filterPathValue(data[name], handler, name, request) 685 data[name] = self._filterPathValue(data[name], handler, name, request)
686 686
687 ## Pagination/Filtering ##
688
689 def getPubsubExtra(self, request, page_max=10, params=None, extra=None,
690 order_by=C.ORDER_BY_CREATION):
691 """Set extra dict to retrieve PubSub items corresponding to URL parameters
692
693 Following parameters are used:
694 - after: set rsm_after with ID of item
695 - before: set rsm_before with ID of item
696 @param request(server.Request): current HTTP request
697 @param page_max(int): required number of items per page
698 @param params(None, dict[unicode, list[unicode]]): params as returned by
699 self.getAllPostedData.
700 None to parse URL automatically
701 @param extra(None, dict): extra dict to use, or None to use a new one
702 @param order_by(unicode, None): key to order by
703 None to not specify order
704 @return (dict): fill extra data
705 """
706 if params is None:
707 params = self.getAllPostedData(request, multiple=False)
708 if extra is None:
709 extra = {}
710 else:
711 assert not {u"rsm_max", u"rsm_after", u"rsm_before",
712 C.KEY_ORDER_BY}.intersection(extra.keys())
713 extra[u"rsm_max"] = unicode(page_max)
714 if order_by is not None:
715 extra[C.KEY_ORDER_BY] = order_by
716 if u'after' in params:
717 extra[u'rsm_after'] = params[u'after']
718 elif u'before' in params:
719 extra[u'rsm_before'] = params[u'before']
720 return extra
721
722 def setPagination(self, request, pubsub_data):
723 """Add to template_data if suitable
724
725 "previous_page_url" and "next_page_url" will be added using respectively
726 "before" and "after" URL parameters
727 @param request(server.Request): current HTTP request
728 @param pubsub_data(dict): pubsub metadata parsed with
729 data_objects.parsePubSubMetadata
730 """
731 template_data = request.template_data
732 try:
733 last_id = pubsub_data[u"rsm_last"]
734 except KeyError:
735 # no pagination available
736 return
737
738 if pubsub_data.get("rsm_index", 1) > 0:
739 # We only show previous button if it's not the first page already.
740 # If we have no index, we default to display the button anyway
741 # as we can't know if we are on the first page or not.
742 first_id = pubsub_data[u"rsm_first"]
743 template_data['previous_page_url'] = self.getParamURL(request,
744 before=first_id)
745 if not pubsub_data[u"complete"]:
746 # we also show the page next button if complete is None because we
747 # can't know where we are in the feed in this case.
748 template_data['next_page_url'] = self.getParamURL(request, after=last_id)
749
750
687 ## Cache handling ## 751 ## Cache handling ##
688 752
689 def _setCacheHeaders(self, request, cache): 753 def _setCacheHeaders(self, request, cache):
690 """Set ETag and Last-Modified HTTP headers, used for caching""" 754 """Set ETag and Last-Modified HTTP headers, used for caching"""
691 request.setHeader("ETag", cache.hash) 755 request.setHeader("ETag", cache.hash)