Mercurial > piecrust2
comparison piecrust/generation/taxonomy.py @ 720:3e188d88a9ac
bake: Fix some bugs with taxonomy combinations.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 28 May 2016 15:43:45 -0700 |
parents | ab5c6a8ae90a |
children | 606f6d57b5df |
comparison
equal
deleted
inserted
replaced
719:a066f4ac9094 | 720:3e188d88a9ac |
---|---|
82 self._setTaxonomyFilter(ctx, tax_terms, is_combination) | 82 self._setTaxonomyFilter(ctx, tax_terms, is_combination) |
83 | 83 |
84 ctx.custom_data = { | 84 ctx.custom_data = { |
85 self.taxonomy.term_name: tax_terms, | 85 self.taxonomy.term_name: tax_terms, |
86 'is_multiple_%s' % self.taxonomy.term_name: is_combination} | 86 'is_multiple_%s' % self.taxonomy.term_name: is_combination} |
87 if (self.taxonomy.is_multiple and | |
88 self.taxonomy.name != self.taxonomy.term_name): | |
89 mult_val = tax_terms | |
90 if not is_combination: | |
91 mult_val = (mult_val,) | |
92 ctx.custom_data[self.taxonomy.name] = mult_val | |
87 logger.debug("Prepared render context with: %s" % ctx.custom_data) | 93 logger.debug("Prepared render context with: %s" % ctx.custom_data) |
88 | 94 |
89 def _getTaxonomyTerms(self, route_metadata): | 95 def _getTaxonomyTerms(self, route_metadata): |
90 all_values = route_metadata.get(self.taxonomy.term_name) | 96 all_values = route_metadata.get(self.taxonomy.term_name) |
91 if all_values is None: | 97 if all_values is None: |
133 level=logging.DEBUG, colored=False): | 139 level=logging.DEBUG, colored=False): |
134 all_terms, dirty_terms = self._buildDirtyTaxonomyTerms(ctx) | 140 all_terms, dirty_terms = self._buildDirtyTaxonomyTerms(ctx) |
135 | 141 |
136 start_time = time.perf_counter() | 142 start_time = time.perf_counter() |
137 page_count = self._bakeTaxonomyTerms(ctx, all_terms, dirty_terms) | 143 page_count = self._bakeTaxonomyTerms(ctx, all_terms, dirty_terms) |
138 logger.info(format_timed(start_time, | 144 logger.info(format_timed( |
139 "baked %d taxonomy pages." % page_count)) | 145 start_time, |
146 "baked %d %s pages." % (page_count, self.taxonomy.term_name))) | |
140 | 147 |
141 def _buildDirtyTaxonomyTerms(self, ctx): | 148 def _buildDirtyTaxonomyTerms(self, ctx): |
142 # Build the list of terms for our taxonomy, and figure out which ones | 149 # Build the list of terms for our taxonomy, and figure out which ones |
143 # are 'dirty' for the current bake. | 150 # are 'dirty' for the current bake. |
144 logger.debug("Gathering dirty taxonomy terms") | 151 logger.debug("Gathering dirty taxonomy terms") |
145 all_terms = set() | 152 all_terms = set() |
146 dirty_terms = set() | 153 single_dirty_terms = set() |
147 | 154 |
148 # Re-bake all taxonomy terms that include new or changed pages. | 155 # Re-bake all taxonomy terms that include new or changed pages. |
149 for prev_entry, cur_entry in ctx.getBakedPageRecords(): | 156 for prev_entry, cur_entry in ctx.getBakedPageRecords(): |
150 entries = [cur_entry] | 157 entries = [cur_entry] |
151 if prev_entry: | 158 if prev_entry: |
157 if entry_terms: | 164 if entry_terms: |
158 if not self.taxonomy.is_multiple: | 165 if not self.taxonomy.is_multiple: |
159 terms.append(entry_terms) | 166 terms.append(entry_terms) |
160 else: | 167 else: |
161 terms += entry_terms | 168 terms += entry_terms |
162 if terms: | 169 single_dirty_terms.update(terms) |
163 dirty_terms.update([(t,) for t in terms]) | |
164 | 170 |
165 # Remember all terms used. | 171 # Remember all terms used. |
166 for _, cur_entry in ctx.getAllPageRecords(): | 172 for _, cur_entry in ctx.getAllPageRecords(): |
167 if cur_entry and not cur_entry.was_overriden: | 173 if cur_entry and not cur_entry.was_overriden: |
168 cur_terms = cur_entry.config.get(self.taxonomy.setting_name) | 174 cur_terms = cur_entry.config.get(self.taxonomy.setting_name) |
171 all_terms.add(cur_terms) | 177 all_terms.add(cur_terms) |
172 else: | 178 else: |
173 all_terms |= set(cur_terms) | 179 all_terms |= set(cur_terms) |
174 | 180 |
175 # Re-bake the combination pages for terms that are 'dirty'. | 181 # Re-bake the combination pages for terms that are 'dirty'. |
182 # We make all terms into tuple, even those that are not actual | |
183 # combinations, so that we have less things to test further down the | |
184 # line. | |
185 dirty_terms = [(t,) for t in single_dirty_terms] | |
186 # Add the combinations to that list. | |
176 if self.taxonomy.is_multiple: | 187 if self.taxonomy.is_multiple: |
177 known_combinations = set() | 188 known_combinations = set() |
178 logger.debug("Gathering dirty term combinations") | 189 logger.debug("Gathering dirty term combinations") |
179 for _, cur_entry in ctx.getAllPageRecords(): | 190 for _, cur_entry in ctx.getAllPageRecords(): |
180 if cur_entry: | 191 if cur_entry: |
182 for terms in used_terms: | 193 for terms in used_terms: |
183 if len(terms) > 1: | 194 if len(terms) > 1: |
184 known_combinations.add(terms) | 195 known_combinations.add(terms) |
185 | 196 |
186 for terms in known_combinations: | 197 for terms in known_combinations: |
187 if not dirty_terms.isdisjoint(set(terms)): | 198 if not single_dirty_terms.isdisjoint(set(terms)): |
188 dirty_terms.add(terms) | 199 dirty_terms.append(terms) |
189 | 200 |
190 return all_terms, dirty_terms | 201 return all_terms, dirty_terms |
191 | 202 |
192 def _bakeTaxonomyTerms(self, ctx, all_terms, dirty_terms): | 203 def _bakeTaxonomyTerms(self, ctx, all_terms, dirty_terms): |
193 # Start baking those terms. | 204 # Start baking those terms. |
252 def _get_all_entry_taxonomy_terms(entry): | 263 def _get_all_entry_taxonomy_terms(entry): |
253 res = set() | 264 res = set() |
254 for o in entry.subs: | 265 for o in entry.subs: |
255 for pinfo in o.render_info: | 266 for pinfo in o.render_info: |
256 if pinfo: | 267 if pinfo: |
257 res |= set(pinfo.getCustomInfo('used_taxonomy_terms', [])) | 268 terms = pinfo.getCustomInfo('used_taxonomy_terms') |
269 if terms: | |
270 res |= set(terms) | |
258 return res | 271 return res |
259 | 272 |
260 | 273 |
261 class HasTaxonomyTermsFilterClause(SettingFilterClause): | 274 class HasTaxonomyTermsFilterClause(SettingFilterClause): |
262 def __init__(self, taxonomy, slugify_mode, value, is_combination): | 275 def __init__(self, taxonomy, slugify_mode, value, is_combination): |