Update transfer-questions CLI to accept user ID instead of username

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-03 10:30:56 -04:00
parent d73cf5545c
commit 406daeeab1

View File

@@ -89,34 +89,36 @@ def create_app(config_name=None):
return {'status': 'ok', 'message': 'Trivia Game API is running'}, 200
@app.cli.command('transfer-questions')
@click.argument('username')
def transfer_questions(username):
@click.argument('user_id', type=int)
def transfer_questions(user_id):
"""Transfer ownership of ALL questions to the specified user.
USERNAME is the preferred_username of the target user.
USER_ID is the database ID of the target user.
"""
from backend.models import User, Question
user = User.query.filter_by(preferred_username=username).first()
user = User.query.get(user_id)
if not user:
click.echo(f"Error: No user found with username '{username}'")
click.echo(f"Error: No user found with id {user_id}")
click.echo("Available users:")
for u in User.query.all():
click.echo(f" - {u.preferred_username} (id={u.id}, name={u.name})")
click.echo(f" - id={u.id}, name={u.name or u.preferred_username}")
raise SystemExit(1)
display_name = user.name or user.preferred_username
count = Question.query.count()
if count == 0:
click.echo("No questions found in the database.")
return
click.echo(f"This will transfer ownership of {count} question(s) to '{username}' (id={user.id}).")
click.echo(f"This will transfer ownership of {count} question(s) to '{display_name}' (id={user.id}).")
if not click.confirm("Proceed?"):
click.echo("Aborted.")
return
Question.query.update({Question.created_by: user.id})
db.session.commit()
click.echo(f"Successfully transferred {count} question(s) to '{username}'.")
click.echo(f"Successfully transferred {count} question(s) to '{display_name}'.")
return app