Files
game-library/app/web/routes.py
Claude Agent bcfb6841eb Faza 1 complete: bilingual+enrichment plumbing, UI/filters, frozen DB
Extraction finished (575/588 chunks; 6 content-filter-blocked, 7 await
re-extraction). DB rebuilt and frozen at 9418 activities — content_keys
are now stable for the enrichment overlay.

Part A (plumbing + UI):
- database.py: name_ro/description_ro/rules_ro/variations_ro, indoor_outdoor,
  space_needed, estimated_fields, source_id/source_ids/chunk_key columns;
  FTS5 indexes the 4 *_ro columns across CREATE + all 3 triggers; new equality
  filters + category counts for both axes.
- activity.py: new fields + bilingual display helpers (get_display_*,
  is_estimated, axis displays).
- config_taxonomy.py: INDOOR_OUTDOOR/SPACE_NEEDED enums + normalizers
  (None on unrecognised, no fabrication).
- search.py / routes.py / config.py / templates / css: new dropdowns,
  RO-primary rendering with "(estimat)" markers and collapsible original
  text, and a /source/<id> download route shipped DARK behind
  SOURCE_DOWNLOAD_ENABLED (copyright opt-in).
- build_database.py: source_id/chunk_key in dict_to_activity; merge_cluster
  unions source_ids without touching enrichment fields.

Part B (enrichment pipeline, built not yet run):
- build_database.py: load_enrichment + apply_enrichment (post-dedup, keyed on
  content_key) + --enrichment CLI + stated-vs-estimated QA.
- run_enrichment.py (resumable, --source/--limit pilot scoping, --collect),
  ENRICHMENT_PROMPT.md.

Repair: scripts/repair_extractions.py fixes the subagents' systematic
unescaped-ASCII-quote bug with a faithful char-scanner (escapes, never
truncates) + schema validation + a strictly-more-text guard. json_repair was
tried first, truncated silently, and is NOT used. build_database has no repair
dependency.

Tests: tests/test_enrichment.py added; 99 pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-29 18:10:13 +00:00

283 lines
9.8 KiB
Python

