From 1a53d4a991dab11bf0b24248790821844470b749 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 12 Feb 2022 14:12:47 +0100 Subject: [PATCH] API - remove `init-data` step since no additional data is now required + remove config init from removed env. variables --- .gitlab-ci.yml | 1 - Makefile | 5 -- docker/init-database.sh | 3 +- fittrackee/__init__.py | 10 ++-- fittrackee/__main__.py | 22 --------- fittrackee/application/utils.py | 38 +++++---------- fittrackee/database_utils.py | 14 ------ .../tests/application/test_database_utils.py | 48 +++++++++++++++++++ pyproject.toml | 1 - 9 files changed, 66 insertions(+), 76 deletions(-) delete mode 100644 fittrackee/database_utils.py create mode 100644 fittrackee/tests/application/test_database_utils.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6264413a..884434da 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -93,7 +93,6 @@ firefox: - poetry config virtualenvs.create false - poetry install --no-interaction --quiet - 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 & - export TEST_APP_URL=http://$(hostname --ip-address):5000 - sleep 5 diff --git a/Makefile b/Makefile index 5ef48d89..ffd7a1fc 100644 --- a/Makefile +++ b/Makefile @@ -85,15 +85,10 @@ html: install-db: psql -U postgres -f db/create.sql $(FLASK) db upgrade --directory $(MIGRATIONS) - $(FLASK) init-data - -init-app-config: - $(FLASK) init-app-config init-db: $(FLASK) drop-db $(FLASK) db upgrade --directory $(MIGRATIONS) - $(FLASK) init-data install: install-client install-python diff --git a/docker/init-database.sh b/docker/init-database.sh index ece8cc19..c89017b9 100755 --- a/docker/init-database.sh +++ b/docker/init-database.sh @@ -5,5 +5,4 @@ cd /usr/src/app source .env.docker flask drop-db -flask db upgrade --directory fittrackee/migrations -flask init-data \ No newline at end of file +flask db upgrade --directory fittrackee/migrations \ No newline at end of file diff --git a/fittrackee/__init__.py b/fittrackee/__init__.py index a7e63363..24f9f14c 100644 --- a/fittrackee/__init__.py +++ b/fittrackee/__init__.py @@ -51,16 +51,16 @@ def create_app() -> Flask: email_service.init_email(app) # get configuration from database - from .application.models import AppConfig - from .application.utils import init_config, update_app_config_from_database + from .application.utils import ( + get_or_init_config, + update_app_config_from_database, + ) with app.app_context(): # Note: check if "app_config" table exist to avoid errors when # dropping tables on dev environments if db.engine.dialect.has_table(db.engine.connect(), 'app_config'): - db_app_config = AppConfig.query.one_or_none() - if not db_app_config: - _, db_app_config = init_config() + db_app_config = get_or_init_config() update_app_config_from_database(app, db_app_config) from .application.app_config import config_blueprint # noqa diff --git a/fittrackee/__main__.py b/fittrackee/__main__.py index e9e38735..ee325e44 100644 --- a/fittrackee/__main__.py +++ b/fittrackee/__main__.py @@ -11,8 +11,6 @@ from flask_migrate import upgrade from tqdm import tqdm 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.utils import update_workout @@ -61,12 +59,6 @@ def drop_db() -> None: 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() def recalculate() -> None: print("Starting workouts data refresh") @@ -85,20 +77,6 @@ def recalculate() -> None: 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: options = {'bind': f'{HOST}:{PORT}', 'workers': WORKERS} StandaloneApplication(app, options).run() diff --git a/fittrackee/application/utils.py b/fittrackee/application/utils.py index d0d20b4b..eb8183fc 100644 --- a/fittrackee/application/utils.py +++ b/fittrackee/application/utils.py @@ -1,42 +1,28 @@ -import os -from typing import Dict, List, Tuple +from typing import Dict, List from flask import Flask from fittrackee import db -from fittrackee.users.models import User from .models import AppConfig 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 - - Note: get some configuration values from env variables - (for FitTrackee versions prior to v0.3.0) + Init application configuration. """ existing_config = AppConfig.query.one_or_none() - nb_users = User.query.count() - if not existing_config: - config = AppConfig() - config.max_users = ( - nb_users - if os.getenv('REACT_APP_ALLOW_REGISTRATION') == "false" - else 0 - ) - config.max_single_file_size = os.environ.get( - '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 + if existing_config: + return existing_config + config = AppConfig() + config.max_users = 0 # no limitation + config.max_single_file_size = MAX_FILE_SIZE + config.max_zip_file_size = MAX_FILE_SIZE * 10 + db.session.add(config) + db.session.commit() + return config def update_app_config_from_database( diff --git a/fittrackee/database_utils.py b/fittrackee/database_utils.py deleted file mode 100644 index aab675dc..00000000 --- a/fittrackee/database_utils.py +++ /dev/null @@ -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.') diff --git a/fittrackee/tests/application/test_database_utils.py b/fittrackee/tests/application/test_database_utils.py new file mode 100644 index 00000000..2d648efd --- /dev/null +++ b/fittrackee/tests/application/test_database_utils.py @@ -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 diff --git a/pyproject.toml b/pyproject.toml index b87413a4..e89a3d8a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -63,7 +63,6 @@ Sphinx = "^4.4.0" [tool.poetry.scripts] fittrackee = 'fittrackee.__main__:main' -fittrackee_init_data = 'fittrackee.__main__:init_data' fittrackee_upgrade_db = 'fittrackee.__main__:upgrade_db' fittrackee_worker = 'fittrackee.__main__:dramatiq_worker'