-
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: organize application as a package (#140)
- Loading branch information
Showing
18 changed files
with
266 additions
and
236 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 +1,2 @@ | ||
FLASK_APP=eligibility_server/app.py | ||
ELIGIBILITY_SERVER_SETTINGS=../config/sample.py |
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 |
---|---|---|
|
@@ -27,6 +27,6 @@ jobs: | |
|
||
- name: Test with pytest | ||
run: | | ||
python setup.py | ||
flask init-db | ||
coverage run -m pytest | ||
coverage report -m |
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ __pycache__/ | |
.env | ||
config/* | ||
!config/sample.py | ||
eligibility_server.egg-info |
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 |
---|---|---|
|
@@ -3,4 +3,4 @@ set -eux | |
|
||
# run database migrations | ||
|
||
python setup.py | ||
flask init-db |
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 was deleted.
Oops, something went wrong.
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,17 @@ | ||
import logging | ||
|
||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
db = SQLAlchemy() | ||
|
||
|
||
def init_app(app): | ||
db.init_app(app) | ||
|
||
from .setup import init_db_command, drop_db_command | ||
|
||
app.cli.add_command(init_db_command) | ||
app.cli.add_command(drop_db_command) |
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,11 @@ | ||
from eligibility_server.db import db | ||
|
||
|
||
class User(db.Model): | ||
id = db.Column(db.Integer, primary_key=True) | ||
sub = db.Column(db.String, unique=True, nullable=False) | ||
name = db.Column(db.String, unique=True, nullable=False) | ||
types = db.Column(db.String, unique=False, nullable=False) | ||
|
||
def __repr__(self): | ||
return "<User %r>" % self.sub |
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,95 @@ | ||
import csv | ||
import json | ||
|
||
import click | ||
from flask import current_app | ||
from flask_sqlalchemy import inspect | ||
|
||
from eligibility_server.db.models import db, User | ||
|
||
|
||
@click.command("init-db") | ||
def init_db_command(): | ||
with current_app.app_context(): | ||
inspector = inspect(db.engine) | ||
|
||
if inspector.get_table_names(): | ||
click.echo("Tables already exist.") | ||
if User.query.count() == 0: | ||
import_users() | ||
else: | ||
click.echo("User table already has data.") | ||
else: | ||
click.echo("Creating table...") | ||
db.create_all() | ||
click.echo("Table created.") | ||
|
||
import_users() | ||
|
||
|
||
def import_users(): | ||
""" | ||
Imports user data to be added to database and saves user to database | ||
Users can be imported from either a JSON file or CSV file, as configured | ||
with settings. CSV files take extra setting configurations: | ||
CSV_DELIMITER, CSV_NEWLINE, CSV_QUOTING, CSV_QUOTECHAR | ||
""" | ||
|
||
file_path = current_app.config["IMPORT_FILE_PATH"] | ||
click.echo(f"Importing users from {file_path}") | ||
|
||
file_format = file_path.split(".")[-1] | ||
|
||
if file_format == "json": | ||
with open(file_path) as file: | ||
data = json.load(file)["users"] | ||
for user in data: | ||
save_users(user, data[user][0], str(data[user][1])) | ||
elif file_format == "csv": | ||
with open(file_path, newline=current_app.config["CSV_NEWLINE"], encoding="utf-8") as file: | ||
data = csv.reader( | ||
file, | ||
delimiter=current_app.config["CSV_DELIMITER"], | ||
quoting=int(current_app.config["CSV_QUOTING"]), | ||
quotechar=current_app.config["CSV_QUOTECHAR"], | ||
) | ||
for user in data: | ||
save_users(user[0], user[1], user[2]) | ||
else: | ||
click.echo(f"Warning: File format is not supported: {file_format}") | ||
|
||
click.echo(f"Users added: {User.query.count()}") | ||
|
||
|
||
def save_users(sub: str, name: str, types: str): | ||
""" | ||
Add users to the database User table | ||
@param sub - User's sub | ||
@param name - User's name | ||
@param types - Types of eligibilities, in a stringified list | ||
""" | ||
|
||
item = User(sub=sub, name=name, types=types) | ||
db.session.add(item) | ||
db.session.commit() | ||
|
||
|
||
@click.command("drop-db") | ||
def drop_db_command(): | ||
with current_app.app_context(): | ||
inspector = inspect(db.engine) | ||
|
||
if inspector.get_table_names(): | ||
try: | ||
click.echo(f"Users to be deleted: {User.query.count()}") | ||
User.query.delete() | ||
db.session.commit() | ||
except Exception as e: | ||
click.echo("Failed to query for Users", e) | ||
|
||
db.drop_all() | ||
click.echo("Database dropped.") | ||
else: | ||
click.echo("Database does not exist.") |
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
Oops, something went wrong.