API - remove init-data step since no additional data is now required

+ remove config init from removed env. variables
This commit is contained in:
Sam 2022-02-12 14:12:47 +01:00
parent e751fec1d0
commit 1a53d4a991
9 changed files with 66 additions and 76 deletions

View File

@ -93,7 +93,6 @@ firefox:
- poetry config virtualenvs.create false - poetry config virtualenvs.create false
- poetry install --no-interaction --quiet - poetry install --no-interaction --quiet
- flask db upgrade --directory fittrackee/migrations - flask db upgrade --directory fittrackee/migrations
- flask init-data
- setsid nohup flask run --with-threads -h 0.0.0.0 -p 5000 >> nohup.out 2>&1 & - 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 - export TEST_APP_URL=http://$(hostname --ip-address):5000
- sleep 5 - sleep 5

View File

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

View File

@ -5,5 +5,4 @@ cd /usr/src/app
source .env.docker source .env.docker
flask drop-db flask drop-db
flask db upgrade --directory fittrackee/migrations flask db upgrade --directory fittrackee/migrations
flask init-data

View File

@ -51,16 +51,16 @@ def create_app() -> Flask:
email_service.init_email(app) email_service.init_email(app)
# get configuration from database # get configuration from database
from .application.models import AppConfig from .application.utils import (
from .application.utils import init_config, update_app_config_from_database get_or_init_config,
update_app_config_from_database,
)
with app.app_context(): with app.app_context():
# Note: check if "app_config" table exist to avoid errors when # Note: check if "app_config" table exist to avoid errors when
# dropping tables on dev environments # dropping tables on dev environments
if db.engine.dialect.has_table(db.engine.connect(), 'app_config'): if db.engine.dialect.has_table(db.engine.connect(), 'app_config'):
db_app_config = AppConfig.query.one_or_none() db_app_config = get_or_init_config()
if not db_app_config:
_, db_app_config = init_config()
update_app_config_from_database(app, db_app_config) update_app_config_from_database(app, db_app_config)
from .application.app_config import config_blueprint # noqa from .application.app_config import config_blueprint # noqa

View File

@ -11,8 +11,6 @@ from flask_migrate import upgrade
from tqdm import tqdm from tqdm import tqdm
from fittrackee import create_app, db from fittrackee import create_app, db
from fittrackee.application.utils import init_config
from fittrackee.database_utils import init_database
from fittrackee.workouts.models import Workout from fittrackee.workouts.models import Workout
from fittrackee.workouts.utils import update_workout from fittrackee.workouts.utils import update_workout
@ -61,12 +59,6 @@ def drop_db() -> None:
print('Uploaded files deleted.') print('Uploaded files deleted.')
@app.cli.command('init-data')
def init_data() -> None:
"""Init the database and application config."""
init_database(app)
@app.cli.command() @app.cli.command()
def recalculate() -> None: def recalculate() -> None:
print("Starting workouts data refresh") print("Starting workouts data refresh")
@ -85,20 +77,6 @@ def recalculate() -> None:
db.session.commit() db.session.commit()
@app.cli.command('init-app-config')
def init_app_config() -> None:
"""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() -> None: def main() -> None:
options = {'bind': f'{HOST}:{PORT}', 'workers': WORKERS} options = {'bind': f'{HOST}:{PORT}', 'workers': WORKERS}
StandaloneApplication(app, options).run() StandaloneApplication(app, options).run()

View File

@ -1,42 +1,28 @@
import os from typing import Dict, List
from typing import Dict, List, Tuple
from flask import Flask from flask import Flask
from fittrackee import db from fittrackee import db
from fittrackee.users.models import User
from .models import AppConfig from .models import AppConfig
MAX_FILE_SIZE = 1 * 1024 * 1024 # 1MB MAX_FILE_SIZE = 1 * 1024 * 1024 # 1MB
def init_config() -> Tuple[bool, AppConfig]: def get_or_init_config() -> AppConfig:
""" """
init application configuration if not existing in database Init application configuration.
Note: get some configuration values from env variables
(for FitTrackee versions prior to v0.3.0)
""" """
existing_config = AppConfig.query.one_or_none() existing_config = AppConfig.query.one_or_none()
nb_users = User.query.count() if existing_config:
if not existing_config: return existing_config
config = AppConfig() config = AppConfig()
config.max_users = ( config.max_users = 0 # no limitation
nb_users config.max_single_file_size = MAX_FILE_SIZE
if os.getenv('REACT_APP_ALLOW_REGISTRATION') == "false" config.max_zip_file_size = MAX_FILE_SIZE * 10
else 0 db.session.add(config)
) db.session.commit()
config.max_single_file_size = os.environ.get( return config
'REACT_APP_MAX_SINGLE_FILE_SIZE', MAX_FILE_SIZE
)
config.max_zip_file_size = os.environ.get(
'REACT_APP_MAX_ZIP_FILE_SIZE', MAX_FILE_SIZE * 10
)
db.session.add(config)
db.session.commit()
return True, config
return False, existing_config
def update_app_config_from_database( def update_app_config_from_database(

View File

@ -1,14 +0,0 @@
from flask import Flask
from fittrackee.application.utils import (
init_config,
update_app_config_from_database,
)
def init_database(app: Flask) -> None:
"""Init the database."""
_, db_app_config = init_config()
update_app_config_from_database(app, db_app_config)
print('Initial data stored in database.')

View File

@ -0,0 +1,48 @@
from flask import Flask
from fittrackee.application.models import AppConfig
from fittrackee.application.utils import get_or_init_config
class TestGetOrInitAppConfig:
def test_it_creates_app_config(self, app_no_config: Flask) -> None:
get_or_init_config()
assert AppConfig.query.count() == 1
def test_it_inits_max_users_with_default_value(
self, app_no_config: Flask
) -> None:
get_or_init_config()
app_config = AppConfig.query.first()
assert app_config.max_users == 0
def test_it_inits_max_single_file_size_with_default_value(
self, app_no_config: Flask
) -> None:
get_or_init_config()
app_config = AppConfig.query.first()
assert app_config.max_single_file_size == 1048576 # 1MB
def test_it_inits_max_zip_file_size_with_default_value(
self, app_no_config: Flask
) -> None:
get_or_init_config()
app_config = AppConfig.query.first()
assert app_config.max_zip_file_size == 10485760 # 10MB
def test_it_inits_gpx_limit_import_with_default_value(
self, app_no_config: Flask
) -> None:
get_or_init_config()
app_config = AppConfig.query.first()
assert app_config.gpx_limit_import == 10
def test_it_returns_existing_config(self, app: Flask) -> None:
app_config = get_or_init_config()
assert app_config.max_users == 100

View File

@ -63,7 +63,6 @@ Sphinx = "^4.4.0"
[tool.poetry.scripts] [tool.poetry.scripts]
fittrackee = 'fittrackee.__main__:main' fittrackee = 'fittrackee.__main__:main'
fittrackee_init_data = 'fittrackee.__main__:init_data'
fittrackee_upgrade_db = 'fittrackee.__main__:upgrade_db' fittrackee_upgrade_db = 'fittrackee.__main__:upgrade_db'
fittrackee_worker = 'fittrackee.__main__:dramatiq_worker' fittrackee_worker = 'fittrackee.__main__:dramatiq_worker'