Skip to content

Commit

Permalink
Merge pull request #42 from ola-t/swagger-3
Browse files Browse the repository at this point in the history
Upgrade to Swagger 3
  • Loading branch information
atarkowska authored Nov 13, 2017
2 parents 65b3717 + cde8e40 commit 2884f44
Show file tree
Hide file tree
Showing 33 changed files with 422 additions and 179 deletions.
2 changes: 1 addition & 1 deletion emgapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def mydata(self, request):
_username = request.user.username
return self.distinct() \
.filter(Q(submission_account_id=_username))
return self.available(request)
return ()

def recent(self):
return self.order_by('-last_update')
Expand Down
14 changes: 7 additions & 7 deletions emgapi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@
base_name='samples-studies'
)

router.register(
(r'studies/(?P<accession>[a-zA-Z0-9\-\_]+)/'
r'pipelines/(?P<release_version>[0-9\.]+)/'
r'summary'),
views_relations.StudySummaryViewSet,
base_name='studies-pipelines-summary'
)
# router.register(
# (r'studies/(?P<accession>[a-zA-Z0-9\-\_]+)/'
# r'pipelines/(?P<release_version>[0-9\.]+)/'
# r'summary'),
# views_relations.StudySummaryViewSet,
# base_name='studies-pipelines-summary'
# )


mydata_router = routers.DefaultRouter(trailing_slash=False)
Expand Down
3 changes: 1 addition & 2 deletions emgapi/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ class MyDataViewSet(mixins.ListModelMixin,

def get_queryset(self):
queryset = emg_models.Study.objects \
.mydata(self.request) \
.select_related('biome')
.mydata(self.request)
return queryset

def get_serializer_class(self):
Expand Down
31 changes: 11 additions & 20 deletions emgcli/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import warnings
import logging
import binascii
import datetime

from os.path import expanduser

Expand Down Expand Up @@ -165,11 +166,10 @@ def create_secret_key(var_dir):
'corsheaders',
# rest framework
'rest_framework',
'rest_framework.authtoken',
'rest_framework_swagger',
# 'rest_framework.authtoken',
'rest_framework_mongoengine',
'rest_framework_jwt',
'django_filters',
'rest_auth',
# apps
'emgapi',
'emgapianns',
Expand Down Expand Up @@ -276,7 +276,7 @@ def create_secret_key(var_dir):

REST_FRAMEWORK = {

'DEFAULT_VERSION': '0.3',
'DEFAULT_VERSION': '0.4',

'PAGE_SIZE': 20,

Expand Down Expand Up @@ -316,9 +316,9 @@ def create_secret_key(var_dir):
'rest_framework_json_api.metadata.JSONAPIMetadata',

'DEFAULT_AUTHENTICATION_CLASSES': (
# 'rest_framework.authentication.BasicAuthentication',
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
# 'rest_framework.authentication.BasicAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticatedOrReadOnly',
Expand All @@ -340,20 +340,6 @@ def create_secret_key(var_dir):
LOGIN_URL = 'rest_framework:login'
LOGOUT_URL = 'rest_framework:logout'

SWAGGER_SETTINGS = {
'ENABLED_METHODS': [
'get',
'post',
],
'SECURITY_DEFINITIONS': {
# 'basic': {
# 'type': 'basic'
# }
},
'USE_SESSION_AUTH': True,
'VALIDATOR_URL': None
}

# Custom settings
try:
EMG_DEFAULT_LIMIT = REST_FRAMEWORK['PAGE_SIZE']
Expand Down Expand Up @@ -414,6 +400,11 @@ def create_secret_key(var_dir):
'OPTIONS'
)

JWT_AUTH = {
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=108000),
'JWT_AUTH_HEADER_PREFIX': 'Bearer',
}

try:
ADMINS = EMG_CONF['emg']['admins']
MANAGERS = ADMINS
Expand Down
63 changes: 38 additions & 25 deletions emgcli/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,26 @@
from django.views.generic import RedirectView
from django.views.generic import TemplateView

from rest_framework import status
from rest_framework.schemas import get_schema_view
from rest_framework_swagger.views import get_swagger_view
from rest_framework.renderers import BaseRenderer, JSONRenderer

from rest_auth import views as rest_auth_views
from rest_framework_jwt.views import obtain_jwt_token
from rest_framework_jwt.views import verify_jwt_token

from emgapi.urls import router as emg_router
from emgapi.urls import mydata_router
from emgapianns.urls import mongo_router
from emgapianns.urls import router as emg_ext_router

from openapi_codec import OpenAPICodec

from . import routers


api_version = settings.REST_FRAMEWORK['DEFAULT_VERSION']


# merge all routers
router = routers.DefaultRouter(trailing_slash=False)
router.extend(emg_router)
Expand All @@ -42,50 +48,57 @@
router.extend(mydata_router)


# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
# API URL routing.
urlpatterns = [

# url(r'^admin/', admin.site.urls),

url(r'^$', RedirectView.as_view(
pattern_name='emgapi:api-root', permanent=False)),

url(r'^500$', TemplateView.as_view(template_name='500.html')),
url(r'^v%s/' % api_version, include(router.urls,
namespace='emgapi')),

]

# API authentication routing.
urlpatterns += [

url(r'^http-auth/', include('rest_framework.urls',
namespace='rest_framework')),

url(
r'^v%s/auth/login' % api_version,
rest_auth_views.LoginView.as_view(),
name='rest_auth_login'
),
url(r'^v%s/utils/token/obtain' % api_version, obtain_jwt_token,
name='obtain_jwt_token'),
url(r'^v%s/utils/token/verify' % api_version, verify_jwt_token,
name='verify_jwt_token'),

url(
r'^v%s/auth/logout' % api_version,
rest_auth_views.LogoutView.as_view(),
name='rest_auth_logout'
),

url(r'^v%s/' % api_version, include(router.urls,
namespace='emgapi')),
# url(r'^500$', TemplateView.as_view(template_name='500.html')),

]

# schema_prefix

class OpenAPIRenderer(BaseRenderer):
media_type = 'application/openapi+json'
charset = None
format = 'openapi'

def render(self, data, accepted_media_type=None, renderer_context=None):
if renderer_context['response'].status_code != status.HTTP_200_OK:
return JSONRenderer().render(data)
return OpenAPICodec().encode(data)


schema_view = get_schema_view(
title=settings.EMG_TITLE, url=settings.EMG_URL,
description=settings.EMG_DESC)

docs_schema_view = get_swagger_view(
title=settings.EMG_TITLE, url=settings.EMG_URL)
description=settings.EMG_DESC, renderer_classes=[OpenAPIRenderer]
)


