From 7d3605f5e11c0a2a1cec94a5b3e13a7fa63a6746 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 28 Jan 2024 18:06:17 +0000 Subject: [PATCH] Add API for latest posts --- website/api/pagination.py | 4 ++-- website/api/serializers.py | 11 +++++++++++ website/api/tests.py | 17 +++++++++++++++++ website/api/urls.py | 1 + website/api/views.py | 17 +++++++++++++++++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/website/api/pagination.py b/website/api/pagination.py index db74939..6031b55 100644 --- a/website/api/pagination.py +++ b/website/api/pagination.py @@ -2,5 +2,5 @@ from rest_framework.pagination import PageNumberPagination class CustomPageNumberPagination(PageNumberPagination): - page_size = 15 - max_page_size = 40 + page_size = 10 + max_page_size = 25 diff --git a/website/api/serializers.py b/website/api/serializers.py index 551ffb5..7a67670 100644 --- a/website/api/serializers.py +++ b/website/api/serializers.py @@ -36,3 +36,14 @@ class LMOTFYSerializer(serializers.ModelSerializer): return self.context["request"].build_absolute_uri(image_url) return image_url + + +class LatestPostSerializer(serializers.ModelSerializer): + full_url = serializers.SerializerMethodField() + + class Meta: + model = BlogPostPage + fields = read_only_fields = ["full_url", "title", "date"] + + def get_full_url(self, page: Page) -> str: + return page.get_full_url(request=self.context["request"]) diff --git a/website/api/tests.py b/website/api/tests.py index dc4f743..f392662 100644 --- a/website/api/tests.py +++ b/website/api/tests.py @@ -86,3 +86,20 @@ class SchemaTestCase(APISimpleTestCase): def test_schema(self) -> None: response = self.client.get(reverse("api:schema")) self.assertEqual(response.status_code, 200) + + +class LatestPostsAPIViewTestCase(APITestCase): + url = reverse("api:latest-posts") + + @classmethod + def setUpTestData(cls) -> None: + cls.home_page = HomePage.objects.get() + + for i in range(4): + BlogPostPageFactory(parent=cls.home_page, title=f"Post {i}") + + def test_accessible(self) -> None: + with self.assertNumQueries(5): + response = self.client.get(self.url) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.data["count"], 4) diff --git a/website/api/urls.py b/website/api/urls.py index 871ef02..74606bb 100644 --- a/website/api/urls.py +++ b/website/api/urls.py @@ -10,6 +10,7 @@ api_urlpatterns = [ path("ping/", views.PingAPIView.as_view(), name="ping"), path("page-links/", views.PageLinksAPIView.as_view(), name="page-links"), path("lmotfy/", views.LMOTFYAPIView.as_view(), name="lmotfy"), + path("latest-posts/", views.LatestPostsAPIView.as_view(), name="latest-posts"), ] schema_view = get_schema_view( diff --git a/website/api/views.py b/website/api/views.py index 52af7c7..96e01a8 100644 --- a/website/api/views.py +++ b/website/api/views.py @@ -60,3 +60,20 @@ class SwaggerRedirectView(RedirectView): return HttpResponseRedirect( self.SWAGGER_EDITOR_URL + request.build_absolute_uri(reverse("api:schema")) ) + + +class LatestPostsAPIView(ListAPIView): + """ + List my latest blog posts + """ + + serializer_class = serializers.LatestPostSerializer + pagination_class = CustomPageNumberPagination + + def get_queryset(self) -> PageQuerySet: + return ( + BlogPostPage.objects.live() + .public() + .only("id", "url_path", "title", "date") + .order_by("-date") + )