def test_set_authors_filters_tags_on_pageids(self, mock_tag_objects, mock_init): mock_init.return_value = None page_ids = [1, 2, 3, 4, 5] form = FilterableListForm() form.fields = {'authors': mock.Mock()} form.set_authors(page_ids=page_ids) mock_tag_objects.filter.assert_called_with(v1_cfgovauthoredpages_items__content_object__id__in=page_ids)
def test_generate_query_returns_empty_query_for_unbound_form(self, mock_init): mock_init.return_value = None form = FilterableListForm() form.is_bound = False result = form.generate_query() assert result.children == []
def test_generate_query_returns_empty_query_for_unbound_form(self, mock_init): mock_init.return_value = None form = FilterableListForm() form.is_bound = False result = form.generate_query() assert result.children == []
def test_set_authors_filters_tags_on_pageids(self, mock_tag_objects, mock_init): mock_init.return_value = None page_ids = [1, 2, 3, 4, 5] form = FilterableListForm() form.fields = {'authors': mock.Mock()} form.set_authors(page_ids=page_ids) mock_tag_objects.filter.assert_called_with(v1_cfgovauthoredpages_items__content_object__id__in=page_ids)
def setUpFilterableForm(self, data=None): filterable_pages = AbstractFilterPage.objects.live() form = FilterableListForm(filterable_pages=filterable_pages, wagtail_block=None) form.is_bound = True form.cleaned_data = data return form
def setUpFilterableForm(self, data=None): hostname = Site.objects.get(is_default_site=True).hostname base_query = AbstractFilterPage.objects.live_shared(hostname) form = FilterableListForm(hostname=hostname, base_query=base_query) form.is_bound = True form.cleaned_data = data return form
def setUpFilterableForm(self, data=None): hostname = Site.objects.get(is_default_site=True).hostname base_query = AbstractFilterPage.objects.live_shared(hostname) form = FilterableListForm(hostname=hostname, base_query=base_query) form.is_bound = True form.cleaned_data = data return form
def test_generate_query_returns_query_from_cleaned_data_fields_and_query_strings(self, mock_init, mock_get_query_strings): mock_init.return_value = None form = FilterableListForm() form.is_bound = True form.declared_fields = ['field'] mock_get_query_strings.return_value = ['field__contains'] form.cleaned_data = {'field': 'foobar'} result = form.generate_query() assert result.children == [('field__contains', 'foobar')]
def test_generate_query_returns_empty_query_fields_not_in_cleaned_data(self, mock_init, mock_get_query_strings): mock_init.return_value = None form = FilterableListForm() form.is_bound = True form.declared_fields = ['field'] mock_get_query_strings.return_value = ['field__contains'] form.cleaned_data = {'notthefield': None} result = form.generate_query() assert result.children == []
def get_filtered_pages(self, data): filterable_pages = AbstractFilterPage.objects.live() form = FilterableListForm(filterable_pages=filterable_pages, wagtail_block=None, filterable_root='/', filterable_categories=None, data=data) self.assertTrue(form.is_valid()) return form.get_page_set()
def test_clean_returns_cleaned_data_if_valid(self, mock_super, mock_init): mock_init.return_value = None from_date = datetime.date.today() to_date = from_date + datetime.timedelta(days=1) form = FilterableListForm() mock_super().clean.return_value = {'from_date': from_date, 'to_date': to_date} form.cleaned_data = {'from_date': from_date, 'to_date': to_date} result = form.clean() assert result['from_date'] == from_date assert result['to_date'] == to_date
def test_clean_returns_cleaned_data_if_valid(self, mock_super, mock_init): mock_init.return_value = None from_date = datetime.date.today() to_date = from_date + datetime.timedelta(days=1) form = FilterableListForm() mock_super().clean.return_value = {'from_date': from_date, 'to_date': to_date} form.cleaned_data = {'from_date': from_date, 'to_date': to_date} result = form.clean() assert result['from_date'] == from_date assert result['to_date'] == to_date
def test_clean_returns_cleaned_data_if_only_one_date_field_is_empty(self, mock_super, mock_init): mock_init.return_value = None from_date = datetime.date.today() to_date = '' form = FilterableListForm() mock_super().clean.return_value = {'from_date': from_date, 'to_date': to_date} form.cleaned_data = {'from_date': from_date, 'to_date': to_date} result = form.clean() assert result['from_date'] == from_date assert result['to_date'] == to_date
def test_clean_returns_cleaned_data_if_both_date_fields_are_empty(self, mock_super, mock_init): mock_init.return_value = None from_date = '' to_date = '' form = FilterableListForm() mock_super().clean.return_value = {'from_date': from_date, 'to_date': to_date} form.cleaned_data = {'from_date': from_date, 'to_date': to_date} result = form.clean() assert result['from_date'] == from_date assert result['to_date'] == to_date
def test_clean_returns_cleaned_data_if_only_one_date_field_is_empty(self, mock_super, mock_init): mock_init.return_value = None from_date = datetime.date.today() to_date = '' form = FilterableListForm() mock_super().clean.return_value = {'from_date': from_date, 'to_date': to_date} form.cleaned_data = {'from_date': from_date, 'to_date': to_date} result = form.clean() assert result['from_date'] == from_date assert result['to_date'] == to_date
def test_clean_returns_cleaned_data_if_both_date_fields_are_empty(self, mock_super, mock_init): mock_init.return_value = None from_date = '' to_date = '' form = FilterableListForm() mock_super().clean.return_value = {'from_date': from_date, 'to_date': to_date} form.cleaned_data = {'from_date': from_date, 'to_date': to_date} result = form.clean() assert result['from_date'] == from_date assert result['to_date'] == to_date
def test_clean_switches_date_fields_if_todate_is_less_than_fromdate(self, mock_super, mock_init): mock_init.return_value = None to_date = datetime.date.today() from_date = to_date + datetime.timedelta(days=1) form = FilterableListForm() mock_super().clean.return_value = {'from_date': from_date, 'to_date': to_date} form.cleaned_data = {'from_date': from_date, 'to_date': to_date} form.data = {'from_date': from_date, 'to_date': to_date} result = form.clean() assert result['from_date'] == to_date assert result['to_date'] == from_date
def test_clean_switches_date_fields_if_todate_is_less_than_fromdate(self, mock_super, mock_init): mock_init.return_value = None to_date = datetime.date.today() from_date = to_date + datetime.timedelta(days=1) form = FilterableListForm() mock_super().clean.return_value = {'from_date': from_date, 'to_date': to_date} form.cleaned_data = {'from_date': from_date, 'to_date': to_date} form.data = {'from_date': from_date, 'to_date': to_date} result = form.clean() assert result['from_date'] == to_date assert result['to_date'] == from_date
def get_browsefilterable_posts(self, request, limit): hostname = request.site.hostname filter_pages = [p.specific for p in self.get_appropriate_descendants(hostname) if 'FilterablePage' in p.specific_class.__name__ and 'archive' not in p.title.lower()] posts_tuple_list = [] for page in filter_pages: base_query = AbstractFilterPage.objects.live_shared(hostname).filter(CFGOVPage.objects.child_of_q(page)) logger.info('Filtering by parent {}'.format(page)) form_id = str(page.form_id()) form = FilterableListForm(hostname=hostname, base_query=base_query) for post in form.get_page_set(): posts_tuple_list.append((form_id, post)) return sorted(posts_tuple_list, key=lambda p: p[1].date_published, reverse=True)[:limit]
def test_clean_returns_cleaned_data_if_valid(self, mock_super, mock_init): mock_init.return_value = None from_date = datetime.date(2017, 7, 4) to_date = from_date + datetime.timedelta(days=1) form_data = {'from_date': from_date, 'to_date': to_date} form = FilterableListForm() mock_super().clean.return_value = form_data form.cleaned_data = form_data form.data = {'from_date': '7/4/2017', 'to_date': '7/5/2017'} form._errors = {} result = form.clean() assert result['from_date'] == from_date assert result['to_date'] == to_date
def test_clean_uses_today_if_todate_field_is_empty(self, mock_super, mock_init): mock_init.return_value = None from_date = datetime.date(2016, 5, 15) to_date = None form_data = {'from_date': from_date, 'to_date': to_date} form = FilterableListForm() mock_super().clean.return_value = form_data form.cleaned_data = form_data form.data = {'from_date': '5-15-2016'} form._errors = {} result = form.clean() assert result['from_date'] == from_date assert result['to_date'] == datetime.date.today()
def get_forms(self, request): hostname = request.site.hostname for form_data in self.get_form_specific_filter_data( request_dict=request.GET): yield FilterableListForm(form_data, hostname=hostname, base_query=self.base_query(hostname))
def get_browsefilterable_posts(self, limit): filter_pages = [ p.specific for p in self.get_appropriate_descendants() if 'FilterablePage' in p.specific_class.__name__ and 'archive' not in p.title.lower() ] posts_list = [] for page in filter_pages: eligible_children = AbstractFilterPage.objects.live().filter( CFGOVPage.objects.child_of_q(page)) form = FilterableListForm(filterable_pages=eligible_children) for post in form.get_page_set(): posts_list.append(post) return sorted(posts_list, key=lambda p: p.date_published, reverse=True)[:limit]
def test_clean_uses_earliest_result_if_fromdate_field_is_empty(self, mock_super, mock_init, mock_pub_date): mock_init.return_value = None from_date = None to_date = datetime.date(2017, 1, 15) form_data = {'from_date': from_date, 'to_date': to_date} form = FilterableListForm() mock_super().clean.return_value = form_data form.cleaned_data = form_data form.data = {'to_date': '1-15-2017'} form._errors = {} mock_pub_date.return_value = datetime.date(1995, 1, 1) result = form.clean() assert result['from_date'] == datetime.date(1995, 1, 1) assert result['to_date'] == to_date
def get_browsefilterable_posts(self, limit): filter_pages = [p.specific for p in self.get_appropriate_descendants() if 'FilterablePage' in p.specific_class.__name__ and 'archive' not in p.title.lower()] posts_list = [] for page in filter_pages: eligible_children = AbstractFilterPage.objects.live().filter( CFGOVPage.objects.child_of_q(page) ) form = FilterableListForm(filterable_pages=eligible_children) for post in form.get_page_set(): posts_list.append(post) return sorted(posts_list, key=lambda p: p.date_published, reverse=True)[:limit]
def get_context(self, request, *args, **kwargs): context = super(FilterableListMixin, self).get_context(request, *args, **kwargs) form_data, has_active_filters = self.get_form_data(request.GET) form = FilterableListForm(form_data, filterable_pages=self.filterable_pages()) context.update({ 'filter_data': self.process_form(request, form), 'get_secondary_nav_items': get_secondary_nav_items, 'has_active_filters': has_active_filters, }) return context
def test_generate_query_returns_query_from_cleaned_data_fields_and_query_strings(self, mock_init, mock_get_query_strings): mock_init.return_value = None form = FilterableListForm() form.is_bound = True form.declared_fields = ['field'] mock_get_query_strings.return_value = ['field__contains'] form.cleaned_data = {'field': 'foobar'} result = form.generate_query() assert result.children == [('field__contains', 'foobar')]
def test_generate_query_returns_empty_query_fields_not_in_cleaned_data(self, mock_init, mock_get_query_strings): mock_init.return_value = None form = FilterableListForm() form.is_bound = True form.declared_fields = ['field'] mock_get_query_strings.return_value = ['field__contains'] form.cleaned_data = {'notthefield': None} result = form.generate_query() assert result.children == []
def test_clean_returns_cleaned_data_if_valid(self, mock_super, mock_init): mock_init.return_value = None from_date = datetime.date(2017, 7, 4) to_date = from_date + datetime.timedelta(days=1) form_data = {'from_date': from_date, 'to_date': to_date} form = FilterableListForm() mock_super().clean.return_value = form_data form.cleaned_data = form_data form.data = {'from_date': '7/4/2017', 'to_date': '7/5/2017'} form._errors = {} result = form.clean() assert result['from_date'] == from_date assert result['to_date'] == to_date
def test_clean_uses_today_if_todate_field_is_empty(self, mock_super, mock_init): mock_init.return_value = None from_date = datetime.date(2016, 5, 15) to_date = None form_data = {'from_date': from_date, 'to_date': to_date} form = FilterableListForm() mock_super().clean.return_value = form_data form.cleaned_data = form_data form.data = {'from_date': '5-15-2016'} form._errors = {} result = form.clean() assert result['from_date'] == from_date assert result['to_date'] == datetime.date.today()
def test_clean_uses_earliest_result_if_fromdate_field_is_empty(self, mock_super, mock_init, mock_pub_date): mock_init.return_value = None from_date = None to_date = datetime.date(2017, 1, 15) form_data = {'from_date': from_date, 'to_date': to_date} form = FilterableListForm() mock_super().clean.return_value = form_data form.cleaned_data = form_data form.data = {'to_date': '1-15-2017'} form._errors = {} mock_pub_date.return_value = datetime.date(1995, 1, 1) result = form.clean() assert result['from_date'] == datetime.date(1995, 1, 1) assert result['to_date'] == to_date
def setUpFilterableForm(self, data=None): filterable_pages = AbstractFilterPage.objects.live() form = FilterableListForm(filterable_pages=filterable_pages) form.is_bound = True form.cleaned_data = data return form