urlpatterns += [

url(r'^schema/', schema_view),
url(r'^schema/$', schema_view, name="schema_view"),

url(r'^docs/', docs_schema_view),
url(r'^docs/',
TemplateView.as_view(template_name='swagger-ui/index.html')),

]
16 changes: 0 additions & 16 deletions emgui/static/emgui/css/screen.css

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file added emgui/static/swagger-ui-3.4.3/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added emgui/static/swagger-ui-3.4.3/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
125 changes: 125 additions & 0 deletions emgui/static/swagger-ui-3.4.3/swagger-ui-bundle.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions emgui/static/swagger-ui-3.4.3/swagger-ui-bundle.js.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions emgui/static/swagger-ui-3.4.3/swagger-ui-standalone-preset.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions emgui/static/swagger-ui-3.4.3/swagger-ui.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions emgui/static/swagger-ui-3.4.3/swagger-ui.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions emgui/static/swagger-ui-3.4.3/swagger-ui.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions emgui/static/swagger-ui-3.4.3/swagger-ui.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion emgui/templates/rest_framework/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@

<!-- local-title -->
<div class="columns medium-12" id="local-title">
{% include "rest_framework_swagger/include/header.html" %}
{% include "swagger-ui/include/header.html" %}
</div>
<!-- /local-title -->

