diff piecrust/rendering.py @ 324:65e6d72f3877

bake/serve: Fix how taxonomy index pages are setup and rendered. * Properly use the taxonomy's setting name where appropriate. * Delete duplicated (and sometimes incorrect) code in 2 places to setup filtering on an index page and consolidate it on the `PageRenderingContext`. * Add unit tests.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 29 Mar 2015 23:08:37 -0700
parents 27d623a241c6
children 422052d2e978
line wrap: on
line diff
--- a/piecrust/rendering.py	Sun Mar 29 23:05:03 2015 -0700
+++ b/piecrust/rendering.py	Sun Mar 29 23:08:37 2015 -0700
@@ -3,6 +3,9 @@
 import logging
 from piecrust.data.builder import (DataBuildingContext, build_page_data,
         build_layout_data)
+from piecrust.data.filters import (
+        PaginationFilter, HasFilterClause, IsFilterClause, AndBooleanClause,
+        page_value_accessor)
 from piecrust.sources.base import PageSource
 from piecrust.templating.base import TemplateNotFoundError, TemplatingError
 from piecrust.uriutil import get_slug
@@ -83,6 +86,23 @@
         if isinstance(source, PageSource):
             self.used_source_names.add((source.name, self.current_pass))
 
+    def setTaxonomyFilter(self, taxonomy, term_value):
+        flt = PaginationFilter(value_accessor=page_value_accessor)
+        if taxonomy.is_multiple:
+            if isinstance(term_value, tuple):
+                abc = AndBooleanClause()
+                for t in term_value:
+                    abc.addClause(HasFilterClause(taxonomy.setting_name, t))
+                flt.addClause(abc)
+            else:
+                flt.addClause(
+                        HasFilterClause(taxonomy.setting_name, term_value))
+        else:
+            flt.addClause(IsFilterClause(taxonomy.setting_name, term_value))
+        self.pagination_filter = flt
+        self.custom_data = {
+                taxonomy.term_name: term_value}
+
 
 def render_page(ctx):
     eis = ctx.app.env.exec_info_stack