Skip to content

Commit 74a1a1a

Browse files
author
Emidio Valereto Neto
committed
feat: add interactive API docs (Swagger/ReDoc) via drf-spectacular
Exclude the unfinished Google login endpoint from the schema until social login is complete.
1 parent 41551bc commit 74a1a1a

6 files changed

Lines changed: 47 additions & 11 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ jobs:
7373
DJANGO_SETTINGS_MODULE: core.settings.docker
7474

7575
- name: Run tests
76-
run: python3 -m pytest tests/ -v -W error::DeprecationWarning
76+
run: python3 -m pytest tests/ --ds=core.settings.docker -v -W error::DeprecationWarning
7777
env:
7878
DJANGO_SETTINGS_MODULE: core.settings.docker

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ cython_debug/
195195
.abstra/
196196

197197
# Visual Studio Code
198-
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
198+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
199199
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
200-
# and can be added to the global gitignore or merged into this file. However, if you prefer,
200+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
201201
# you could uncomment the following to ignore the entire vscode folder
202202
# .vscode/
203203
# Temporary file for partial code execution
@@ -218,4 +218,4 @@ __marimo__/
218218
.streamlit/secrets.toml
219219

220220
# Static
221-
/media
221+
/media

accounts/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter
88
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
99

10+
from drf_spectacular.utils import extend_schema
1011
from .serializers import RegisterSerializer, UserSerializer
1112

1213

@@ -30,6 +31,7 @@ def put(self, request):
3031
return Response(serializer.data)
3132

3233

34+
@extend_schema(exclude=True)
3335
class GoogleLoginView(SocialLoginView):
3436
adapter_class = GoogleOAuth2Adapter
3537
callback_url = "http://localhost:3000/auth/google/callback"

core/settings/base.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@
2525
"dj_rest_auth",
2626
"dj_rest_auth.registration",
2727

28+
# drf-spectacular
29+
"drf_spectacular",
30+
2831
# Allauth
2932
"allauth",
33+
"allauth.account",
3034
"allauth.socialaccount",
3135
"allauth.socialaccount.providers.google",
3236
"allauth.socialaccount.providers.github",
@@ -51,6 +55,28 @@
5155
"django.middleware.clickjacking.XFrameOptionsMiddleware",
5256
]
5357

58+
REST_FRAMEWORK = {
59+
"DEFAULT_AUTHENTICATION_CLASSES": ("rest_framework_simplejwt.authentication.JWTAuthentication",),
60+
"DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",),
61+
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
62+
"PAGE_SIZE": 20,
63+
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
64+
}
65+
66+
SPECTACULAR_SETTINGS = {
67+
"TITLE": "Simlog API",
68+
"DESCRIPTION": "Django REST API for a flight simulator companion app.",
69+
"VERSION": "1.0",
70+
"SERVE_INCLUDE_SCHEMA": False,
71+
72+
"AUTHENTICATION_CLASSES": (
73+
"rest_framework_simplejwt.authentication.JWTAuthentication",
74+
),
75+
"SWAGGER_UI_SETTINGS": {
76+
"persistAuthorization": True,
77+
},
78+
}
79+
5480
ROOT_URLCONF = "core.urls"
5581

5682
SITE_ID = 1
@@ -113,12 +139,5 @@
113139

114140
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
115141

116-
REST_FRAMEWORK = {
117-
"DEFAULT_AUTHENTICATION_CLASSES": ("rest_framework_simplejwt.authentication.JWTAuthentication",),
118-
"DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",),
119-
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
120-
"PAGE_SIZE": 20,
121-
}
122-
123142
MEDIA_URL = "/media/"
124143
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

core/urls.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
from django.contrib import admin
22
from django.urls import path, include
33
from django.conf import settings
4+
45
from django.conf.urls.static import static
6+
from django.views.generic import RedirectView
7+
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
58

69
urlpatterns = [
710
path("admin/", admin.site.urls),
811
path("api/auth/", include("accounts.urls")),
912
path("api/flights/", include("logbook.urls")),
1013
path("api/simbrief/", include("simbrief.urls")),
1114
path("api/", include("checklist.urls")),
15+
path("api/schema/", SpectacularAPIView.as_view(), name="schema"),
16+
path("api/docs/", SpectacularSwaggerView.as_view(
17+
url_name="schema",
18+
),
19+
name="swagger-ui"),
20+
path("", RedirectView.as_view(pattern_name="swagger-ui")),
21+
1222
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

requirements/base.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ requests>=2.31
77
whitenoise>=6.7
88
django-cors-headers>=4.3
99
pillow>=12.0,<13.0
10+
django-allauth>=65.18,<66.0
11+
dj-rest-auth>=7.2,<8.0
12+
oauthlib>=3.3,<4.0
13+
cryptography>=49.0,<50.0
14+
drf-spectacular>=0.29,<0.30

0 commit comments

Comments
 (0)