Expand Down
11 changes: 0 additions & 11 deletions emgui/templates/rest_framework_swagger/include/swagger_css.html

This file was deleted.

10 changes: 0 additions & 10 deletions emgui/templates/rest_framework_swagger/include/swagger_doc.html

This file was deleted.

17 changes: 0 additions & 17 deletions emgui/templates/rest_framework_swagger/include/swagger_js.html

This file was deleted.

22 changes: 22 additions & 0 deletions emgui/templates/swagger-ui/include/swagger_css.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% load staticfiles %}

<link rel="stylesheet" type="text/css" href='{% static "swagger-ui-3.4.3/swagger-ui.css" %}' >
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*,
*:before,
*:after
{
box-sizing: inherit;
}

body {
margin:0;
background: #fafafa;
}
</style>
35 changes: 35 additions & 0 deletions emgui/templates/swagger-ui/include/swagger_doc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="position:absolute;width:0;height:0">
<defs>
<symbol viewBox="0 0 20 20" id="unlocked">
<path d="M15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V6h2v-.801C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8z"></path>
</symbol>

<symbol viewBox="0 0 20 20" id="locked">
<path d="M15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8zM12 8H8V5.199C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8z"/>
</symbol>

<symbol viewBox="0 0 20 20" id="close">
<path d="M14.348 14.849c-.469.469-1.229.469-1.697 0L10 11.819l-2.651 3.029c-.469.469-1.229.469-1.697 0-.469-.469-.469-1.229 0-1.697l2.758-3.15-2.759-3.152c-.469-.469-.469-1.228 0-1.697.469-.469 1.228-.469 1.697 0L10 8.183l2.651-3.031c.469-.469 1.228-.469 1.697 0 .469.469.469 1.229 0 1.697l-2.758 3.152 2.758 3.15c.469.469.469 1.229 0 1.698z"/>
</symbol>

<symbol viewBox="0 0 20 20" id="large-arrow">
<path d="M13.25 10L6.109 2.58c-.268-.27-.268-.707 0-.979.268-.27.701-.27.969 0l7.83 7.908c.268.271.268.709 0 .979l-7.83 7.908c-.268.271-.701.27-.969 0-.268-.269-.268-.707 0-.979L13.25 10z"/>
</symbol>

<symbol viewBox="0 0 20 20" id="large-arrow-down">
<path d="M17.418 6.109c.272-.268.709-.268.979 0s.271.701 0 .969l-7.908 7.83c-.27.268-.707.268-.979 0l-7.908-7.83c-.27-.268-.27-.701 0-.969.271-.268.709-.268.979 0L10 13.25l7.418-7.141z"/>
</symbol>


<symbol viewBox="0 0 24 24" id="jump-to">
<path d="M19 7v4H5.83l3.58-3.59L8 6l-6 6 6 6 1.41-1.41L5.83 13H21V7z"/>
</symbol>

<symbol viewBox="0 0 24 24" id="expand">
<path d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"/>
</symbol>

</defs>
</svg>

<div id="swagger-ui"></div>
33 changes: 33 additions & 0 deletions emgui/templates/swagger-ui/include/swagger_js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% load staticfiles %}

<script src='{% static "swagger-ui-3.4.3/swagger-ui-bundle.js" %}'> </script>
<script src='{% static "swagger-ui-3.4.3/swagger-ui-standalone-preset.js" %}'> </script>
<script>

var baseUrl = window.location.protocol + "//" + window.location.host

window.onload = function() {

// Build a system
const ui = SwaggerUIBundle({
url: baseUrl + "{% url 'schema_view' %}?format=openapi",
dom_id: '#swagger-ui',
deepLinking: true,
docExpansion: null,
validatorUrl: null,
supportHeaderParams: false,
jsonEditor: true,
supportedSubmitMethods: ["get", "post"],
presets: [
SwaggerUIBundle.presets.apis,
// SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
// layout: "StandaloneLayout"
})

window.ui = ui
}
</script>
Loading

0 comments on commit 2884f44

Please sign in to comment.