feat: Add Spectacutlar API schema generation

This commit is contained in:
moanos [he/him] 2025-01-05 15:56:12 +01:00
parent ad6e2f4e17
commit 002dded0d5
3 changed files with 22 additions and 3 deletions

View File

@ -39,7 +39,8 @@ dependencies = [
"django-crispy-forms", "django-crispy-forms",
"crispy-bootstrap4", "crispy-bootstrap4",
"djangorestframework", "djangorestframework",
"celery[redis]" "celery[redis]",
"drf-spectacular[sidecar]"
] ]
dynamic = ["version", "readme"] dynamic = ["version", "readme"]

View File

@ -5,6 +5,7 @@ from .forms import CustomRegistrationForm
from .feeds import LatestAdoptionNoticesFeed from .feeds import LatestAdoptionNoticesFeed
from . import views from . import views
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
urlpatterns = [ urlpatterns = [
path("", views.index, name="index"), path("", views.index, name="index"),
@ -82,6 +83,10 @@ urlpatterns = [
## API ## ## API ##
######### #########
path('api/', include('fellchensammlung.api.urls')), 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 ## ## External Site ##

View File

@ -169,7 +169,9 @@ INSTALLED_APPS = [
'crispy_forms', 'crispy_forms',
"crispy_bootstrap4", "crispy_bootstrap4",
"rest_framework", "rest_framework",
'rest_framework.authtoken' 'rest_framework.authtoken',
'drf_spectacular',
'drf_spectacular_sidecar', # required for Django collectstatic discovery
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@ -283,5 +285,16 @@ REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [ 'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication', 'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication', '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,
} }