diff piecrust/data/iterators.py @ 729:e35407c60e00

templating: Make blog archives generator expose more templating data. In addition to pagination data, also expose a non-paginating iterator that lists all posts in a source by chronological order.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 01 Jun 2016 22:09:21 -0700
parents 911a054f4fbd
children 4850f8c21b6e
line wrap: on
line diff
--- a/piecrust/data/iterators.py	Mon May 30 20:45:27 2016 -0700
+++ b/piecrust/data/iterators.py	Wed Jun 01 22:09:21 2016 -0700
@@ -109,13 +109,27 @@
                 yield page
 
 
+class GenericSortIterator(object):
+    def __init__(self, it, sorter):
+        self.it = it
+        self.sorter = sorter
+        self._sorted_it = None
+
+    def __iter__(self):
+        if self._sorted_it is None:
+            self._sorted_it = self.sorter(self.it)
+        return iter(self._sorted_it)
+
+
 class PageIterator(object):
     debug_render = []
     debug_render_doc_dynamic = ['_debugRenderDoc']
     debug_render_not_empty = True
 
-    def __init__(self, source, current_page=None, pagination_filter=None,
-            offset=0, limit=-1, locked=False):
+    def __init__(self, source, *,
+                 current_page=None,
+                 pagination_filter=None, sorter=None,
+                 offset=0, limit=-1, locked=False):
         self._source = source
         self._current_page = current_page
         self._locked = False
@@ -151,6 +165,10 @@
             self._simpleNonSortedWrap(PaginationFilterIterator,
                                       pagination_filter)
 
+        if sorter is not None:
+            self._simpleNonSortedWrap(GenericSortIterator, sorter)
+            self._has_sorter = True
+
         if offset > 0 or limit > 0:
             self.slice(offset, limit)