-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor dataservice to be more modular
Create common module for common code among resources Create new folder for person tests, move all person tests into it Separate out model code, resource code, serialization code for Person resources Refactor and replace Flask-Script with built in CLI Add support for api versioning Clean up Flask CLI commands Change api prefix from /api/v1 to /v1 Remove Flask-Script dependenct Replace relative imports with absolute imports
- Loading branch information
Showing
16 changed files
with
159 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,69 @@ | ||
import os | ||
from flask import Flask, jsonify | ||
from flask_sqlalchemy import SQLAlchemy | ||
from flask_restplus import Api | ||
from config import config, DevelopmentConfig | ||
# -*- coding: utf-8 -*- | ||
"""The app module, containing the app factory function.""" | ||
from flask import Flask | ||
|
||
db = SQLAlchemy() | ||
from dataservice import commands | ||
from dataservice.extensions import db, migrate | ||
from dataservice.api.person.models import Person | ||
from config import config | ||
|
||
|
||
def create_app(config_name): | ||
""" | ||
An application factory | ||
""" | ||
app = Flask(__name__) | ||
app.url_map.strict_slashes = False | ||
app.config.from_object(config[config_name]) | ||
config[config_name].init_app(app) | ||
|
||
db.init_app(app) | ||
from dataservice.api import api | ||
api.init_app(app) | ||
# Register Flask extensions | ||
register_extensions(app) | ||
register_shellcontext(app) | ||
register_commands(app) | ||
register_blueprints(app) | ||
|
||
return app | ||
|
||
|
||
def register_shellcontext(app): | ||
""" | ||
Register shell context objects | ||
""" | ||
|
||
def shell_context(): | ||
"""Shell context objects.""" | ||
return {'db': db, | ||
'Person': Person} | ||
|
||
app.shell_context_processor(shell_context) | ||
|
||
|
||
def register_commands(app): | ||
""" | ||
Register Click commands | ||
""" | ||
app.cli.add_command(commands.test) | ||
|
||
|
||
def register_extensions(app): | ||
""" | ||
Register Flask extensions | ||
""" | ||
|
||
# SQLAlchemy | ||
db.init_app(app) | ||
|
||
# Migrate | ||
migrate.init_app(app, db) | ||
|
||
|
||
def register_error_handlers(app): | ||
""" | ||
Register error handlers | ||
""" | ||
pass | ||
|
||
|
||
def register_blueprints(app): | ||
from dataservice.api import api_v1 | ||
app.register_blueprint(api_v1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .person import person_api | ||
from .resources import person_api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from dataservice.extensions import db | ||
from dataservice.api.common.model import Base | ||
|
||
|
||
class Person(Base): | ||
""" | ||
Person entity. | ||
:param _id: Unique id assigned by RDBMS | ||
:param kf_id: Unique id given by the Kid's First DCC | ||
:param external_id: Name given to person by contributor | ||
:param created_at: Time of object creation | ||
:param modified_at: Last time of object modification | ||
""" | ||
__tablename__ = "person" | ||
external_id = db.Column(db.String(32)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from datetime import datetime | ||
from flask_restplus import fields | ||
|
||
from dataservice.api.person.resources import person_api | ||
|
||
person_model = person_api.model('Person', { | ||
'kf_id': fields.String( | ||
example='KF00001', | ||
description='ID assigned by Kids First'), | ||
'created_at': fields.String( | ||
example=datetime.now().isoformat(), | ||
description='Date Person was registered in with the DCC'), | ||
'modified_at': fields.String( | ||
example=datetime.now().isoformat(), | ||
description='Date of last update to the Persons data'), | ||
'external_id': fields.String( | ||
example='SUBJ-3993', | ||
description='Identifier used in the original study data') | ||
}) | ||
|
||
person_list = person_api.model("Persons", { | ||
"persons": fields.List(fields.Nested(person_model)) | ||
}) | ||
|
||
response_model = person_api.model('Response', { | ||
'content': fields.Nested(person_list), | ||
'status': fields.Integer( | ||
description='HTTP response status code', | ||
example=200), | ||
'message': fields.String( | ||
description='Additional information about the response', | ||
example='Success') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Click commands.""" | ||
|
||
import click | ||
|
||
|
||
@click.command() | ||
def test(): | ||
""" Run the unit tests and pep8 checks """ | ||
from subprocess import call | ||
call(["python", "-m", "pytest", "tests"]) | ||
call(["python", "-m", "pytest", "--pep8", "dataservice"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from flask_migrate import Migrate | ||
from flask_sqlalchemy import SQLAlchemy, Model | ||
|
||
|
||
db = SQLAlchemy() | ||
|
||
migrate = Migrate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,9 @@ | ||
#!/usr/bin/env python | ||
import os | ||
#! /usr/bin/env python | ||
|
||
from dataservice import create_app, db | ||
from flask_script import Manager, Shell | ||
from flask_migrate import Migrate, MigrateCommand | ||
import os | ||
from dataservice import create_app | ||
|
||
app = create_app(os.getenv('FLASK_CONFIG') or 'default') | ||
manager = Manager(app) | ||
migrate = Migrate(app, db) | ||
|
||
|
||
def make_shell_context(): | ||
return dict(app=app, db=db) | ||
|
||
|
||
manager.add_command("shell", Shell(make_context=make_shell_context)) | ||
manager.add_command('db', MigrateCommand) | ||
|
||
|
||
@manager.command | ||
def test(coverage=False): | ||
""" Run the unit tests and pep8 checks """ | ||
from subprocess import call | ||
call(["python","-m","pytest","tests"]) | ||
call(["python","-m","pytest","--pep8","dataservice"]) | ||
|
||
|
||
@manager.command | ||
def deploy(): | ||
""" Run deployment tasks """ | ||
from flask.ext.migrate import upgrade | ||
|
||
# migrate database to latest revision | ||
upgrade() | ||
|
||
|
||
if __name__ == '__main__': | ||
manager.run() | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters