comparison piecrust/baking/single.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 eb958151c8dc
children 422052d2e978
comparison
equal deleted inserted replaced
323:412537e91e45 324:65e6d72f3877
53 53
54 return os.path.normpath(os.path.join(*bake_path)) 54 return os.path.normpath(os.path.join(*bake_path))
55 55
56 def bake(self, factory, route, record_entry, 56 def bake(self, factory, route, record_entry,
57 taxonomy_name=None, taxonomy_term=None): 57 taxonomy_name=None, taxonomy_term=None):
58 custom_data = None 58 taxonomy = None
59 pagination_filter = None
60 route_metadata = dict(factory.metadata) 59 route_metadata = dict(factory.metadata)
61 if taxonomy_name and taxonomy_term: 60 if taxonomy_name and taxonomy_term:
62 # Must bake a taxonomy listing page... we'll have to add a 61 # TODO: add options for combining and slugifying terms
63 # pagination filter for only get matching posts, and the output 62 taxonomy = self.app.getTaxonomy(taxonomy_name)
64 # URL will be a bit different. 63 if taxonomy.is_multiple:
65 tax = self.app.getTaxonomy(taxonomy_name)
66 pagination_filter = PaginationFilter(
67 value_accessor=page_value_accessor)
68 if tax.is_multiple:
69 if isinstance(taxonomy_term, tuple): 64 if isinstance(taxonomy_term, tuple):
70 abc = AndBooleanClause()
71 for t in taxonomy_term:
72 abc.addClause(HasFilterClause(taxonomy_name, t))
73 pagination_filter.addClause(abc)
74 slugified_term = '/'.join(taxonomy_term) 65 slugified_term = '/'.join(taxonomy_term)
75 else: 66 else:
76 pagination_filter.addClause(
77 HasFilterClause(taxonomy_name, taxonomy_term))
78 slugified_term = taxonomy_term 67 slugified_term = taxonomy_term
79 else: 68 else:
80 pagination_filter.addClause(
81 IsFilterClause(taxonomy_name, taxonomy_term))
82 slugified_term = taxonomy_term 69 slugified_term = taxonomy_term
83 custom_data = {tax.term_name: taxonomy_term} 70 route_metadata.update({taxonomy.setting_name: slugified_term})
84 route_metadata.update({tax.term_name: slugified_term})
85 71
86 # Generate the URL using the route. 72 # Generate the URL using the route.
87 page = factory.buildPage() 73 page = factory.buildPage()
88 uri = route.getUri(route_metadata, provider=page, 74 uri = route.getUri(route_metadata, provider=page,
89 include_site_root=False) 75 include_site_root=False)
177 self.app.env.rendered_segments_repository.invalidate( 163 self.app.env.rendered_segments_repository.invalidate(
178 cache_key) 164 cache_key)
179 165
180 logger.debug(" p%d -> %s" % (cur_sub, out_path)) 166 logger.debug(" p%d -> %s" % (cur_sub, out_path))
181 ctx, rp = self._bakeSingle(page, sub_uri, cur_sub, out_path, 167 ctx, rp = self._bakeSingle(page, sub_uri, cur_sub, out_path,
182 pagination_filter, custom_data) 168 taxonomy, taxonomy_term)
183 except Exception as ex: 169 except Exception as ex:
184 if self.app.debug: 170 if self.app.debug:
185 logger.exception(ex) 171 logger.exception(ex)
186 page_rel_path = os.path.relpath(page.path, self.app.root_dir) 172 page_rel_path = os.path.relpath(page.path, self.app.root_dir)
187 raise BakingError("%s: error baking '%s'." % 173 raise BakingError("%s: error baking '%s'." %
217 ctx.used_pagination.has_more): 203 ctx.used_pagination.has_more):
218 cur_sub += 1 204 cur_sub += 1
219 has_more_subs = True 205 has_more_subs = True
220 206
221 def _bakeSingle(self, page, sub_uri, num, out_path, 207 def _bakeSingle(self, page, sub_uri, num, out_path,
222 pagination_filter=None, custom_data=None): 208 taxonomy=None, taxonomy_term=None):
223 ctx = PageRenderingContext(page, sub_uri) 209 ctx = PageRenderingContext(page, sub_uri)
224 ctx.page_num = num 210 ctx.page_num = num
225 if pagination_filter: 211 if taxonomy and taxonomy_term:
226 ctx.pagination_filter = pagination_filter 212 ctx.setTaxonomyFilter(taxonomy, taxonomy_term)
227 if custom_data:
228 ctx.custom_data = custom_data
229 213
230 rp = render_page(ctx) 214 rp = render_page(ctx)
231 215
232 out_dir = os.path.dirname(out_path) 216 out_dir = os.path.dirname(out_path)
233 if not os.path.isdir(out_dir): 217 if not os.path.isdir(out_dir):