Skip to content

Commit

Permalink
feat: add exclude content filter
Browse files Browse the repository at this point in the history
  • Loading branch information
SharleneNdinda committed Oct 17, 2023
1 parent d2bfe39 commit f3d0474
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
16 changes: 16 additions & 0 deletions mycarehub/content/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ def filter_queryset(self, request, queryset, view):
return queryset.distinct()


class ContentFilter(BaseFilterBackend):
"""
This filter excludes the content item given while returning all content
available on CMS.
"""

def filter_queryset(self, request, queryset, view):
query_params = request.query_params
exclude_content = query_params.get("exclude_content", "")

if exclude_content and queryset.model is ContentItem:
queryset = queryset.exclude(id=exclude_content)

return queryset


class ClientFilter(BaseFilterBackend):
"""
Implements the client_id filter which returns only pages that a specific client can view
Expand Down
15 changes: 15 additions & 0 deletions mycarehub/content/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,18 @@ def test_sms_content_item_tag_snippet_filter(sms_category, request_with_user, cl
url = reverse("wagtailsnippets_content_smscontentitemtag:list")
response = client.get(url)
assert response.status_code == status.HTTP_200_OK


def test_content_filter_exclude_content(
content_item_with_tag_and_category, request_with_user, client
):
"""Test exclude content item filter."""
url = (
reverse("wagtailapi:pages:listing") + f"?type=content.ContentItem"
f"&fields=*&exclude_content={content_item_with_tag_and_category.id}"
)
client.force_login(request_with_user.user)
response = client.get(url)
assert response.status_code == status.HTTP_200_OK
response_data = response.json()
assert response_data["meta"]["total_count"] == 0
3 changes: 3 additions & 0 deletions mycarehub/content/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from mycarehub.content.filters import (
CategoryFilter,
ClientFilter,
ContentFilter,
ContentItemCategoryFilter,
ContentSequenceFilter,
FacilityFilter,
Expand All @@ -33,6 +34,7 @@ class CustomPageAPIViewset(PagesAPIViewSet):
ContentSequenceFilter,
FacilityFilter,
CategoryFilter,
ContentFilter,
FieldsFilter,
ChildOfFilter,
AncestorOfFilter,
Expand All @@ -53,6 +55,7 @@ class CustomPageAPIViewset(PagesAPIViewSet):
"client_id",
"facility_id",
"exclude_category",
"exclude_content",
]
)

Expand Down

0 comments on commit f3d0474

Please sign in to comment.