from flask import Blueprint, render_template, redirect, url_for from flask_login import login_required from app.models.set import Set from app.models.instruction import Instruction from sqlalchemy import func main_bp = Blueprint('main', __name__) @main_bp.route('/') def index(): """Homepage.""" return render_template('index.html') @main_bp.route('/dashboard') @login_required def dashboard(): """User dashboard with statistics and recent sets.""" # Get statistics total_sets = Set.query.count() total_instructions = Instruction.query.count() # Get theme statistics theme_stats = db.session.query( Set.theme, func.count(Set.id).label('count') ).group_by(Set.theme).order_by(func.count(Set.id).desc()).limit(5).all() # Get recent sets recent_sets = Set.query.order_by(Set.created_at.desc()).limit(6).all() # Get sets by year year_stats = db.session.query( Set.year_released, func.count(Set.id).label('count') ).group_by(Set.year_released).order_by(Set.year_released.desc()).limit(10).all() return render_template('dashboard.html', total_sets=total_sets, total_instructions=total_instructions, theme_stats=theme_stats, year_stats=year_stats, recent_sets=recent_sets) # Import here to avoid circular imports at module level from app import db