From 002dded0d5a7024eb45bcf9cc2002cfbb1f317f4 Mon Sep 17 00:00:00 2001 From: moanos Date: Sun, 5 Jan 2025 15:56:12 +0100 Subject: [PATCH] feat: Add Spectacutlar API schema generation --- pyproject.toml | 3 ++- src/fellchensammlung/urls.py | 5 +++++ src/notfellchen/settings.py | 17 +++++++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 36b59b6..e2fc4d8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,7 +39,8 @@ dependencies = [ "django-crispy-forms", "crispy-bootstrap4", "djangorestframework", - "celery[redis]" + "celery[redis]", + "drf-spectacular[sidecar]" ] dynamic = ["version", "readme"] diff --git a/src/fellchensammlung/urls.py b/src/fellchensammlung/urls.py index 6e41a03..b47ecc2 100644 --- a/src/fellchensammlung/urls.py +++ b/src/fellchensammlung/urls.py @@ -5,6 +5,7 @@ from .forms import CustomRegistrationForm from .feeds import LatestAdoptionNoticesFeed from . import views +from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView urlpatterns = [ path("", views.index, name="index"), @@ -82,6 +83,10 @@ urlpatterns = [ ## API ## ######### path('api/', include('fellchensammlung.api.urls')), + path('api/schema/', SpectacularAPIView.as_view(), name='schema'), + # Optional UI: + path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'), + path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'), ################### ## External Site ## diff --git a/src/notfellchen/settings.py b/src/notfellchen/settings.py index bc0ce77..5551c96 100644 --- a/src/notfellchen/settings.py +++ b/src/notfellchen/settings.py @@ -169,7 +169,9 @@ INSTALLED_APPS = [ 'crispy_forms', "crispy_bootstrap4", "rest_framework", - 'rest_framework.authtoken' + 'rest_framework.authtoken', + 'drf_spectacular', + 'drf_spectacular_sidecar', # required for Django collectstatic discovery ] MIDDLEWARE = [ @@ -283,5 +285,16 @@ REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ 'rest_framework.authentication.TokenAuthentication', 'rest_framework.authentication.SessionAuthentication', - ] + ], + 'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema', +} + +SPECTACULAR_SETTINGS = { + 'SWAGGER_UI_DIST': 'SIDECAR', # shorthand to use the sidecar instead + 'SWAGGER_UI_FAVICON_HREF': 'SIDECAR', + 'REDOC_DIST': 'SIDECAR', + 'TITLE': 'Notfellchen API', + 'DESCRIPTION': 'Adopt a animal in need', + 'VERSION': '1.0.0', + 'SERVE_INCLUDE_SCHEMA': False, }