"""
Flask routes for INDEX-SISTEM-JOCURI v2.0
Clean, minimalist web interface with dynamic filters
"""
from flask import (
Blueprint, request, render_template, jsonify, current_app,
send_from_directory,
)
from app.models.database import DatabaseManager
from app.models.activity import Activity
from app.services.search import SearchService
from app.config_taxonomy import (
CATEGORIES, CONTENT_TYPES, INDOOR_OUTDOOR, SPACE_NEEDED,
)
bp = Blueprint('main', __name__)
# Slug -> Romanian display name. Category, content_type, indoor_outdoor and
# space_needed slugs never collide, so a single flat map is enough for the UI
# filter labels.
LANGUAGE_NAMES = {'ro': 'Română', 'en': 'Engleză'}
DISPLAY_NAMES = {
**CATEGORIES, **CONTENT_TYPES, **INDOOR_OUTDOOR, **SPACE_NEEDED,
**LANGUAGE_NAMES,
}
# Initialize database manager (will be configured in application factory)
def get_db_manager():
"""Get database manager instance"""
db_path = current_app.config.get('DATABASE_URL', 'sqlite:///data/activities.db')
if db_path.startswith('sqlite:///'):
db_path = db_path[10:]
return DatabaseManager(db_path)
def get_search_service():
"""Get search service instance"""
return SearchService(get_db_manager())
@bp.route('/')
def index():
"""Main search page with dynamic filters"""
try:
db = get_db_manager()
# Get dynamic filter options from database
filter_options = db.get_filter_options()
# Get database statistics for the interface
stats = db.get_statistics()
return render_template('index.html',
filters=filter_options,
display_names=DISPLAY_NAMES,
stats=stats)
except Exception as e:
print(f"Error loading main page: {e}")
# Fallback with empty filters
return render_template('index.html',
filters={},
display_names=DISPLAY_NAMES,
stats={'total_activities': 0})
@bp.route('/search', methods=['GET', 'POST'])
def search():
"""Search activities with filters"""
try:
search_service = get_search_service()
# Get search parameters
if request.method == 'POST':
search_query = request.form.get('search_query', '').strip()
filters = {k: v for k, v in request.form.items()
if k != 'search_query' and v and v.strip()}
else:
search_query = request.args.get('q', '').strip()
filters = {k: v for k, v in request.args.items()
if k != 'q' and v and v.strip()}
# Perform search
results = search_service.search_activities(
search_text=search_query if search_query else None,
filters=filters,
limit=current_app.config.get('SEARCH_RESULTS_LIMIT', 100)
)
# Convert results to Activity objects for better template handling
activities = [Activity.from_dict(result) for result in results]
# Get filter options for the form
db = get_db_manager()
filter_options = db.get_filter_options()
return render_template('results.html',
activities=activities,
search_query=search_query,
applied_filters=filters,
filters=filter_options,
display_names=DISPLAY_NAMES,
results_count=len(activities))
except Exception as e:
print(f"Search error: {e}")
return render_template('results.html',
activities=[],
search_query='',
applied_filters={},
filters={},
display_names=DISPLAY_NAMES,
results_count=0,
error=str(e))
@bp.route('/activity/<int:activity_id>')
def activity_detail(activity_id):
"""Show detailed activity information"""
try:
db = get_db_manager()
# Get activity
activity_data = db.get_activity_by_id(activity_id)
if not activity_data:
return render_template('404.html'), 404
activity = Activity.from_dict(activity_data)
# Get similar activities (same category)
similar_results = db.search_activities(
category=activity.category,
limit=5
)
# Filter out current activity and convert to Activity objects
similar_activities = [
Activity.from_dict(result) for result in similar_results
if result['id'] != activity_id
][:3] # Limit to 3 recommendations
return render_template('activity.html',
activity=activity,
display_names=DISPLAY_NAMES,
similar_activities=similar_activities)
except Exception as e:
print(f"Error loading activity {activity_id}: {e}")
return render_template('404.html'), 404
@bp.route('/source/<int:activity_id>')
def source_download(activity_id):
"""Download the original source file for an activity (plan A6).
Shipped DARK: returns 404 unless SOURCE_DOWNLOAD_ENABLED is set (copyright
exposure — the user opts in). Resolves the activity's `source_file` under
CORPUS_DIR. send_from_directory does the safe-join and blocks traversal;
web-mirror / extension-less sources that are not real files 404 gracefully.
"""
if not current_app.config.get('SOURCE_DOWNLOAD_ENABLED', False):
return render_template('404.html'), 404
try:
db = get_db_manager()
activity_data = db.get_activity_by_id(activity_id)
if not activity_data:
return render_template('404.html'), 404
source_file = (activity_data.get('source_file') or '').strip()
if not source_file:
return render_template('404.html'), 404
corpus_dir = current_app.config.get('CORPUS_DIR')
if not corpus_dir:
return render_template('404.html'), 404
try:
# send_from_directory rejects path traversal and missing files with
# a 404 (NotFound) — no manual safe_join needed.
return send_from_directory(
corpus_dir, source_file, as_attachment=True
)
except Exception:
# Missing file / web-mirror source with no on-disk original.
return render_template('404.html'), 404
except Exception as e:
print(f"Source download error for {activity_id}: {e}")
return render_template('404.html'), 404
@bp.route('/health')
def health_check():
"""Health check endpoint for Docker"""
try:
db = get_db_manager()
stats = db.get_statistics()
return jsonify({
'status': 'healthy',
'database': 'connected',
'activities_count': stats.get('total_activities', 0),
'timestamp': stats.get('timestamp', 'unknown')
})
except Exception as e:
return jsonify({
'status': 'unhealthy',
'error': str(e)
}), 500
@bp.route('/api/statistics')
def api_statistics():
"""API endpoint for database statistics"""
try:
db = get_db_manager()
stats = db.get_statistics()
return jsonify(stats)
except Exception as e:
return jsonify({'error': str(e)}), 500
@bp.route('/api/filters')
def api_filters():
"""API endpoint for dynamic filter options"""
try:
db = get_db_manager()
filters = db.get_filter_options()
return jsonify(filters)
except Exception as e:
return jsonify({'error': str(e)}), 500
@bp.route('/api/search')
def api_search():
"""JSON API for search (for AJAX requests)"""
try:
search_service = get_search_service()
# Get search parameters from query string
search_query = request.args.get('q', '').strip()
filters = {k: v for k, v in request.args.items()
if k not in ['q', 'limit', 'format'] and v and v.strip()}
limit = min(int(request.args.get('limit', 50)), 100) # Max 100 results
# Perform search
results = search_service.search_activities(
search_text=search_query if search_query else None,
filters=filters,
limit=limit
)
# Format results for JSON response
formatted_results = []
for result in results:
activity = Activity.from_dict(result)
formatted_results.append({
'id': activity.id,
'name': activity.name,
'description': activity.description[:200] + '...' if len(activity.description) > 200 else activity.description,
'category': activity.category,
'age_range': activity.get_age_range_display(),
'participants': activity.get_participants_display(),
'duration': activity.get_duration_display(),
'materials': activity.get_materials_display(),
'source_file': activity.source_file,
'url': f'/activity/{activity.id}'
})
return jsonify({
'results': formatted_results,
'count': len(formatted_results),
'query': search_query,
'filters': filters
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@bp.errorhandler(404)
def not_found(error):
"""404 error handler"""
return render_template('404.html'), 404
@bp.errorhandler(500)
def internal_error(error):
"""500 error handler"""
return render_template('500.html'), 500