-
Notifications
You must be signed in to change notification settings - Fork 0
Create configuration and database routing #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from flask import json | ||
|
|
||
| def test_list_should_return_empty_array(client): | ||
| """Should return an empty array""" | ||
| response = client.get("/time-entries", follow_redirects=True) | ||
|
|
||
| assert 200 == response.status_code | ||
|
|
||
| json_data = json.loads(response.data) | ||
| assert [] == json_data |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,37 @@ | ||
| import os | ||
| from flask import Flask | ||
|
|
||
|
|
||
| def create_app(): | ||
| def create_app(config_path='time_tracker_api.config.WhateverDevelopConfig', | ||
| config_data=None): | ||
| flask_app = Flask(__name__) | ||
|
|
||
| init_app_config(flask_app, config_path, config_data) | ||
| init_app(flask_app) | ||
|
|
||
| return flask_app | ||
|
|
||
|
|
||
| def init_app_config(app, config_path, config_data=None): | ||
| if config_path: | ||
| app.config.from_object(config_path) | ||
| else: | ||
| # ensure the instance folder exists | ||
| try: | ||
| os.makedirs(app.instance_path) | ||
| except OSError: | ||
| pass | ||
|
|
||
| # Located in `/instance` | ||
| app.config.from_pyfile('config.py', silent=True) | ||
|
|
||
| if config_data: | ||
| app.config.update(config_data) | ||
|
|
||
|
|
||
| def init_app(app: Flask): | ||
| from .database import init_app as init_database | ||
| init_database(app) | ||
|
|
||
| from .api import api | ||
| api.init_app(app) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class Config: | ||
| DEBUG = False | ||
|
|
||
|
|
||
| class WhateverDevelopConfig(Config): | ||
| DEBUG = True | ||
| FLASK_DEBUG = True | ||
| FLASK_ENV = "develop" | ||
| DATABASE = "whatever" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """ | ||
| Agnostic database assets | ||
| Put here your utils and class independent of | ||
| the database solution | ||
| """ | ||
| from flask import Flask | ||
|
|
||
| RepositoryModel = None | ||
|
|
||
|
|
||
| def init_app(app: Flask) -> None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not call this function 'init_database' directly ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is some sort of standard that some objects like the APIs in flask Restplus have a function called init_app so they take the Flask app as a parameter and they do whatever initialization logic they need to be ready to be used by that app. So whatever module or package you add if you want to make it like pluggable to Flask this is pretty much the way to go |
||
| """Make the app ready to use the database""" | ||
| database_strategy_name = app.config['DATABASE'] | ||
| with app.app_context(): | ||
| module = globals()["use_%s" % database_strategy_name]() | ||
| global RepositoryModel | ||
| RepositoryModel = module.repository_model | ||
|
|
||
|
|
||
| def create(model_name: str): | ||
| """Creates the repository instance for the chosen database""" | ||
| return RepositoryModel(model_name) | ||
|
|
||
|
|
||
| def use_whatever(): | ||
| from time_tracker_api import whatever_repository | ||
| return whatever_repository | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| class WhateverRepository(): | ||
| def __init__(self, model_name): | ||
| self.model_name = model_name | ||
|
|
||
| def find_all(self): | ||
| print('Fetching all entries from {0}'.format(self.model_name)) | ||
| return [] | ||
|
|
||
|
|
||
| repository_model = WhateverRepository |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we have type hints here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure