From d1aa220e1df6c6b3f24cc39eeefa4db445935eb3 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 13 Feb 2022 14:29:08 +0100 Subject: [PATCH 1/5] API - fix dramatiq warning when launching workers w/ script entrypoint --- fittrackee/__main__.py | 2 -- pyproject.toml | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/fittrackee/__main__.py b/fittrackee/__main__.py index ba56e991..0bc38ed2 100644 --- a/fittrackee/__main__.py +++ b/fittrackee/__main__.py @@ -7,7 +7,6 @@ from typing import Dict, Optional import click import gunicorn.app.base from flask import Flask -from flask_dramatiq import worker from flask_migrate import upgrade from fittrackee import create_app, db @@ -19,7 +18,6 @@ PORT = os.getenv('PORT', '5000') WORKERS = os.getenv('APP_WORKERS', 1) BASEDIR = os.path.abspath(os.path.dirname(__file__)) app = create_app() -dramatiq_worker = worker class StandaloneApplication(gunicorn.app.base.BaseApplication): diff --git a/pyproject.toml b/pyproject.toml index 253113a9..c8617464 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,7 +64,7 @@ Sphinx = "^4.4.0" fittrackee = 'fittrackee.__main__:main' fittrackee_set_admin = 'fittrackee.__main__:set_admin' fittrackee_upgrade_db = 'fittrackee.__main__:upgrade_db' -fittrackee_worker = 'fittrackee.__main__:dramatiq_worker' +fittrackee_worker = 'flask_dramatiq:worker' [tool.black] line-length = 79 From eea3ab5ae767cb1b151f45fed0fb3427eeaf19ac Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 13 Feb 2022 14:31:59 +0100 Subject: [PATCH 2/5] API - check app config before dropping database --- fittrackee/__init__.py | 14 ++++++++++++++ fittrackee/__main__.py | 14 +------------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/fittrackee/__init__.py b/fittrackee/__init__.py index 68743494..61d40a5b 100644 --- a/fittrackee/__init__.py +++ b/fittrackee/__init__.py @@ -1,5 +1,6 @@ import logging import os +import shutil from importlib import import_module, reload from typing import Any @@ -128,4 +129,17 @@ def create_app() -> Flask: else: return render_template('index.html') + @app.cli.command('drop-db') + def drop_db() -> None: + """Empty database and delete uploaded files for dev environments.""" + if app_settings == 'fittrackee.config.ProductionConfig': + print('This is a production server, aborting!') + return + 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.') + return app diff --git a/fittrackee/__main__.py b/fittrackee/__main__.py index 0bc38ed2..2905f0ea 100644 --- a/fittrackee/__main__.py +++ b/fittrackee/__main__.py @@ -1,7 +1,6 @@ # source for StandaloneApplication class: # http://docs.gunicorn.org/en/stable/custom.html import os -import shutil from typing import Dict, Optional import click @@ -9,7 +8,7 @@ import gunicorn.app.base from flask import Flask from flask_migrate import upgrade -from fittrackee import create_app, db +from fittrackee import create_app from fittrackee.users.exceptions import UserNotFoundException from fittrackee.users.utils import set_admin_rights @@ -46,17 +45,6 @@ def upgrade_db() -> None: upgrade(directory=BASEDIR + '/migrations') -@app.cli.command('drop-db') -def drop_db() -> None: - """Empty database and delete uploaded files 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('set-admin') @click.argument('username') def set_admin(username: str) -> None: From e773be0e1a7e0895cde216108c9e6517fd96c4e0 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 13 Feb 2022 14:34:24 +0100 Subject: [PATCH 3/5] API - remove dotenv warning --- .env.docker | 1 + .env.example | 1 + 2 files changed, 2 insertions(+) diff --git a/.env.docker b/.env.docker index afa556dc..e5c1c70c 100644 --- a/.env.docker +++ b/.env.docker @@ -3,6 +3,7 @@ # Application export FLASK_ENV=development export FLASK_APP=fittrackee/__main__.py +export FLASK_SKIP_DOTENV=1 export APP_SETTINGS=fittrackee.config.DevelopmentConfig export APP_SECRET_KEY='just for test' # export APP_WORKERS= diff --git a/.env.example b/.env.example index adc1939f..03c339fb 100644 --- a/.env.example +++ b/.env.example @@ -3,6 +3,7 @@ # Application # export FLASK_APP=fittrackee +export FLASK_SKIP_DOTENV=1 # export HOST= # export PORT= # export APP_SETTINGS=fittrackee.config.ProductionConfig From 18e4ba61fc36b6f47a8ec1399429c6643ad316c2 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 13 Feb 2022 14:36:10 +0100 Subject: [PATCH 4/5] API - move `set-admin` command in users blueprint --- Makefile | 2 +- docker/set-admin.sh | 2 +- fittrackee/__main__.py | 14 -------------- fittrackee/users/users.py | 14 ++++++++++++++ pyproject.toml | 2 +- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index ad1b713d..7e8c8e49 100644 --- a/Makefile +++ b/Makefile @@ -162,7 +162,7 @@ serve-python-dev: $(FLASK) run --with-threads -h $(HOST) -p $(PORT) --cert=adhoc set-admin: - $(FLASK) set-admin $(USERNAME) + $(FLASK) users set-admin $(USERNAME) test-e2e: init-db $(PYTEST) e2e --driver firefox $(PYTEST_ARGS) diff --git a/docker/set-admin.sh b/docker/set-admin.sh index a9b9992e..04f94441 100755 --- a/docker/set-admin.sh +++ b/docker/set-admin.sh @@ -4,4 +4,4 @@ cd /usr/src/app source .env.docker -flask set-admin $1 +flask users set-admin $1 diff --git a/fittrackee/__main__.py b/fittrackee/__main__.py index 2905f0ea..cab00192 100644 --- a/fittrackee/__main__.py +++ b/fittrackee/__main__.py @@ -3,14 +3,11 @@ import os from typing import Dict, Optional -import click import gunicorn.app.base from flask import Flask from flask_migrate import upgrade from fittrackee import create_app -from fittrackee.users.exceptions import UserNotFoundException -from fittrackee.users.utils import set_admin_rights HOST = os.getenv('HOST', '0.0.0.0') PORT = os.getenv('PORT', '5000') @@ -45,17 +42,6 @@ def upgrade_db() -> None: upgrade(directory=BASEDIR + '/migrations') -@app.cli.command('set-admin') -@click.argument('username') -def set_admin(username: str) -> None: - """Set admin rights for given user""" - try: - set_admin_rights(username) - print(f"User '{username}' updated.") - except UserNotFoundException: - print(f"User '{username}' not found.") - - def main() -> None: options = {'bind': f'{HOST}:{PORT}', 'workers': WORKERS} StandaloneApplication(app, options).run() diff --git a/fittrackee/users/users.py b/fittrackee/users/users.py index 8d0089be..9c556830 100644 --- a/fittrackee/users/users.py +++ b/fittrackee/users/users.py @@ -2,6 +2,7 @@ import os import shutil from typing import Any, Dict, Tuple, Union +import click from flask import Blueprint, request, send_file from sqlalchemy import exc @@ -18,13 +19,26 @@ from fittrackee.workouts.models import Record, Workout, WorkoutSegment from fittrackee.workouts.utils_files import get_absolute_file_path from .decorators import authenticate, authenticate_as_admin +from .exceptions import UserNotFoundException from .models import User, UserSportPreference +from .utils import set_admin_rights users_blueprint = Blueprint('users', __name__) USER_PER_PAGE = 10 +@users_blueprint.cli.command('set-admin') +@click.argument('username') +def set_admin(username: str) -> None: + """Set admin rights for given user""" + try: + set_admin_rights(username) + print(f"User '{username}' updated.") + except UserNotFoundException: + print(f"User '{username}' not found.") + + @users_blueprint.route('/users', methods=['GET']) @authenticate def get_users(auth_user: User) -> Dict: diff --git a/pyproject.toml b/pyproject.toml index c8617464..7ed076c1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -62,7 +62,7 @@ Sphinx = "^4.4.0" [tool.poetry.scripts] fittrackee = 'fittrackee.__main__:main' -fittrackee_set_admin = 'fittrackee.__main__:set_admin' +fittrackee_set_admin = 'fittrackee.users.users:set_admin' fittrackee_upgrade_db = 'fittrackee.__main__:upgrade_db' fittrackee_worker = 'flask_dramatiq:worker' From 27d47091b95d002f29a36cdfe202b5619766e6d9 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 13 Feb 2022 15:13:16 +0100 Subject: [PATCH 5/5] API - set autoescape on jinja templates --- fittrackee/emails/email.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/fittrackee/emails/email.py b/fittrackee/emails/email.py index 562fdbc0..df84b069 100644 --- a/fittrackee/emails/email.py +++ b/fittrackee/emails/email.py @@ -6,7 +6,7 @@ from email.mime.text import MIMEText from typing import Dict, Optional, Type, Union from flask import Flask -from jinja2 import Environment, FileSystemLoader +from jinja2 import Environment, FileSystemLoader, select_autoescape from .utils_email import parse_email_url @@ -38,7 +38,10 @@ class EmailMessage: class EmailTemplate: def __init__(self, template_directory: str) -> None: - self._env = Environment(loader=FileSystemLoader(template_directory)) + self._env = Environment( + autoescape=select_autoescape(['html', 'htm', 'xml']), + loader=FileSystemLoader(template_directory), + ) def get_content( self, template_name: str, lang: str, part: str, data: Dict