Skip to content

Commit

Permalink
feat: require debug mode for browsable api (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
niccofyren authored Oct 24, 2024
1 parent 30cceb2 commit c649dd1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
18 changes: 18 additions & 0 deletions aktuelt/api.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
18 changes: 18 additions & 0 deletions praktisk/api.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
1 change: 0 additions & 1 deletion praktisk/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.db import models
from rest_framework.serializers import ModelSerializer
from wagtail.api.v2.serializers import (
BaseSerializer,
ChildRelationField,
Expand Down
22 changes: 21 additions & 1 deletion tgno/api.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit c649dd1

Please sign in to comment.