Skip to content

Commit 0bf9434

Browse files
committed
The changes made in this commit include:
- Added `rest_framework` and `drf_spectacular` to the `EXTERNAL_APPS` list in `settings.py`. - Added `PAGE_SIZE` variable to the settings file, which retrieves its value from the `PAGE_SIZE` environment variable or defaults to 10. - Added Django REST framework settings to the `REST_FRAMEWORK` dictionary in `settings.py`, including pagination class, renderer classes, parser classes, authentication classes, schema class, and filter backends. - Added DRF Spectacular settings to the `SPECTACULAR_SETTINGS` dictionary in `settings.py`, including title, description, version, component split settings, serve public setting, schema path prefix, Swagger UI settings, and serve include schema setting. - Imported necessary views from `drf_spectacular.views` in `urls.py`. - Added URL patterns for DRF Spectacular views in `urls.py`, including the schema view, Swagger UI view, and ReDoc view.
1 parent 9abbb2a commit 0bf9434

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/signal_documentation/settings.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
https://docs.djangoproject.com/en/4.2/ref/settings/
1111
"""
1212
import os
13-
import sentry_sdk
1413
from pathlib import Path
1514
from typing import Any
1615

16+
import sentry_sdk
1717
# Sentry init and config:
1818
# - If you want to use Sentry, specify the DSN via the env var of `SENTRY_DSN`.
1919
# - Useful defaults for a development environment are set below. They can be
@@ -81,6 +81,8 @@
8181
'debug_toolbar',
8282
'django_extensions',
8383
'models_extensions',
84+
'rest_framework',
85+
'drf_spectacular',
8486
'django_filters',
8587
'health_check',
8688
'health_check.db',
@@ -150,6 +152,54 @@
150152
}
151153
}
152154

155+
156+
PAGE_SIZE = os.environ.get('PAGE_SIZE', 10)
157+
158+
159+
# Django REST framework
160+
# https://www.django-rest-framework.org/
161+
REST_FRAMEWORK = {
162+
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
163+
"PAGE_SIZE": os.environ.get('PAGE_SIZE', PAGE_SIZE),
164+
'DEFAULT_RENDERER_CLASSES': [
165+
'rest_framework.renderers.JSONRenderer',
166+
'rest_framework.renderers.BrowsableAPIRenderer',
167+
],
168+
'DEFAULT_PARSER_CLASSES': [
169+
'rest_framework.parsers.JSONParser',
170+
'rest_framework.parsers.MultiPartParser',
171+
],
172+
'DEFAULT_AUTHENTICATION_CLASSES': [
173+
'rest_framework.authentication.BasicAuthentication',
174+
'rest_framework.authentication.SessionAuthentication',
175+
],
176+
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
177+
'DEFAULT_FILTER_BACKENDS': (
178+
'django_filters.rest_framework.DjangoFilterBackend',
179+
),
180+
181+
}
182+
183+
184+
# DRF Spectacular settings
185+
# https://drf-spectacular.readthedocs.io/en/latest/settings.html
186+
SPECTACULAR_SETTINGS = {
187+
'TITLE': 'Signal Documentation',
188+
'DESCRIPTION': 'Signal Documentation API',
189+
'VERSION': '1.0.0',
190+
"COMPONENT_SPLIT_PATCH": True,
191+
"COMPONENT_SPLIT_REQUEST": True,
192+
'SERVE_PUBLIC': True,
193+
'SCHEMA_PATH_PREFIX': '/api/v[0-9]',
194+
'SWAGGER_UI_SETTINGS': {
195+
'docExpansion': 'list',
196+
'filter': True,
197+
'tagsSorter': 'alpha',
198+
},
199+
'SERVE_INCLUDE_SCHEMA': False,
200+
}
201+
202+
153203
# Django chache
154204
# https://docs.djangoproject.com/en/4.2/topics/cache/#redis
155205

src/signal_documentation/urls.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
include,
2323
path,
2424
)
25+
from drf_spectacular.views import (
26+
SpectacularAPIView,
27+
SpectacularRedocView,
28+
SpectacularSwaggerView,
29+
)
2530

2631
from base.views import (
2732
BadRequestErrorView,
@@ -39,6 +44,11 @@
3944
path(f'{settings.MAIN_PAGE}/admin/' if settings.MAIN_PAGE else 'admin/', admin.site.urls),
4045
path('__debug__/', include('debug_toolbar.urls')),
4146
path(f'{settings.MAIN_PAGE}/' if settings.MAIN_PAGE else '', include('signals.urls')),
47+
48+
# drf-spectacular
49+
path("api/docs/schema/", SpectacularAPIView.as_view(), name="spectacular-schema"),
50+
path("api/docs/swagger/", SpectacularSwaggerView.as_view(url_name="spectacular-schema"), name="swagger"),
51+
path("api/docs/redoc/", SpectacularRedocView.as_view(url_name="spectacular-schema"), name="redoc"),
4252
]
4353

4454
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # type: ignore

0 commit comments

Comments
 (0)