Skip to content

Commit b94a95f

Browse files
author
EliuX
committed
Fixes #23 Add support for configs and flex databases
1 parent 55b66ea commit b94a95f

File tree

8 files changed

+86
-3
lines changed

8 files changed

+86
-3
lines changed

tests/projects/projects_namespace_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ def test_list_should_return_nothing(client):
88
assert 200 == response.status_code
99

1010
json_data = json.loads(response.data)
11-
assert [] == json_data
11+
assert [] == json_data

tests/time_entries/__init__.py

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from flask import json
2+
3+
def test_list_should_return_empty_array(client):
4+
"""Should return an empty array"""
5+
response = client.get("/time-entries", follow_redirects=True)
6+
7+
assert 200 == response.status_code
8+
9+
json_data = json.loads(response.data)
10+
assert [] == json_data

time_tracker_api/__init__.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
1+
import os
12
from flask import Flask
23

34

4-
def create_app():
5+
def create_app(config_path='time_tracker_api.config.WhateverDevelopConfig',
6+
config_data=None):
57
flask_app = Flask(__name__)
68

9+
init_app_config(flask_app, config_path, config_data)
710
init_app(flask_app)
811

912
return flask_app
1013

1114

15+
def init_app_config(app, config_path, config_data=None):
16+
if config_path:
17+
app.config.from_object(config_path)
18+
else:
19+
# ensure the instance folder exists
20+
try:
21+
os.makedirs(app.instance_path)
22+
except OSError:
23+
pass
24+
25+
# Located in `/instance`
26+
app.config.from_pyfile('config.py', silent=True)
27+
28+
if config_data:
29+
app.config.update(config_data)
30+
31+
1232
def init_app(app: Flask):
33+
from .database import init_app as init_database
34+
init_database(app)
35+
1336
from .api import api
1437
api.init_app(app)

time_tracker_api/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Config:
2+
DEBUG = False
3+
4+
5+
class WhateverDevelopConfig(Config):
6+
DEBUG = True
7+
FLASK_DEBUG = True
8+
FLASK_ENV = "develop"
9+
DATABASE = "whatever"

time_tracker_api/database.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Agnostic database assets
3+
4+
Put here your utils and class independent of
5+
the database solution
6+
"""
7+
from flask import Flask
8+
9+
RepositoryModel = None
10+
11+
12+
def init_app(app: Flask) -> None:
13+
"""Make the app ready to use the database"""
14+
database_strategy_name = app.config['DATABASE']
15+
with app.app_context():
16+
module = globals()["use_%s" % database_strategy_name]()
17+
global RepositoryModel
18+
RepositoryModel = module.repository_model
19+
20+
21+
def create(model_name: str):
22+
"""Creates the repository instance for the chosen database"""
23+
return RepositoryModel(model_name)
24+
25+
26+
def use_whatever():
27+
from time_tracker_api import whatever_repository
28+
return whatever_repository

time_tracker_api/time_entries/time_entries_namespace.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from flask_restplus import fields, Resource, Namespace
22
from time_tracker_api.api import audit_fields
3+
from time_tracker_api import database
34

45
ns = Namespace('time-entries', description='API for time entries')
56

@@ -61,13 +62,15 @@
6162
)
6263

6364

65+
model = database.create('time-entries')
66+
6467
@ns.route('')
6568
class TimeEntries(Resource):
6669
@ns.doc('list_time_entries')
6770
@ns.marshal_list_with(time_entry_response, code=200)
6871
def get(self):
6972
"""List all available time entries"""
70-
return []
73+
return model.find_all()
7174

7275
@ns.doc('create_time_entry')
7376
@ns.expect(time_entry)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class WhateverRepository():
2+
def __init__(self, model_name):
3+
self.model_name = model_name
4+
5+
def find_all(self):
6+
print('Fetching all entries from {0}'.format(self.model_name))
7+
return []
8+
9+
10+
repository_model = WhateverRepository

0 commit comments

Comments
 (0)