API - minor refactor

This commit is contained in:
Sam 2020-09-17 16:08:57 +02:00
parent 8c84afe793
commit b44a2cef53
5 changed files with 60 additions and 74 deletions

View File

@ -8,7 +8,7 @@ variables:
APP_SETTINGS: fittrackee.config.TestingConfig
DATABASE_TEST_URL: postgres://fittrackee:fittrackee@postgres:5432/fittrackee_test
EMAIL_URL: smtp://none:none@0.0.0.0:1025
FLASK_APP: fittrackee/server.py
FLASK_APP: fittrackee/__main__.py
SENDER_EMAIL: fittrackee@example.com
services:
@ -57,7 +57,7 @@ firefox:
- poetry config virtualenvs.create false
- poetry install --no-interaction --quiet
- flask db upgrade --directory fittrackee/migrations
- flask initdata
- flask init-data
- setsid nohup flask run --with-threads -h 0.0.0.0 -p 5000 >> nohup.out 2>&1 &
- export TEST_APP_URL=http://$(hostname --ip-address):5000
- sleep 5

View File

@ -27,7 +27,7 @@ html:
install-db:
psql -U postgres -f db/create.sql
$(FLASK) db upgrade --directory $(MIGRATIONS)
$(FLASK) initdata
$(FLASK) init-data
init-app-config:
$(FLASK) init-app-config
@ -35,7 +35,7 @@ init-app-config:
init-db:
$(FLASK) drop-db
$(FLASK) db upgrade --directory $(MIGRATIONS)
$(FLASK) initdata
$(FLASK) init-data
install: install-client install-python

View File

@ -2,7 +2,7 @@ HOST = 0.0.0.0
API_PORT = 5000
CLIENT_PORT = 3000
export FLASK_APP = $(PWD)/fittrackee/server.py
export FLASK_APP = $(PWD)/fittrackee/__main__.py
export FLASK_ENV=development
export TEST_APP_URL = http://$(HOST):$(API_PORT)
export TEST_CLIENT_URL = http://$(HOST):$(CLIENT_PORT)

View File

@ -1,11 +1,17 @@
# source: http://docs.gunicorn.org/en/stable/custom.html
# source for StandaloneApplication class:
# http://docs.gunicorn.org/en/stable/custom.html
import os
import shutil
import gunicorn.app.base
from fittrackee import create_app
from fittrackee import create_app, db
from fittrackee.activities.models import Activity
from fittrackee.activities.utils import update_activity
from fittrackee.application.utils import init_config
from fittrackee.database_utils import init_database
from flask_dramatiq import worker
from flask_migrate import upgrade
from tqdm import tqdm
HOST = os.getenv('HOST', '0.0.0.0')
PORT = os.getenv('API_PORT', '5000')
@ -39,9 +45,53 @@ def upgrade_db():
upgrade(directory=BASEDIR + '/migrations')
@app.cli.command('drop-db')
def drop_db():
"""Empty database for dev environments."""
db.engine.execute("DROP TABLE IF EXISTS alembic_version;")
db.drop_all()
db.session.commit()
print('Database dropped.')
shutil.rmtree(app.config['UPLOAD_FOLDER'], ignore_errors=True)
print('Uploaded files deleted.')
@app.cli.command('init-data')
def init_data():
with app.app_context():
init_database(app)
"""Init the database and application config."""
init_database(app)
@app.cli.command()
def recalculate():
print("Starting activities data refresh")
activities = (
Activity.query.filter(Activity.gpx != None) # noqa
.order_by(Activity.activity_date.asc()) # noqa
.all()
)
if len(activities) == 0:
print('➡️ no activities to upgrade.')
return None
pbar = tqdm(activities)
for activity in pbar:
update_activity(activity)
pbar.set_postfix(activitiy_id=activity.id)
db.session.commit()
@app.cli.command('init-app-config')
def init_app_config():
"""Init application configuration."""
print("Init application configuration")
config_created, _ = init_config()
if config_created:
print("Creation done!")
else:
print(
"Application configuration already existing in database. "
"Please use web application to update it."
)
def main():
@ -50,4 +100,4 @@ def main():
if __name__ == '__main__':
main()
app.run()

View File

@ -1,64 +0,0 @@
import shutil
from fittrackee import create_app, db
from fittrackee.activities.models import Activity
from fittrackee.activities.utils import update_activity
from fittrackee.application.utils import init_config
from tqdm import tqdm
from .database_utils import init_database
app = create_app()
@app.cli.command()
def drop_db():
"""Empty database for dev environments."""
db.engine.execute("DROP TABLE IF EXISTS alembic_version;")
db.drop_all()
db.session.commit()
print('Database dropped.')
shutil.rmtree(app.config['UPLOAD_FOLDER'], ignore_errors=True)
print('Uploaded files deleted.')
@app.cli.command()
def initdata():
"""Init the database and application config."""
init_database(app)
@app.cli.command()
def recalculate():
print("Starting activities data refresh")
activities = (
Activity.query.filter(Activity.gpx != None) # noqa
.order_by(Activity.activity_date.asc()) # noqa
.all()
)
if len(activities) == 0:
print('➡️ no activities to upgrade.')
return None
pbar = tqdm(activities)
for activity in pbar:
update_activity(activity)
pbar.set_postfix(activitiy_id=activity.id)
db.session.commit()
@app.cli.command('init-app-config')
def init_app_config():
"""Init application configuration."""
print("Init application configuration")
config_created, _ = init_config()
if config_created:
print("Creation done!")
else:
print(
"Application configuration already existing in database. "
"Please use web application to update it."
)
if __name__ == '__main__':
app.run()