From e490302b5b7f47588a83deaa26c623013b88aaa0 Mon Sep 17 00:00:00 2001 From: "ying.yunlong" Date: Wed, 24 Jan 2018 14:32:24 +0800 Subject: Add vfc-vnfres swagger settings. Change-Id: I2386bea86ba81a3174d5531bda80a0a32bae188c Issue-ID: VFC-679 Signed-off-by: ying.yunlong --- res/res/settings.py | 27 ++++++++++++++++++++++++++- res/res/urls.py | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/res/res/settings.py b/res/res/settings.py index 6b19ab5..a6cca4b 100644 --- a/res/res/settings.py +++ b/res/res/settings.py @@ -45,7 +45,8 @@ INSTALLED_APPS = [ 'rest_framework', 'res.pub.database', 'res.samples', - 'res.resources' + 'res.resources', + 'drf_yasg', ] MIDDLEWARE_CLASSES = [ @@ -76,6 +77,30 @@ REST_FRAMEWORK = { ) } +# drf-yasg +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +SWAGGER_SETTINGS = { + 'LOGIN_URL': '/admin/login', + 'LOGOUT_URL': '/admin/logout', + + 'DEFAULT_INFO': 'res.urls.swagger_info' +} + DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', diff --git a/res/res/urls.py b/res/res/urls.py index 9db7895..b9c08ca 100644 --- a/res/res/urls.py +++ b/res/res/urls.py @@ -13,11 +13,37 @@ # limitations under the License. from django.conf.urls import include, url +from drf_yasg import openapi +from drf_yasg.views import get_schema_view +from rest_framework import permissions + from res.pub.config.config import REG_TO_MSB_WHEN_START, REG_TO_MSB_REG_URL, REG_TO_MSB_REG_PARAM +swagger_info = openapi.Info( + title="vnfres API", + default_version='v1', + description=""" + +The `swagger-ui` view can be found [here](/cached/swagger). +The `ReDoc` view can be found [here](/cached/redoc). +The swagger YAML document can be found [here](/cached/swagger.yaml).""" +) + +SchemaView = get_schema_view( + validators=['ssv', 'flex'], + public=True, + permission_classes=(permissions.AllowAny,), +) + urlpatterns = [ url(r'^', include('res.samples.urls')), url(r'^', include('res.resources.urls')), + url(r'^swagger(?P.json|.yaml)$', SchemaView.without_ui(cache_timeout=0), name='schema-json'), + url(r'^swagger/$', SchemaView.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), + url(r'^redoc/$', SchemaView.with_ui('redoc', cache_timeout=0), name='schema-redoc'), + url(r'^cached/swagger(?P.json|.yaml)$', SchemaView.without_ui(cache_timeout=None), name='cschema-json'), + url(r'^cached/swagger/$', SchemaView.with_ui('swagger', cache_timeout=None), name='cschema-swagger-ui'), + url(r'^cached/redoc/$', SchemaView.with_ui('redoc', cache_timeout=None), name='cschema-redoc'), ] # regist to MSB when startup -- cgit 1.2.3-korg