refactor: extract shared _query_scenarios() to remove duplication

scenarios() and scenario_results() both built the same WHERE clause and
ran the same filtered query. Extracted into _query_scenarios(search,
country, venue_type) -> (rows, total). Each handler is now ~10 lines
of param parsing + render_template.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Deeman
2026-02-24 20:42:52 +01:00
parent ad48f23cfc
commit 83d148477d

View File

@@ -1417,6 +1417,29 @@ SCENARIO_FORM_FIELDS = [
] ]
async def _query_scenarios(search: str, country: str, venue_type: str) -> tuple[list, int]:
"""Execute filtered scenario query. Returns (rows, total_count)."""
wheres = ["1=1"]
params: list = []
if search:
wheres.append("(title LIKE ? OR location LIKE ? OR slug LIKE ?)")
params.extend([f"%{search}%", f"%{search}%", f"%{search}%"])
if country:
wheres.append("country = ?")
params.append(country)
if venue_type:
wheres.append("venue_type = ?")
params.append(venue_type)
where = " AND ".join(wheres)
rows = await fetch_all(
f"SELECT * FROM published_scenarios WHERE {where} ORDER BY created_at DESC LIMIT 500",
tuple(params),
)
total_row = await fetch_one("SELECT COUNT(*) as cnt FROM published_scenarios")
return rows, (total_row["cnt"] if total_row else 0)
@bp.route("/scenarios") @bp.route("/scenarios")
@role_required("admin") @role_required("admin")
async def scenarios(): async def scenarios():
@@ -1425,37 +1448,20 @@ async def scenarios():
country_filter = request.args.get("country", "") country_filter = request.args.get("country", "")
venue_filter = request.args.get("venue_type", "") venue_filter = request.args.get("venue_type", "")
wheres = ["1=1"] scenario_list, total = await _query_scenarios(search, country_filter, venue_filter)
params: list = []
if search:
wheres.append("(title LIKE ? OR location LIKE ? OR slug LIKE ?)")
params.extend([f"%{search}%", f"%{search}%", f"%{search}%"])
if country_filter:
wheres.append("country = ?")
params.append(country_filter)
if venue_filter:
wheres.append("venue_type = ?")
params.append(venue_filter)
where = " AND ".join(wheres)
scenario_list = await fetch_all(
f"SELECT * FROM published_scenarios WHERE {where} ORDER BY created_at DESC LIMIT 500",
tuple(params),
)
countries = await fetch_all( countries = await fetch_all(
"SELECT DISTINCT country FROM published_scenarios WHERE country != '' ORDER BY country" "SELECT DISTINCT country FROM published_scenarios WHERE country != '' ORDER BY country"
) )
venue_types = await fetch_all( venue_types = await fetch_all(
"SELECT DISTINCT venue_type FROM published_scenarios WHERE venue_type != '' ORDER BY venue_type" "SELECT DISTINCT venue_type FROM published_scenarios WHERE venue_type != '' ORDER BY venue_type"
) )
total = await fetch_one("SELECT COUNT(*) as cnt FROM published_scenarios")
return await render_template( return await render_template(
"admin/scenarios.html", "admin/scenarios.html",
scenarios=scenario_list, scenarios=scenario_list,
countries=[r["country"] for r in countries], countries=[r["country"] for r in countries],
venue_types=[r["venue_type"] for r in venue_types], venue_types=[r["venue_type"] for r in venue_types],
total=total["cnt"] if total else 0, total=total,
current_search=search, current_search=search,
current_country=country_filter, current_country=country_filter,
current_venue_type=venue_filter, current_venue_type=venue_filter,
@@ -1471,29 +1477,12 @@ async def scenario_results():
country_filter = request.args.get("country", "") country_filter = request.args.get("country", "")
venue_filter = request.args.get("venue_type", "") venue_filter = request.args.get("venue_type", "")
wheres = ["1=1"] scenario_list, total = await _query_scenarios(search, country_filter, venue_filter)
params: list = []
if search:
wheres.append("(title LIKE ? OR location LIKE ? OR slug LIKE ?)")
params.extend([f"%{search}%", f"%{search}%", f"%{search}%"])
if country_filter:
wheres.append("country = ?")
params.append(country_filter)
if venue_filter:
wheres.append("venue_type = ?")
params.append(venue_filter)
where = " AND ".join(wheres)
scenario_list = await fetch_all(
f"SELECT * FROM published_scenarios WHERE {where} ORDER BY created_at DESC LIMIT 500",
tuple(params),
)
total = await fetch_one("SELECT COUNT(*) as cnt FROM published_scenarios")
return await render_template( return await render_template(
"admin/partials/scenario_results.html", "admin/partials/scenario_results.html",
scenarios=scenario_list, scenarios=scenario_list,
total=total["cnt"] if total else 0, total=total,
is_generating=await _is_generating(), is_generating=await _is_generating(),
) )