manage.py: separated the user management logic

This commit is contained in:
dogeystamp 2023-03-09 16:39:20 -05:00
parent 00314f155a
commit 8e1b3e4b81
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38
2 changed files with 43 additions and 27 deletions

View File

@ -0,0 +1,33 @@
from sachet.server import app, db
from sachet.server.models import User
def create_user(admin, username, password):
user = User.query.filter_by(username=username).first()
if not user:
user = User(
username=username,
password=password,
admin=admin
)
db.session.add(user)
db.session.commit()
else:
raise KeyError(f"User '{username}' already exists (ID: {user.id})")
def delete_user_by_username(username):
user = User.query.filter_by(username=username).first()
if not user:
raise KeyError(f"User {username} does not exist.")
else:
db.session.delete(user)
db.session.commit()
def delete_user_by_id(id):
user = User.query.filter_by(id=id).first()
if not user:
raise KeyError(f"User with ID {id} does not exist.")
else:
db.session.delete(user)
db.session.commit()

View File

@ -1,6 +1,7 @@
import click import click
from sachet.server import app, db from sachet.server import app, db
from sachet.server.models import User from sachet.server.models import User
from sachet.server.auth import manage
from flask.cli import AppGroup from flask.cli import AppGroup
@ -11,7 +12,6 @@ def create_db():
"""Create all db tables.""" """Create all db tables."""
db.create_all() db.create_all()
@db_cli.command("drop") @db_cli.command("drop")
@click.option('--yes', is_flag=True, expose_value=False, prompt="Are you sure you want to drop all tables?") @click.option('--yes', is_flag=True, expose_value=False, prompt="Are you sure you want to drop all tables?")
def drop_db(): def drop_db():
@ -32,34 +32,17 @@ user_cli = AppGroup("user")
help="Sets the user's password (for security, avoid setting this from the command line).") help="Sets the user's password (for security, avoid setting this from the command line).")
def create_user(admin, username, password): def create_user(admin, username, password):
"""Create a user directly in the database.""" """Create a user directly in the database."""
user = User.query.filter_by(username=username).first() manage.create_user(admin, username, password)
if not user:
user = User(
username=username,
password=password,
admin=admin
)
db.session.add(user)
db.session.commit()
else:
raise Exception(f"User '{username}' already exists (ID: {user.id})")
@user_cli.command("delete") @user_cli.command("delete")
@click.option("--username", "selector", help="Delete by username.", flag_value="username", default=True) @click.option("--username", "selector_type", help="Delete by username.", flag_value="username", default=True)
@click.option("--id", "selector", help="Delete by ID.", flag_value="ID", default=True) @click.option("--id", "selector_type", help="Delete by ID.", flag_value="ID", default=True)
@click.argument("usersel") @click.argument("selector")
@click.option('--yes', is_flag=True, expose_value=False, prompt=f"Are you sure you want to delete this user?") @click.option('--yes', is_flag=True, expose_value=False, prompt=f"Are you sure you want to delete this user?")
def delete_user(selector, usersel): def delete_user(selector_type, selector):
if selector == "username": if selector_type == "username":
user = User.query.filter_by(username=usersel).first() manage.delete_user_by_username(selector)
elif selector == "ID": elif selector_type == "ID":
user = User.query.filter_by(id=usersel).first() manage.delete_user_by_id(selector)
if not user:
raise KeyError(f"User ({selector} {usersel}) does not exist.")
else:
db.session.delete(user)
db.session.commit()
app.cli.add_command(user_cli) app.cli.add_command(user_cli)