diff --git a/requirements.txt b/requirements.txt index 04d25f9..f42fbef 100644 --- a/requirements.txt +++ b/requirements.txt @@ -151,3 +151,4 @@ Werkzeug==0.14.1 WTForms==2.2.1 zope.deprecation==4.4.0 zope.interface==4.6.0 +django-rest-swagger \ No newline at end of file diff --git a/seal/settings.py b/seal/settings.py index 11d95be..b7aebd0 100644 --- a/seal/settings.py +++ b/seal/settings.py @@ -50,6 +50,7 @@ INSTALLED_APPS = [ 'django_filters', 'graphene_django', 'channels', + 'rest_framework_swagger', ] GRAPHENE = { @@ -267,3 +268,34 @@ CHANNEL_LAYERS = { # 配置ASGI ASGI_APPLICATION = "seal.routing.application" + +SWAGGER_SETTINGS = { + # 基础样式 + # 'SECURITY_DEFINITIONS': { + # "basic": { + # 'type': 'basic' + # } + # }, + 'USE_SESSION_AUTH': True, + 'SECURITY_DEFINITIONS': { + 'api_key': { + 'type': 'apiKey', + 'in': 'header', + 'name': 'authorization' + } + }, + # 如果需要登录才能够查看接口文档, 登录的链接使用restframework自带的. + # 'LOGIN_URL': '/api/v1/login/', + # 'LOGOUT_URL': 'rest_framework:logout', + # 'DOC_EXPANSION': None, + # 'SHOW_REQUEST_HEADERS':True, + # 'USE_SESSION_AUTH': True, + # 'DOC_EXPANSION': 'list', + # 接口文档中方法列表以首字母升序排列 + 'APIS_SORTER': 'alpha', + # 如果支持json提交, 则接口文档中包含json输入框 + 'JSON_EDITOR': True, + # 方法列表字母排序 + 'OPERATIONS_SORTER': 'alpha', + 'VALIDATOR_URL': None, +} diff --git a/seal/urls.py b/seal/urls.py index a3541c2..ce928ef 100644 --- a/seal/urls.py +++ b/seal/urls.py @@ -6,6 +6,9 @@ from rest_framework.authtoken import views from rest_framework.documentation import include_docs_urls from graphene_django.views import GraphQLView from seal.schema import schema +from rest_framework_swagger.views import get_swagger_view + +schema_view = get_swagger_view(title='海豹 API 文档') API_TITLE = '海豹 API 文档' API_DESCRIPTION = '海豹 API 文档' @@ -20,6 +23,7 @@ urlpatterns = [ path('api/token', views.obtain_auth_token), path('api/docs/', include_docs_urls(title=API_TITLE, description=API_DESCRIPTION, authentication_classes=[], permission_classes=[])), + path('api/docs2/', schema_view, name="docs"), path('graphql/', GraphQLView.as_view(graphiql=True, schema=schema)), path('sql/', include('sql.urls', namespace='sql')),