def filterable_pages(self): """Return pages that are eligible to be filtered by this page. Always includes only live pages and pages that live in the same Wagtail site as this page. If this page cannot be mapped to a Wagtail site (for example, if it does not live under a site root), then it will not return any filterable results. The class property filterable_categories can be set to a list of page categories from the set in v1.util.ref.categories. If set, this page will only filter pages that are tagged with a tag in those categories. By default this is an empty list and all page tags are eligible. The class property filterable_children_only determines whether this page filters only pages that are direct children of this page. By default this is True; set this to False to allow this page to filter pages that are not direct children of this page. """ site = self.get_site() if not site: return self.get_model_class().objects.none() pages = self.get_model_class().objects.in_site(site).live() if self.filterable_categories: category_names = get_category_children(self.filterable_categories) pages = pages.filter(categories__name__in=category_names) if self.filterable_children_only: pages = pages.child_of(self) return pages
def get_page_set(self): if flag_enabled('ELASTICSEARCH_FILTERABLE_LISTS'): categories = self.cleaned_data.get('categories') # If no categories are submitted by the form if categories == []: # And we have defined a prexisting set of categories # to limit results by Using CategoryFilterableMixin if self.filterable_categories not in ([], None): # Search for results only within the provided categories categories = ref.get_category_children( self.filterable_categories) return FilterablePagesDocumentSearch( prefix=self.filterable_root, topics=self.cleaned_data.get('topics'), categories=categories, authors=self.cleaned_data.get('authors'), to_date=self.cleaned_data.get('to_date'), from_date=self.cleaned_data.get('from_date'), title=self.cleaned_data.get('title'), archived=self.cleaned_data.get('archived'), order_by=self.get_order_by()).search() else: query = self.generate_query() return self.filterable_pages.filter(query).distinct().order_by( '-date_published' )
def filterable_pages(self): """Return pages that are eligible to be filtered by this page. Always includes only live pages and pages that live in the same Wagtail site as this page. If this page cannot be mapped to a Wagtail site (for example, if it does not live under a site root), then it will not return any filterable results. The class property filterable_categories can be set to a list of page categories from the set in v1.util.ref.categories. If set, this page will only filter pages that are tagged with a tag in those categories. By default this is an empty list and all page tags are eligible. The class property filterable_children_only determines whether this page filters only pages that are direct children of this page. By default this is True; set this to False to allow this page to filter pages that are not direct children of this page. """ site = self.get_site() if not site: return AbstractFilterPage.objects.none() pages = AbstractFilterPage.objects.in_site(site).live() if self.filterable_categories: category_names = get_category_children(self.filterable_categories) pages = pages.filter(categories__name__in=category_names) if self.filterable_children_only: pages = pages.child_of(self) return pages
def test_eligible_categories(self): self.assertEqual( get_category_children(NewsroomLandingPage.filterable_categories), [ 'op-ed', 'press-release', 'speech', 'testimony', ])
def get_filterable_queryset(self): """Return the queryset of pages to be filtered by this page. The class property filterable_categories can be set to a list of page categories from the set in v1.util.ref.categories. If set, this page will only filter pages that are tagged with a tag in those categories. By default this is an empty list and all page tags are eligible. """ queryset = super().get_filterable_queryset() category_names = get_category_children(self.filterable_categories) return queryset.filter(categories__name__in=category_names)
def test_eligible_categories(self): self.assertEqual( get_category_children(NewsroomLandingPage.filterable_categories), [ 'directors-notebook', 'op-ed', 'press-release', 'speech', 'testimony', ] )
def test_eligible_categories(self): self.assertEqual( get_category_children(NewsroomLandingPage.filterable_categories), [ 'at-the-cfpb', 'data-research-reports', 'info-for-consumers', 'op-ed', 'policy_compliance', 'press-release', 'speech', 'testimony', ])
def test_eligible_categories(self): self.assertEqual( get_category_children(NewsroomLandingPage.filterable_categories), [ 'at-the-cfpb', 'data-research-reports', 'info-for-consumers', 'op-ed', 'policy_compliance', 'press-release', 'speech', 'testimony', ] )
def test_get_children_with_invalid_category_raises_keyerror(self): with self.assertRaises(KeyError): get_category_children(['This is not a valid category'])
def test_get_children_of_multiple_categories(self): self.assertEqual( get_category_children(['Final rule', 'Implementation Resource']), [ 'compliance-aid', 'final-rule', 'interim-final-rule', 'official-guidance' ])
def test_get_children_of_single_category(self): self.assertEqual(get_category_children(['Amicus Brief']), [ 'fed-circuit-court', 'fed-district-court', 'state-court', 'us-supreme-court' ])
def test_get_children_of_multiple_categories(self): self.assertEqual( get_category_children(['Final Rule', 'Implementation Resource']), ['compliance-aid', 'final-rule', 'interim-final-rule', 'official-guidance'] )
def test_get_children_of_single_category(self): self.assertEqual( get_category_children(['Amicus Brief']), ['fed-circuit-court', 'fed-district-court', 'state-court', 'us-supreme-court'] )