From 2e9cae46cb732eecc49c080322fd657680afcf23 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Wed, 24 Apr 2019 15:46:50 -0400 Subject: [PATCH] Add drf-yasg * Install drf-yasg * Add drf-yasg settings and urls * Pin drf to make drf-yasg work * Adjust config-models version to be compatible * Remove django-rest-swagger (the old way) --- cms/envs/common.py | 2 +- cms/envs/devstack.py | 4 ++++ cms/urls.py | 6 ++++-- lms/envs/common.py | 6 +++++- lms/urls.py | 6 ++++-- openedx/core/openapi.py | 22 ++++++++++++++++++++++ requirements/edx/base.in | 5 +++-- requirements/edx/github.in | 3 --- 8 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 openedx/core/openapi.py diff --git a/cms/envs/common.py b/cms/envs/common.py index 36d156b6af..fa41067a51 100644 --- a/cms/envs/common.py +++ b/cms/envs/common.py @@ -1173,7 +1173,7 @@ INSTALLED_APPS = [ 'pipeline_mako', # API Documentation - 'rest_framework_swagger', + 'drf_yasg', 'openedx.features.course_duration_limits', 'openedx.features.content_type_gating', diff --git a/cms/envs/devstack.py b/cms/envs/devstack.py index edc70d1c75..1222a2a21a 100644 --- a/cms/envs/devstack.py +++ b/cms/envs/devstack.py @@ -108,6 +108,10 @@ def should_show_debug_toolbar(request): DEBUG_TOOLBAR_MONGO_STACKTRACES = False +########################### API DOCS ################################# + +FEATURES['ENABLE_API_DOCS'] = True + ################################ MILESTONES ################################ FEATURES['MILESTONES_APP'] = True diff --git a/cms/urls.py b/cms/urls.py index 04edae7027..251aea690d 100644 --- a/cms/urls.py +++ b/cms/urls.py @@ -3,7 +3,7 @@ from django.conf.urls import include, url from django.conf.urls.static import static from django.contrib.admin import autodiscover as django_autodiscover from django.utils.translation import ugettext_lazy as _ -from rest_framework_swagger.views import get_swagger_view +from openedx.core.openapi import schema_view import contentstore.views from cms.djangoapps.contentstore.views.organization import OrganizationListView @@ -266,7 +266,9 @@ urlpatterns += [ if settings.FEATURES.get('ENABLE_API_DOCS'): urlpatterns += [ - url(r'^api-docs/$', get_swagger_view(title='Studio API')), + url(r'^swagger(?P\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'), + url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), + url(r'^api-docs/$', schema_view.with_ui('swagger', cache_timeout=0)), ] from openedx.core.djangoapps.plugins import constants as plugin_constants, plugin_urls diff --git a/lms/envs/common.py b/lms/envs/common.py index d368a731d3..85d003f063 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -2150,6 +2150,7 @@ INSTALLED_APPS = [ # User API 'rest_framework', + 'openedx.core.djangoapps.user_api', # Shopping cart @@ -2301,7 +2302,7 @@ INSTALLED_APPS = [ 'django_filters', # API Documentation - 'rest_framework_swagger', + 'drf_yasg', # edx-drf-extensions 'csrf.apps.CsrfAppConfig', # Enables frontend apps to retrieve CSRF tokens. @@ -2331,6 +2332,9 @@ REST_FRAMEWORK = { }, } +SWAGGER_SETTINGS = { + 'DEFAULT_INFO': 'openedx.core.openapi.openapi_info', +} ######################### MARKETING SITE ############################### EDXMKTG_LOGGED_IN_COOKIE_NAME = 'edxloggedin' diff --git a/lms/urls.py b/lms/urls.py index 2c40254d43..dd1190ef19 100644 --- a/lms/urls.py +++ b/lms/urls.py @@ -8,7 +8,6 @@ from django.conf.urls.static import static from django.contrib.admin import autodiscover as django_autodiscover from django.utils.translation import ugettext_lazy as _ from django.views.generic.base import RedirectView -from rest_framework_swagger.views import get_swagger_view from branding import views as branding_views from config_models.views import ConfigurationModelCurrentAPIView @@ -42,6 +41,7 @@ from openedx.core.djangoapps.programs.models import ProgramsApiConfig from openedx.core.djangoapps.self_paced.models import SelfPacedConfiguration from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers from openedx.core.djangoapps.verified_track_content import views as verified_track_content_views +from openedx.core.openapi import schema_view from openedx.features.enterprise_support.api import enterprise_enabled from ratelimitbackend import admin from static_template_view import views as static_template_view_views @@ -964,7 +964,9 @@ if settings.BRANCH_IO_KEY: if settings.FEATURES.get('ENABLE_API_DOCS'): urlpatterns += [ - url(r'^api-docs/$', get_swagger_view(title='LMS API')), + url(r'^swagger(?P\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'), + url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), + url(r'^api-docs/$', schema_view.with_ui('swagger', cache_timeout=0)), ] # edx-drf-extensions csrf app diff --git a/openedx/core/openapi.py b/openedx/core/openapi.py new file mode 100644 index 0000000000..d463683222 --- /dev/null +++ b/openedx/core/openapi.py @@ -0,0 +1,22 @@ +""" +Open API support. +""" + +from rest_framework import permissions +from drf_yasg.views import get_schema_view +from drf_yasg import openapi + +openapi_info = openapi.Info( + title="Open edX API", + default_version="v1", + description="APIs for access to Open edX information", + #terms_of_service="https://www.google.com/policies/terms/", # TODO: Do we have these? + contact=openapi.Contact(email="oscm@edx.org"), + #license=openapi.License(name="BSD License"), # TODO: What does this mean? +) + +schema_view = get_schema_view( + openapi_info, + public=True, + permission_classes=(permissions.AllowAny,), +) diff --git a/requirements/edx/base.in b/requirements/edx/base.in index e91f65332d..283aea61ce 100644 --- a/requirements/edx/base.in +++ b/requirements/edx/base.in @@ -37,7 +37,7 @@ celery==3.1.25 # Asynchronous task execution library defusedxml Django==1.11.21 # Web application framework django-babel-underscore # underscore template extractor for django-babel (internationalization utilities) -django-config-models>=0.2.2 # Configuration models for Django allowing config management with auditing +django-config-models>=1.0.0 # Configuration models for Django allowing config management with auditing django-cors-headers==2.1.0 # Used to allow to configure CORS headers for cross-domain requests django-countries==4.6.1 # Country data for Django forms and model fields django-crum # Middleware that stores the current request and user in thread local storage @@ -54,7 +54,6 @@ django-pyfs django-ratelimit django-ratelimit-backend==1.1.1 django-require -django-rest-swagger # API documentation django-sekizai django-ses==0.8.4 django-simple-history @@ -64,7 +63,9 @@ django-storages==1.4.1 django-user-tasks django-waffle==0.12.0 django-webpack-loader # Used to wire webpack bundles into the django asset pipeline +djangorestframework==3.7.7 djangorestframework-jwt +drf-yasg # Replacement for django-rest-swagger edx-ace==0.1.10 edx-analytics-data-api-client edx-ccx-keys diff --git a/requirements/edx/github.in b/requirements/edx/github.in index d296770156..9cb17b85bb 100644 --- a/requirements/edx/github.in +++ b/requirements/edx/github.in @@ -66,9 +66,6 @@ git+https://github.com/edx/MongoDBProxy.git@25b99097615bda06bd7cdfe5669ed80dc2a7 # This can go away when we update auth to not use django-rest-framework-oauth git+https://github.com/edx/django-oauth-plus.git@01ec2a161dfc3465f9d35b9211ae790177418316#egg=django-oauth-plus==2.2.9.edx-1 -# Why a DRF fork? See: https://openedx.atlassian.net/browse/PLAT-1581 -git+https://github.com/edx/django-rest-framework.git@1ceda7c086fddffd1c440cc86856441bbf0bd9cb#egg=djangorestframework==3.6.3 - # Why a drf-oauth fork? To add Django 1.11 compatibility to the abandoned repo. # This dependency will be removed by this work: https://openedx.atlassian.net/browse/PLAT-1660 git+https://github.com/edx/django-rest-framework-oauth.git@0a43e8525f1e3048efe4bc70c03de308a277197c#egg=djangorestframework-oauth==1.1.1