CLI - keep old commands (add deprecation warning)

This commit is contained in:
Sam 2022-04-24 11:05:01 +02:00
parent 1f098fb2ce
commit a7cf9396d4
3 changed files with 49 additions and 1 deletions

View File

@ -3,14 +3,23 @@
import os import os
from typing import Dict, Optional from typing import Dict, Optional
import click
import gunicorn.app.base import gunicorn.app.base
from flask import Flask from flask import Flask
from flask_migrate import upgrade
from fittrackee import create_app 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') HOST = os.getenv('HOST', '0.0.0.0')
PORT = os.getenv('PORT', '5000') PORT = os.getenv('PORT', '5000')
WORKERS = os.getenv('APP_WORKERS', 1) 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() app = create_app()
@ -35,6 +44,43 @@ class StandaloneApplication(gunicorn.app.base.BaseApplication):
return self.application 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: 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

@ -35,7 +35,7 @@ def manage_user(
reset_password: bool, reset_password: bool,
update_email: Optional[str], update_email: Optional[str],
) -> None: ) -> None:
"""Manage giver user account.""" """Manage given user account."""
with app.app_context(): with app.app_context():
try: try:
user_manager_service = UserManagerService(username) user_manager_service = UserManagerService(username)

View File

@ -65,6 +65,8 @@ Sphinx = "^4.5"
fittrackee = 'fittrackee.__main__:main' fittrackee = 'fittrackee.__main__:main'
fittrackee_worker = 'flask_dramatiq:worker' fittrackee_worker = 'flask_dramatiq:worker'
ftcli = 'fittrackee.cli:cli' ftcli = 'fittrackee.cli:cli'
fittrackee_set_admin = 'fittrackee.__main__:set_admin' # deprecated
fittrackee_upgrade_db = 'fittrackee.__main__:upgrade_db' # deprecated
[tool.black] [tool.black]
line-length = 79 line-length = 79