Skip to content

Commit

Permalink
feat: fetch content by slug
Browse files Browse the repository at this point in the history
  • Loading branch information
Muchogoc committed Oct 17, 2023
1 parent bde27b1 commit 5e922cf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
31 changes: 31 additions & 0 deletions mycarehub/content/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,34 @@ def test_content_view_with_no_attached_pdf_document_media(

response_data = response.json()
assert response_data["id"] != ""


def test_content_detail_view(
content_item_with_tag_and_category,
request_with_user,
client,
):
"""
Test fetching content item using id or slug.
"""
client.force_login(request_with_user.user)

pk_url = reverse(
"wagtailapi:pages:detail", kwargs={"pk": content_item_with_tag_and_category.id}
)
response = client.get(pk_url)

assert response.status_code == status.HTTP_200_OK
response_data = response.json()

assert response_data["id"] == content_item_with_tag_and_category.id

slug_url = reverse(
"wagtailapi:pages:detail", kwargs={"slug": content_item_with_tag_and_category.slug}
)
response_two = client.get(slug_url)

assert response_two.status_code == status.HTTP_200_OK
response_data = response_two.json()

assert response_data["id"] == content_item_with_tag_and_category.id
20 changes: 20 additions & 0 deletions mycarehub/content/views/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.urls import path
from wagtail.api.v2.filters import (
AncestorOfFilter,
ChildOfFilter,
Expand Down Expand Up @@ -59,6 +60,25 @@ class CustomPageAPIViewset(PagesAPIViewSet):
]
)

def detail_view(self, request, pk=None, slug=None):
param = pk
if slug is not None:
self.lookup_field = "slug"
param = slug
return super().detail_view(request, param)

@classmethod
def get_urlpatterns(cls):
"""
This returns a list of URL patterns for the endpoint
"""
return [
path("", cls.as_view({"get": "listing_view"}), name="listing"),
path("<int:pk>/", cls.as_view({"get": "detail_view"}), name="detail"),
path("<slug:slug>/", cls.as_view({"get": "detail_view"}), name="detail"),
path("find/", cls.as_view({"get": "find_view"}), name="find"),
]


class ContentItemCategoryViewSet(BaseView):
queryset = ContentItemCategory.objects.all()
Expand Down

0 comments on commit 5e922cf

Please sign in to comment.