Skip to content

Commit

Permalink
Reorganized core app to be more modular, allows for FileField swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
adelavega committed Jul 3, 2019
1 parent 8448b84 commit 5489c9e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 51 deletions.
25 changes: 25 additions & 0 deletions neuroscout/api_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Setup API docs and Swagger
from .core import app
from .auth import add_auth_to_swagger
from apispec import APISpec
from flask_apispec.extension import FlaskApiSpec
import webargs as wa
from apispec.ext.marshmallow import MarshmallowPlugin

file_plugin = MarshmallowPlugin()
spec = APISpec(
title='neuroscout',
version='v1',
plugins=[file_plugin],
openapi_version='2.0'
)
app.config.update({
'APISPEC_SPEC': spec})
add_auth_to_swagger(spec)

docs = FlaskApiSpec(app)


@file_plugin.map_to_openapi_type('file', None)
class FileField(wa.fields.Raw):
pass
35 changes: 11 additions & 24 deletions neuroscout/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,30 @@
from flask_security import Security
from flask_security.confirmable import confirm_email_token_status, confirm_user
from flask_cors import CORS
from apispec import APISpec
from flask_apispec.extension import FlaskApiSpec

from .basic import create_app, file_plugin
from .models import db
from .basic import create_app
from .models import db, user_datastore

app = create_app()
mail = Mail(app)
cache = Cache(
config={'CACHE_TYPE': 'filesystem', 'CACHE_DIR': app.config['CACHE_DIR']})
cache.init_app(app)
# Enable CORS
cors = CORS(app,
resources={r"/api/*": {"origins": "*"},
r"/swagger/": {"origins": "*"}})
cors = CORS(
app,
resources={r"/api/*": {"origins": "*"}, r"/swagger/": {"origins": "*"}})

# Setup Flask-Security and JWT
from .auth import authenticate, load_user, add_auth_to_swagger
from .models import *
# These imports require the above
from .auth import authenticate, load_user
from .utils.factory import route_factory
from .api_spec import docs

# Setup Flask-Security and JWT
security = Security(app, user_datastore)
jwt = JWT(app, authenticate, load_user)


# Setup API and Swagger
from .utils.core import route_factory
spec = APISpec(
title='neuroscout',
version='v1',
plugins=[file_plugin],
openapi_version='2.0'
)
app.config.update({
'APISPEC_SPEC': spec})
add_auth_to_swagger(spec)
docs = FlaskApiSpec(app)

# Set up API routes
route_factory(
app, docs,
[
Expand Down
25 changes: 8 additions & 17 deletions neuroscout/resources/analysis/reports.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import datetime
import tempfile
from hashids import Hashids
import webargs as wa
from flask_apispec import doc, use_kwargs, MethodResource, marshal_with
from flask import current_app

from ...models import Report, NeurovaultCollection
from ...database import db
from ...worker import celery_app
import webargs as wa
from marshmallow import fields
from ..utils import owner_required, abort, fetch_analysis
from ...schemas.analysis import (
ReportSchema, AnalysisCompiledSchema, NeurovaultCollectionSchema)
from ...utils.db import put_record

import datetime
import tempfile
from hashids import Hashids
from ..utils import owner_required, abort, fetch_analysis
from ...api_spec import FileField


def _validation_hash(analysis_id):
Expand Down Expand Up @@ -58,7 +58,7 @@ def get(self, analysis):

@marshal_with(ReportSchema)
@use_kwargs({
'run_id': wa.fields.DelimitedList(fields.Int(),
'run_id': wa.fields.DelimitedList(wa.fields.Int(),
description='Run id(s).'),
'sampling_rate': wa.fields.Number(description='Sampling rate in Hz'),
'scale': wa.fields.Boolean(description='Scale columns for plotting'),
Expand Down Expand Up @@ -110,15 +110,6 @@ def get(self, analysis, run_id=None):
return report


# Use current_app
# file_plugin = MarshmallowPlugin()
#
#
# @file_plugin.map_to_openapi_type('file', None)
class FileField(wa.fields.Raw):
pass


@doc(tags=['analysis'])
class AnalysisUploadResource(MethodResource):
@doc(summary='Upload fitlins analysis tarball.',
Expand Down
6 changes: 6 additions & 0 deletions neuroscout/resources/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,9 @@ def get(self, **kwargs):
query = query.filter(
getattr(PredictorEvent, param).in_(kwargs[param]))
return dump_pe(query)


class PredictorCreateResource(MethodResource):
@doc(tags=['predictors'], summary='Create a new predictor')
def post(self, **kwargs):
return {}
10 changes: 0 additions & 10 deletions neuroscout/utils/core.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
"""
Misc. utilities useful across package.
"""
from .. import resources


def route_factory(app, docs, pairings, prepend='/api/'):
""" Create API routes and add to app """
for res_name, route in pairings:
res = getattr(resources, res_name)
app.add_url_rule(prepend + route,
view_func=res.as_view(res_name.lower()))
docs.register(res)


def listify(obj):
Expand Down
11 changes: 11 additions & 0 deletions neuroscout/utils/factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from .. import resources
from ..models import *


def route_factory(app, docs, pairings, prepend='/api/'):
""" Create API routes and add to app """
for res_name, route in pairings:
res = getattr(resources, res_name)
app.add_url_rule(prepend + route,
view_func=res.as_view(res_name.lower()))
docs.register(res)

0 comments on commit 5489c9e

Please sign in to comment.