Files
game-library/app/web/routes.py
Claude Agent 66ae831c36 Rebuild extraction pipeline infrastructure (Faza 0 prep)
Implements the approved plan to replace the broken regex/index-master
extraction with an LLM-subagent pipeline. Four parallel lanes:

Lane A — scripts/extract_common.py (PDF/docx/doc/pptx/html/zip, no
  max_pages truncation), normalize_sources.py, chunk_sources.py
  (~20pg chunks + overlap, manifest registry), activity_schema.json.
Lane B — app/config_taxonomy.py (16 fixed category slugs), schema
  rebuilt from scratch in app/models/ with content_type, language,
  source_files, source_excerpt, normalized_name, extraction_confidence,
  needs_review; FTS5 + 3 triggers extended with materials_list and
  skills_developed.
Lane C — build_database.py (--rebuild, atomic swap, schema + fuzzy
  source_excerpt validation, dedup with needs_review band),
  validate_extractions.py, review_queue.py, new run_extraction.py
  orchestrator, SUBAGENT_PROMPT.md.
Lane D — search.py content_type/language filters (default search
  excludes non-game content), E7 schema-compat audit; fixed a NULL
  keywords AttributeError in _boost_search_relevance.

Removes 8 orphaned/dead scripts and app/services/parser.py +
indexer.py. Adds tests/ (70 passing, 1 skipped — libreoffice absent).

Note: Lane D made one additive edit to app/models/database.py
(_update_category_counts) to surface content_type/language in
get_filter_options, outside its nominal lane boundary but after
Lane B completed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-19 17:43:38 +00:00

238 lines
8.1 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
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
import os
from pathlib import Path
bp = Blueprint('main', __name__)
# Slug -> Romanian display name. Category and content_type 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, **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('/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