Skip to content
Snippets Groups Projects
Commit 127a0b47 authored by Martin Burchell's avatar Martin Burchell
Browse files

Ignore empty term filter

This was causing zero results to be returned if only the taxonomy was specified
and not the term
parent c0c2f4b9
Branches
No related tags found
1 merge request!102Filter fixes
......@@ -159,6 +159,23 @@ def test_filter_by_term_works_when_term_name_includes_colon():
assert payload[0]['body'] == item['body']
@pytest.mark.django_db
def test_empty_term_filter_ignored():
taxonomy = create_taxonomy(name='taxonomy').data
term = add_term(taxonomy=taxonomy['slug'], name='my term').data
item1 = create_item(body='item 1').data
item2 = create_item(body='item 2').data
categorize_item(item1, term)
term_filter = '{}:'.format(taxonomy['slug'])
payload = get(data={'terms': [term_filter]}).data
assert len(payload) == 2
assert payload[0]['body'] == item1['body']
assert payload[1]['body'] == item2['body']
@pytest.mark.django_db
def test_item_listed_with_associated_terms():
# TODO: Refactor to use the REST API when we can add
......
......@@ -63,14 +63,15 @@ class ItemViewSet(viewsets.ModelViewSet, BulkDestroyModelMixin):
terms = self.request.query_params.getlist('terms', [])
for taxonomy_and_term in terms:
(taxonomy, term) = taxonomy_and_term.split(':', 1)
matches = Term.objects.filter(
name=term, taxonomy__slug=taxonomy
)
if len(matches) == 0:
# If the term doesn't exist, there can be no matches
return Item.objects.none()
if term:
matches = Term.objects.filter(
name=term, taxonomy__slug=taxonomy
)
if len(matches) == 0:
# If the term doesn't exist, there can be no matches
return Item.objects.none()
items = items.filter(terms__id=matches[0].id)
items = items.filter(terms__id=matches[0].id)
location = self.request.query_params.get('location', None)
if location is not None:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment