def _materialize_auto_blocks(self): """Replace automatic teaser blocks by teaser blocks with same content and same attributes (e.g. `layout`). (Make sure this method only runs when #automatic is enabled, otherwise IRenderedArea will not retrieve results from SOLR / referenced CP.) """ order = self.keys() for old in zeit.content.cp.interfaces.IRenderedArea(self).values(): if not IAutomaticTeaserBlock.providedBy(old): continue # Delete automatic teaser first, since adding normal teaser will # delete the last automatic teaser via the # `adjust_auto_blocks_to_count` event handler. # (Deleting doesn't remove __name__ or __parent__, so we can still # copy those afterwards) del self[old.__name__] new = self.create_item("teaser") new.update(old) # Preserve order of non-auto blocks. self.updateOrder(order) # Remove unfilled auto blocks. for block in list(self.values()): if IAutomaticTeaserBlock.providedBy(block): del self[block.__name__]
def _materialize_auto_blocks(self): """Replace automatic teaser blocks by teaser blocks with same content and same attributes (e.g. `layout`). (Make sure this method only runs when #automatic is enabled, otherwise IRenderedArea will not retrieve results from the content query source.) """ order = self.keys() for old in zeit.content.cp.interfaces.IRenderedArea(self).values(): if not IAutomaticTeaserBlock.providedBy(old): continue # Delete automatic teaser first, since adding normal teaser will # delete the last automatic teaser via the # `adjust_auto_blocks_to_count` event handler. # (Deleting doesn't remove __name__ or __parent__, so we can still # copy those afterwards) del self[old.__name__] new = self.create_item('teaser') new.update(old) # Preserve order of non-auto blocks. self.updateOrder(order) # Remove unfilled auto blocks. for block in list(self.values()): if IAutomaticTeaserBlock.providedBy(block): del self[block.__name__]
def values(self): if not self.automatic: return self.context.values() self._v_try_to_retrieve_content = True self._v_retrieved_content = 0 content = self._retrieve_content() result = [] for block in self.context.values(): if not IAutomaticTeaserBlock.providedBy(block): result.append(block) continue # This assumes that the *first* block always has a leader layout, # since otherwise the first result that may_be_leader might be # given to a non-leader block. if block.layout.id in ['leader', 'zon-large']: teaser = self._extract_newest( content, predicate=is_lead_candidate) if teaser is None: teaser = self._extract_newest(content) block.change_layout( zeit.content.cp.layout.get_layout('buttons')) else: teaser = self._extract_newest(content) if teaser is None: continue block.insert(0, teaser) result.append(block) return result
def values(self): if not self.automatic: return self.context.values() try: content = self._content_query() except LookupError: log.warning('%s found no IContentQuery type %s', self.context, self.automatic_type) return self.context.values() result = [] for block in self.context.values(): if not IAutomaticTeaserBlock.providedBy(block): result.append(block) continue # This assumes that the *first* block always has a leader layout, # since otherwise the first result that may_be_leader might be # given to a non-leader block. if self.context.require_lead_candidates and block.layout.is_leader: teaser = pop_filter(content, is_lead_candidate) if teaser is None: teaser = pop_filter(content) block.change_layout(self.context.default_teaser_layout) else: teaser = pop_filter(content) if teaser is None: continue block.insert(0, teaser) result.append(block) return result
def test_removing_manual_teaser_automatically_adds_auto_teaser(self): from zeit.content.cp.interfaces import IAutomaticTeaserBlock lead = self.repository['cp']['lead'] lead.count = 2 lead.automatic = True manual_teaser1 = lead.create_item('teaser') manual_teaser2 = lead.create_item('teaser') self.assertEqual([manual_teaser1, manual_teaser2], lead.values()) del lead[manual_teaser1.__name__] self.assertNotIn(manual_teaser1, lead.values()) self.assertIn(manual_teaser2, lead.values()) self.assertTrue(IAutomaticTeaserBlock.providedBy(lead.values()[-1]))
def test_removing_manual_teaser_automatically_adds_auto_teaser(self): from zeit.content.cp.interfaces import IAutomaticTeaserBlock lead = self.repository['cp']['lead'] lead.count = 2 lead.automatic = True manual_teaser1 = lead.create_item('teaser') manual_teaser2 = lead.create_item('teaser') self.assertEqual([manual_teaser1, manual_teaser2], lead.values()) del lead[manual_teaser1.__name__] self.assertNotIn(manual_teaser1, lead.values()) self.assertIn(manual_teaser2, lead.values()) self.assertTrue(IAutomaticTeaserBlock.providedBy(lead.values()[-1]))
def adjust_auto_blocks_to_count(self): """Does not touch any block that is not an IAutomaticTeaserBlock, so only the number of _automatic_ teasers is configured via the `Area.count` setting. Thus we may contain more than `Area.count` teasers. """ if not self.automatic: return automatic_blocks = [x for x in self.values() if IAutomaticTeaserBlock.providedBy(x)] while self.count < len(self) and len(automatic_blocks) > 0: block = automatic_blocks.pop(-1) del self[block.__name__] while self.count > len(self): self.create_item("auto-teaser")
def adjust_auto_blocks_to_count(context, event): if IAutomaticTeaserBlock.providedBy(context): return # avoid infty loop when adding / deleting auto teaser area = context.__parent__ area.adjust_auto_blocks_to_count()
def adjust_auto_blocks_to_count(context, event): if IAutomaticTeaserBlock.providedBy(context): return # avoid infty loop when adding / deleting auto teaser area = context.__parent__ area.adjust_auto_blocks_to_count()