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:
@@ -89,34 +89,36 @@ def create_app(config_name=None):
|
|||||||
return {'status': 'ok', 'message': 'Trivia Game API is running'}, 200
|
return {'status': 'ok', 'message': 'Trivia Game API is running'}, 200
|
||||||
|
|
||||||
@app.cli.command('transfer-questions')
|
@app.cli.command('transfer-questions')
|
||||||
@click.argument('username')
|
@click.argument('user_id', type=int)
|
||||||
def transfer_questions(username):
|
def transfer_questions(user_id):
|
||||||
"""Transfer ownership of ALL questions to the specified user.
|
"""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
|
from backend.models import User, Question
|
||||||
|
|
||||||
user = User.query.filter_by(preferred_username=username).first()
|
user = User.query.get(user_id)
|
||||||
if not user:
|
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:")
|
click.echo("Available users:")
|
||||||
for u in User.query.all():
|
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)
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
display_name = user.name or user.preferred_username
|
||||||
|
|
||||||
count = Question.query.count()
|
count = Question.query.count()
|
||||||
if count == 0:
|
if count == 0:
|
||||||
click.echo("No questions found in the database.")
|
click.echo("No questions found in the database.")
|
||||||
return
|
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?"):
|
if not click.confirm("Proceed?"):
|
||||||
click.echo("Aborted.")
|
click.echo("Aborted.")
|
||||||
return
|
return
|
||||||
|
|
||||||
Question.query.update({Question.created_by: user.id})
|
Question.query.update({Question.created_by: user.id})
|
||||||
db.session.commit()
|
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
|
return app
|
||||||
|
|||||||
Reference in New Issue
Block a user