From 638face1674daf2bf2acb80f28255e9b8b854e6f Mon Sep 17 00:00:00 2001 From: Zakki Date: Thu, 7 Sep 2023 09:47:44 +0700 Subject: [PATCH] Add custom Swagger UI intro --- Makefile | 7 +++++++ django_project/core/admin.py | 5 +++++ django_project/core/models/preferences.py | 7 +++++++ django_project/core/settings/base.py | 8 +------- django_project/core/settings/project.py | 7 ------- django_project/core/templates/drf-yasg/swagger-ui.html | 2 ++ django_project/core/urls.py | 2 ++ 7 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 125d9af9..ba0e786c 100644 --- a/Makefile +++ b/Makefile @@ -166,6 +166,13 @@ devweb-shell: @echo "------------------------------------------------------------------" @docker-compose exec dev /bin/bash +run-local: + @echo + @echo "------------------------------------------------------------------" + @echo "Running Django in development mode" + @echo "------------------------------------------------------------------" + @docker-compose exec dev python manage.py runserver 0.0.0.0:8080 --settings=core.settings.dev + db-bash: @echo @echo "------------------------------------------------------------------" diff --git a/django_project/core/admin.py b/django_project/core/admin.py index dfe42daf..373f7069 100644 --- a/django_project/core/admin.py +++ b/django_project/core/admin.py @@ -88,6 +88,11 @@ class SitePreferencesAdmin(admin.ModelAdmin): 'base_url_help_page', ) }), + ('Swagger UI', { + 'fields': ( + 'swagger_ui_info', + ) + }) ) inlines = (SitePreferencesImageInline,) diff --git a/django_project/core/models/preferences.py b/django_project/core/models/preferences.py index 4e045f89..69c4a67e 100644 --- a/django_project/core/models/preferences.py +++ b/django_project/core/models/preferences.py @@ -253,6 +253,13 @@ class SitePreferences(SingletonModel): ) ) + swagger_ui_info = models.TextField( + default='', + help_text=_( + 'Shown as the introduction at the top of Swagger UI. HTML and Markdown supported.' + ) + ) + class Meta: # noqa: D106 verbose_name_plural = "site preferences" diff --git a/django_project/core/settings/base.py b/django_project/core/settings/base.py index 0aeaf853..3e6bdf21 100644 --- a/django_project/core/settings/base.py +++ b/django_project/core/settings/base.py @@ -162,11 +162,5 @@ SWAGGER_SETTINGS = { 'TAGS_SORTER': 'alpha', 'showCommonExtensions': 'true', - 'SECURITY_DEFINITIONS': { - 'ApiKey Auth': { - 'type': 'apiKey', - 'in': 'header', - 'name': 'Authorization' - } - }, + 'SECURITY_DEFINITIONS': {}, } diff --git a/django_project/core/settings/project.py b/django_project/core/settings/project.py index 98df2629..b239ce5e 100644 --- a/django_project/core/settings/project.py +++ b/django_project/core/settings/project.py @@ -147,13 +147,6 @@ # override logout url in swagger ui SWAGGER_SETTINGS['LOGIN_URL'] = '/azure-auth/login' SWAGGER_SETTINGS['LOGOUT_URL'] = '/azure-auth/logout' - SWAGGER_SETTINGS['SECURITY_DEFINITIONS'].update({ - 'B2C JWT Token Auth': { - 'type': 'apiKey', - 'in': 'header', - 'name': 'Authorization' - } - }) # Azure blob storage AZURE_STORAGE = os.environ.get('AZURE_STORAGE') AZURE_STORAGE_CONTAINER = os.environ.get('AZURE_STORAGE_CONTAINER') diff --git a/django_project/core/templates/drf-yasg/swagger-ui.html b/django_project/core/templates/drf-yasg/swagger-ui.html index d0fc2408..0bb16b0e 100644 --- a/django_project/core/templates/drf-yasg/swagger-ui.html +++ b/django_project/core/templates/drf-yasg/swagger-ui.html @@ -104,6 +104,8 @@ buttonTextEl.innerText = "Home"; buttonTextEl.style = "background-color: #49cc90;color: white; text-decoration: none; margin-bottom: 20px;position: absolute;margin-top: -40px;" document.querySelector('.information-container').appendChild(buttonTextEl); + + document.querySelector('hgroup.main').querySelector('a.link').innerHTML = '' } }, 100); diff --git a/django_project/core/urls.py b/django_project/core/urls.py index 1bf0eec4..3c59d244 100644 --- a/django_project/core/urls.py +++ b/django_project/core/urls.py @@ -12,6 +12,7 @@ from django.conf.urls.static import static from django.contrib import admin from django.http import HttpResponseNotFound +from core.models.preferences import SitePreferences import json @@ -41,6 +42,7 @@ def get_context_data(self, **kwargs): openapi.Info( title="GeoRepo API", default_version='v1.0.0', + description=SitePreferences.preferences().swagger_ui_info, ), public=True, permission_classes=[permissions.AllowAny],