Skip to content

Commit

Permalink
#105 added a theme list endpoint to cast api
Browse files Browse the repository at this point in the history
  • Loading branch information
ephes committed Sep 2, 2023
1 parent b464678 commit 1f32884
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
2 changes: 2 additions & 0 deletions cast/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
re_path(r"facet_counts/(?P<pk>\d+)/?$", views.FacetCountsDetailView.as_view(), name="facet-counts-detail"),
# comment training data
path("comment_training_data/", views.CommentTrainingDataView.as_view(), name="comment-training-data"),
# themes
path("themes/", views.ThemeListView.as_view(), name="theme-list"),
# wagtail api
path("wagtail/", include((views.wagtail_api_router.get_urlpatterns(), "api"), namespace="wagtail")),
]
33 changes: 31 additions & 2 deletions cast/api/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from collections import OrderedDict
from typing import Any
from typing import Any, cast

from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import QuerySet
Expand All @@ -20,7 +20,15 @@

from ..filters import PostFilterset
from ..forms import VideoForm
from ..models import Audio, Blog, SpamFilter, Video
from ..models import (
Audio,
Blog,
SpamFilter,
Video,
get_template_base_dir,
get_template_base_dir_choices,
)
from ..views import HtmxHttpRequest
from .serializers import (
AudioPodloveSerializer,
AudioSerializer,
Expand All @@ -45,6 +53,7 @@ def api_root(request: Request) -> Response:
("videos", request.build_absolute_uri(reverse("cast:api:video_list"))),
("audios", request.build_absolute_uri(reverse("cast:api:audio_list"))),
("comment_training_data", request.build_absolute_uri(reverse("cast:api:comment-training-data"))),
("themes", request.build_absolute_uri(reverse("cast:api:theme-list"))),
("pages", request.build_absolute_uri(reverse("cast:api:wagtail:pages:listing"))),
("images", request.build_absolute_uri(reverse("cast:api:wagtail:images:listing"))),
("facet_counts", request.build_absolute_uri(reverse("cast:api:facet-counts-list"))),
Expand Down Expand Up @@ -137,6 +146,26 @@ def get(request, _format: Any = None) -> JsonResponse:
return JsonResponse(train, safe=False)


class ThemeListView(generics.ListAPIView):
"""
Return a list of available themes. Mark the currently selected theme.
This is used by the theme switcher for the vue frontend for example.
"""

def get_queryset(self) -> None:
return None

def list(self, request: Request, *args, **kwargs) -> Response:
choices = get_template_base_dir_choices()
request = cast(HtmxHttpRequest, request)
template_base_dir = get_template_base_dir(request, None)
results = []
for slug, name in choices:
selected = slug == template_base_dir
results.append({"slug": slug, "name": name, "selected": selected})
return Response(results)


class FilteredPagesAPIViewSet(PagesAPIViewSet):
def get_filtered_queryset(self) -> QuerySet:
# allow additional query parameters from PostFilterset + use_post_filter flag
Expand Down
4 changes: 3 additions & 1 deletion docs/releases/0.2.21.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
0.2.21 (2023-09-04)
-------------------

Smaller fixes and improvements.
Make it possible to select a new theme on the website and store
the selection result in the session.

- Added a snippet viewset to be able to do CRUD on tags via wagtail admin #100
- Let users select a theme for the site and store the choice in the session #105
- Added a new api endpoint to get the list of available themes #105
24 changes: 23 additions & 1 deletion tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
from django.urls import reverse
from django.utils import timezone

from cast.api.views import AudioPodloveDetailView, FilteredPagesAPIViewSet
from cast.api.views import (
AudioPodloveDetailView,
FilteredPagesAPIViewSet,
ThemeListView,
)
from cast.models import PostCategory

from .factories import UserFactory
Expand Down Expand Up @@ -268,3 +272,21 @@ def test_get_comments_via_post_detail(api_client, post, comment):

comments = r.json()["comments"]
assert comments[0]["comment"] == comment.comment


def test_theme_list_queryset_is_none():
view = ThemeListView()
assert view.get_queryset() is None


@pytest.mark.django_db
def test_list_themes(api_client):
# Given an api url to fetch the list of themes
url = reverse("cast:api:theme-list")
# When we request the list of themes
r = api_client.get(url, format="json")
assert r.status_code == 200

# Then we expect a list of themes to be returned and include the `plain` theme
result = r.json()
assert "plain" in {theme["slug"] for theme in result}
2 changes: 1 addition & 1 deletion tests/theme_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_post_select_theme_view_invalid(client):
# and the form was invalid and has an error for the invalid field
assert response.status_code == 200
assert "template_base_dir" not in client.session
assert "template_base_dir" in response.context["form"].errors
assert "template_base_dir" in response.context["theme_form"].errors


@pytest.mark.django_db
Expand Down

0 comments on commit 1f32884

Please sign in to comment.