Skip to content

Commit

Permalink
feat(category): add slug filters for category list
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed Aug 23, 2023
1 parent 34047ff commit 23f7d7e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion alexandria/core/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
from alexandria.core import models


class CharInFilter(BaseInFilter, CharFilter):
pass


class JSONValueFilter(Filter):
field_type_by_lookup_expr = {
"exact": TextField,
Expand Down Expand Up @@ -114,10 +118,12 @@ class CategoryFilterSet(FilterSet):
metainfo = JSONValueFilter(field_name="metainfo")
active_group = ActiveGroupFilter()
has_parent = BooleanFilter(field_name="parent", lookup_expr="isnull", exclude=True)
slug = CharFilter()
slugs = CharInFilter(field_name="slug", lookup_expr="in")

class Meta:
model = models.Category
fields = ["active_group", "metainfo", "has_parent"]
fields = ["active_group", "metainfo", "has_parent", "slug", "slugs"]


class DocumentFilterSet(FilterSet):
Expand Down
21 changes: 21 additions & 0 deletions alexandria/core/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,24 @@ def test_has_parent(db, admin_client, category_factory, has_parent, expected_cou

received_ids = set([obj["id"] for obj in result["data"]])
assert len(received_ids) == expected_count


@pytest.mark.parametrize(
"filters,expected_count",
[
({"filter[slug]": "category-1"}, 1),
({"filter[slug]": "category-none"}, 0),
({"filter[slugs]": "category-1,category-2"}, 2),
({"filter[slugs]": "category-1,category-none"}, 1),
],
)
def test_category_slug_filters(
db, admin_client, category_factory, filters, expected_count
):
category_factory(pk="category-1")
category_factory(pk="category-2")
category_factory(pk="category-3")

response = admin_client.get(reverse("category-list"), filters)
assert response.status_code == HTTP_200_OK
assert len(response.json()["data"]) == expected_count

0 comments on commit 23f7d7e

Please sign in to comment.