From c649dd1d74a83a1ab1df70c85a294973a71eb658 Mon Sep 17 00:00:00 2001 From: niccofyren Date: Thu, 24 Oct 2024 21:50:53 +0200 Subject: [PATCH] feat: require debug mode for browsable api (#36) --- aktuelt/api.py | 18 ++++++++++++++++++ praktisk/api.py | 18 ++++++++++++++++++ praktisk/models.py | 1 - tgno/api.py | 22 +++++++++++++++++++++- 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/aktuelt/api.py b/aktuelt/api.py index 3ea3b48..3381efa 100644 --- a/aktuelt/api.py +++ b/aktuelt/api.py @@ -1,3 +1,5 @@ +from django.conf import settings +from rest_framework.renderers import BrowsableAPIRenderer, JSONRenderer from wagtail.api.v2.router import WagtailAPIRouter from wagtail.api.v2.views import PagesAPIViewSet @@ -11,5 +13,21 @@ class NewsPagesAPIViewSet(PagesAPIViewSet): meta_fields = PagesAPIViewSet.meta_fields + ["last_published_at"] + # To disable rest_framework's default browsable renderer + # - https://github.com/wagtail/wagtail/issues/6066 + # (outlines how they secretly ignore/override default REST_FRAMEWORK config options) + # - https://docs.wagtail.org/en/stable/advanced_topics/api/v2/configuration.html#enable-the-app + # (says rest_framework is optional, it isn't) + renderer_classes = [ + JSONRenderer, + ] + +if settings.DEBUG: + NewsPagesAPIViewSet.renderer_classes = [ + JSONRenderer, + BrowsableAPIRenderer, + ] + +print(NewsPagesAPIViewSet.renderer_classes) api_router.register_endpoint("news", NewsPagesAPIViewSet) diff --git a/praktisk/api.py b/praktisk/api.py index edf001a..7741bc9 100644 --- a/praktisk/api.py +++ b/praktisk/api.py @@ -1,3 +1,5 @@ +from django.conf import settings +from rest_framework.renderers import BrowsableAPIRenderer, JSONRenderer from wagtail.api.v2.router import WagtailAPIRouter from wagtail.api.v2.views import PagesAPIViewSet @@ -14,9 +16,25 @@ class InfoPagesAPIViewSet(PagesAPIViewSet): meta_fields = PagesAPIViewSet.meta_fields + ["last_published_at"] + # To disable rest_framework's default browsable renderer + # - https://github.com/wagtail/wagtail/issues/6066 + # (outlines how they secretly ignore/override default REST_FRAMEWORK config options) + # - https://docs.wagtail.org/en/stable/advanced_topics/api/v2/configuration.html#enable-the-app + # (says rest_framework is optional, it isn't) + renderer_classes = [ + JSONRenderer, + ] + def get_queryset(self): allowed_models = [InfoIndexPage, InfoPage] return super().get_queryset().type(tuple(allowed_models)) +if settings.DEBUG: + InfoPagesAPIViewSet.renderer_classes = [ + JSONRenderer, + BrowsableAPIRenderer, + ] + + api_router.register_endpoint("info", InfoPagesAPIViewSet) diff --git a/praktisk/models.py b/praktisk/models.py index 06987d5..0af7784 100644 --- a/praktisk/models.py +++ b/praktisk/models.py @@ -1,5 +1,4 @@ from django.db import models -from rest_framework.serializers import ModelSerializer from wagtail.api.v2.serializers import ( BaseSerializer, ChildRelationField, diff --git a/tgno/api.py b/tgno/api.py index 88a5679..17c5cf3 100644 --- a/tgno/api.py +++ b/tgno/api.py @@ -1,8 +1,28 @@ +from django.conf import settings +from rest_framework.renderers import BrowsableAPIRenderer, JSONRenderer from wagtail.api.v2.router import WagtailAPIRouter from wagtail.images.api.v2.views import ImagesAPIViewSet # "Base" routes # Content/app specific routes existing in each individual app, not here + +class AppImagesAPIViewSet(ImagesAPIViewSet): + # To disable rest_framework's default browsable renderer + # - https://github.com/wagtail/wagtail/issues/6066 + # (outlines how they secretly ignore/override default REST_FRAMEWORK config options) + # - https://docs.wagtail.org/en/stable/advanced_topics/api/v2/configuration.html#enable-the-app + # (says rest_framework is optional, it isn't) + renderer_classes = [ + JSONRenderer, + ] + + +if settings.DEBUG: + AppImagesAPIViewSet.renderer_classes = [ + JSONRenderer, + BrowsableAPIRenderer, + ] + api_router = WagtailAPIRouter("wagtailapi") -api_router.register_endpoint("images", ImagesAPIViewSet) +api_router.register_endpoint("images", AppImagesAPIViewSet)