diff --git a/fittrackee/__main__.py b/fittrackee/__main__.py index 1890c648..82477a56 100644 --- a/fittrackee/__main__.py +++ b/fittrackee/__main__.py @@ -3,14 +3,23 @@ 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.admin import UserManagerService HOST = os.getenv('HOST', '0.0.0.0') PORT = os.getenv('PORT', '5000') WORKERS = os.getenv('APP_WORKERS', 1) +BASEDIR = os.path.abspath(os.path.dirname(__file__)) +WARNING_MESSAGE = ( + "\nThis command is deprecated, it will be removed in a next version.\n" + "Please use ftcli instead.\n" +) app = create_app() @@ -35,6 +44,43 @@ class StandaloneApplication(gunicorn.app.base.BaseApplication): return self.application +# DEPRECATED COMMANDS +@click.group() +def users_cli() -> None: + pass + + +@users_cli.command('set_admin') +@click.argument('username') +def set_admin(username: str) -> None: + """ + [deprecated] Set admin rights for given user. + + It will be removed in a next version. + """ + print(WARNING_MESSAGE) + with app.app_context(): + try: + user_manager_service = UserManagerService(username) + user_manager_service.update( + is_admin=True, + ) + print(f"User '{username}' updated.") + except UserNotFoundException: + print(f"User '{username}' not found.") + + +def upgrade_db() -> None: + """ + [deprecated] Apply migrations. + + It will be removed in a next version. + """ + print(WARNING_MESSAGE) + with app.app_context(): + upgrade(directory=BASEDIR + '/migrations') + + def main() -> None: options = {'bind': f'{HOST}:{PORT}', 'workers': WORKERS} StandaloneApplication(app, options).run() diff --git a/fittrackee/users/commands.py b/fittrackee/users/commands.py index dd8dc4d3..a5adece4 100644 --- a/fittrackee/users/commands.py +++ b/fittrackee/users/commands.py @@ -35,7 +35,7 @@ def manage_user( reset_password: bool, update_email: Optional[str], ) -> None: - """Manage giver user account.""" + """Manage given user account.""" with app.app_context(): try: user_manager_service = UserManagerService(username) diff --git a/pyproject.toml b/pyproject.toml index 25d33b62..7f8a2f04 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -65,6 +65,8 @@ Sphinx = "^4.5" fittrackee = 'fittrackee.__main__:main' fittrackee_worker = 'flask_dramatiq:worker' ftcli = 'fittrackee.cli:cli' +fittrackee_set_admin = 'fittrackee.__main__:set_admin' # deprecated +fittrackee_upgrade_db = 'fittrackee.__main__:upgrade_db' # deprecated [tool.black] line-length = 79