Skip to content

Commit 1d175d7

Browse files
committed
Using drf-spectacular for openapi 3
1 parent 3021a21 commit 1d175d7

File tree

4 files changed

+77
-97
lines changed

4 files changed

+77
-97
lines changed

main/settings.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
INSTALLED_APPS = [
141141
# External App (This app has to defined before django.contrib.admin)
142142
'modeltranslation', # https://django-modeltranslation.readthedocs.io/en/latest/installation.html#installed-apps
143-
'drf_yasg',
143+
'drf_spectacular',
144144

145145
# Django Apps
146146
'django.contrib.admin',
@@ -208,7 +208,7 @@
208208
'rest_framework.renderers.BrowsableAPIRenderer',
209209
'rest_framework_csv.renderers.PaginatedCSVRenderer',
210210
),
211-
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
211+
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
212212
}
213213

214214
GRAPHENE = {
@@ -559,7 +559,12 @@
559559
}
560560
CACHE_MIDDLEWARE_SECONDS = env('CACHE_MIDDLEWARE_SECONDS') # Planned: 600 for staging, 60 from prod
561561

562-
OPEN_API_DOCS_TIMEOUT = 60
562+
SPECTACULAR_SETTINGS = {
563+
'TITLE': 'IFRC-GO API',
564+
'DESCRIPTION': 'IFRC-GO API Documenation',
565+
'VERSION': '1.0.0',
566+
'SERVE_INCLUDE_SCHEMA': False,
567+
}
563568

564569
# Need to load this to overwrite modeltranslation module
565570
import main.translation # noqa: F401 E402

main/urls.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@
7575
from rest_framework import routers
7676
from rest_framework.documentation import include_docs_urls
7777
from rest_framework import permissions
78-
from drf_yasg.views import get_schema_view
79-
from drf_yasg import openapi
78+
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
8079
from api import drf_views as api_views
8180
from flash_update import views as flash_views
8281
from per import drf_views as per_views
@@ -171,17 +170,6 @@
171170

172171
router.register(r"review-country", api_views.CountryOfFieldReportToReviewViewset, basename="review_country")
173172

174-
api_schema_view = get_schema_view(
175-
openapi.Info(
176-
title="IFRC-GO API",
177-
default_version='v1',
178-
description="IFRC-GO API Openapi schema",
179-
contact=openapi.Contact(email="[email protected]"), # TODO: Update this
180-
),
181-
public=True,
182-
permission_classes=(permissions.AllowAny,),
183-
)
184-
185173
# Country Plan apis
186174
router.register(r"country-plan", country_plan_views.CountryPlanViewset, basename="country_plan")
187175

@@ -244,16 +232,19 @@
244232
url(r"^api/v2/exportperresults/", per_views.ExportAssessmentToCSVViewset.as_view()),
245233
url(r"^api/v2/local-unit/(?P<pk>\d+)", LocalUnitDetailAPIView.as_view()),
246234
url(r"^api/v2/local-unit/", LocalUnitListAPIView.as_view()),
247-
url(r"^docs/", include_docs_urls(title="IFRC GO API", public=False)),
248-
url(r'^api-docs(?P<format>\.json|\.yaml)$',
249-
api_schema_view.without_ui(cache_timeout=settings.OPEN_API_DOCS_TIMEOUT), name='api-schema'),
250235
url(r"^tinymce/", include("tinymce.urls")),
251236
url(r"^$", RedirectView.as_view(url="/admin")),
252237
# url(r'^', admin.site.urls),
253238
url(r"^favicon\.ico$", RedirectView.as_view(url="/static/favicon.ico")),
254239
url(r"^server-error-for-devs", DummyHttpStatusError.as_view()),
255240
url(r"^exception-error-for-devs", DummyExceptionError.as_view()),
256241
path("i18n/", include("django.conf.urls.i18n")),
242+
# Docs
243+
url(r"^docs/", include_docs_urls(title="IFRC GO API", public=False)), # TODO: Remove this?
244+
path("api-docs/", SpectacularAPIView.as_view(), name='schema'),
245+
path("api-docs/swagger-ui/", SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
246+
path("api-docs/redoc/", SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
247+
257248
]
258249

259250
if settings.DEBUG:

poetry.lock

Lines changed: 61 additions & 77 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ celery = { version = "==5.1.2", extras = ["redis"] }
8181
django-redis = "==5.0.0"
8282
sentry-sdk = "*"
8383
django-haystack = { version = "*", extras = ["elasticsearch"] }
84-
drf_yasg = "*"
84+
drf-spectacular = "*"
8585

8686
mapbox-tilesets = "*"
8787
ipython = "*"

0 commit comments

Comments
 (0)