Skip to content

Commit

Permalink
Utilise settings to allow control over authentication_classes, permis…
Browse files Browse the repository at this point in the history
…sion_classes and renderer_classes for menu generator views
  • Loading branch information
ababic committed Sep 6, 2020
1 parent 7ca2013 commit f82305b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
18 changes: 18 additions & 0 deletions wagtailmenus/api/v1/conf/defaults.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# NOTE: All supported app settings must be added here

# --------------------
# REST Framework views
# --------------------

# Use the REST framework defaults by default
AUTHENTICATION_CLASSES = None

# Use the REST framework defaults by default
PERMISSION_CLASSES = None

# Use custom renderer by default. Override this setting to hide the browsable
# API in production
RENDERER_CLASSES = [
'rest_framework.renderers.JSONRenderer',
'wagtailmenus.api.v1.renderers.BrowsableAPIWithArgumentFormRenderer',
]


# -----------------------
# Menu serializer classes
# -----------------------
Expand Down
20 changes: 18 additions & 2 deletions wagtailmenus/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from rest_framework.renderers import BrowsableAPIRenderer, JSONRenderer
from rest_framework.reverse import reverse
from rest_framework.response import Response
from rest_framework.settings import perform_import

from wagtailmenus.conf import settings as wagtailmenus_settings
from wagtailmenus.utils.misc import derive_ancestor_ids
from wagtailmenus.api.utils import make_serializer_class
from wagtailmenus.api.v1.conf import settings as api_settings
from wagtailmenus.api.v1.renderers import BrowsableAPIWithArgumentFormRenderer
from wagtailmenus.api.v1.serializers import BaseModelMenuSerializer

from . import forms
Expand Down Expand Up @@ -54,7 +54,23 @@ class BaseMenuGeneratorView(APIView):
serializer_class = None
serializer_class_setting_name = None

renderer_classes = (JSONRenderer, BrowsableAPIWithArgumentFormRenderer)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if api_settings.AUTHENTICATION_CLASSES is not None:
self.authentication_classes = perform_import(
api_settings.AUTHENTICATION_CLASSES,
'WAGTAILMENUS_API_V1_AUTHENTICATION_CLASSES'
)
if api_settings.PERMISSION_CLASSES is not None:
self.permission_classes = perform_import(
api_settings.PERMISSION_CLASSES,
'WAGTAILMENUS_API_V1_PERMISSION_CLASSES'
)
if api_settings.RENDERER_CLASSES is not None:
self.renderer_classes = perform_import(
api_settings.RENDERER_CLASSES,
'WAGTAILMENUS_API_V1_RENDERER_CLASSES'
)

@classmethod
def get_menu_class(cls):
Expand Down

0 comments on commit f82305b

Please sign in to comment.