Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic endpoint for blog posts #1728

Merged
merged 14 commits into from
Oct 15, 2024
87 changes: 87 additions & 0 deletions etna/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
from wagtail.models import Page, PageViewRestriction, Site

from rest_framework import status
from rest_framework.filters import BaseFilterBackend
from rest_framework.response import Response
from wagtail_headless_preview.models import PagePreview
from wagtailmedia.api.views import MediaAPIViewSet

from etna.blog.models import BlogPostPage
from etna.core.serializers.pages import DefaultPageSerializer

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -248,9 +250,94 @@ class CustomImagesAPIViewSet(ImagesAPIViewSet):
]


class YearFilter(BaseFilterBackend):
"""
Implements the ?year filter used to filter the results to only contain
blog posts that were published in the specified year.
"""

def filter_queryset(self, request, queryset, view):
if "year" in request.GET:
try:
year = int(request.GET["year"])
if year < 0:
raise ValueError()
except ValueError:
raise BadRequestError("year must be a positive integer")

queryset = queryset.filter(**{"published_date__year": year})

return queryset


class MonthFilter(BaseFilterBackend):
"""
Implements the ?month filter used to filter the results to only contain
blog posts that were published in the specified month.
"""

def filter_queryset(self, request, queryset, view):
if "month" in request.GET:
if "year" not in request.GET:
raise BadRequestError("cannot use month filter without a year filter")
try:
month = int(request.GET["month"])
if month < 0 or month > 12:
raise ValueError()
except ValueError:
raise BadRequestError("month must be a positive integer between 1-12")

queryset = queryset.filter(**{"published_date__month": month})

return queryset


class DayFilter(BaseFilterBackend):
"""
Implements the ?day filter used to filter the results to only contain
blog posts that were published in the specified day.
"""

def filter_queryset(self, request, queryset, view):
if "day" in request.GET:
if "year" not in request.GET or "month" not in request.GET:
raise BadRequestError(
"cannot use day filter without a year and month filter"
)
try:
day = int(request.GET["day"])
if day < 0 or day > 31:
raise ValueError()
except ValueError:
raise BadRequestError("day must be a positive integer between 1-31")

queryset = queryset.filter(**{"published_date__day": day})

return queryset


class BlogAPIViewSet(CustomPagesAPIViewSet):
filter_backends = [
YearFilter,
MonthFilter,
DayFilter,
# TODO: Add filter by author
jamesbiggs marked this conversation as resolved.
Show resolved Hide resolved
] + CustomPagesAPIViewSet.filter_backends
known_query_parameters = CustomPagesAPIViewSet.known_query_parameters.union(
[
"year",
"month",
"day",
"author",
]
)
model = BlogPostPage


api_router = WagtailAPIRouter("wagtailapi")

api_router.register_endpoint("pages", CustomPagesAPIViewSet)
api_router.register_endpoint("page_preview", PagePreviewAPIViewSet)
api_router.register_endpoint("images", CustomImagesAPIViewSet)
api_router.register_endpoint("media", MediaAPIViewSet)
api_router.register_endpoint("blog_posts", BlogAPIViewSet)