Add transfer-questions CLI command for bulk ownership reassignment
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
import click
|
||||||
from flask import Flask, send_from_directory
|
from flask import Flask, send_from_directory
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
from flask_migrate import Migrate
|
from flask_migrate import Migrate
|
||||||
@@ -87,4 +88,35 @@ def create_app(config_name=None):
|
|||||||
"""Health check endpoint"""
|
"""Health check endpoint"""
|
||||||
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')
|
||||||
|
@click.argument('username')
|
||||||
|
def transfer_questions(username):
|
||||||
|
"""Transfer ownership of ALL questions to the specified user.
|
||||||
|
|
||||||
|
USERNAME is the preferred_username of the target user.
|
||||||
|
"""
|
||||||
|
from backend.models import User, Question
|
||||||
|
|
||||||
|
user = User.query.filter_by(preferred_username=username).first()
|
||||||
|
if not user:
|
||||||
|
click.echo(f"Error: No user found with username '{username}'")
|
||||||
|
click.echo("Available users:")
|
||||||
|
for u in User.query.all():
|
||||||
|
click.echo(f" - {u.preferred_username} (id={u.id}, name={u.name})")
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
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}).")
|
||||||
|
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}'.")
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|||||||
Reference in New Issue
Block a user