61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
from flask import Blueprint, render_template, redirect, url_for
|
|
from flask_login import login_required, current_user
|
|
from app.models.set import Set
|
|
from app.models.instruction import Instruction
|
|
from app.models.site_setting import SiteSetting
|
|
from sqlalchemy import func
|
|
|
|
main_bp = Blueprint('main', __name__)
|
|
|
|
|
|
@main_bp.route('/')
|
|
def index():
|
|
"""Homepage, or straight to the dashboard for logged-in users."""
|
|
if current_user.is_authenticated and \
|
|
SiteSetting.get_bool('home_redirect_dashboard', True):
|
|
return redirect(url_for('main.dashboard'))
|
|
return render_template('index.html')
|
|
|
|
|
|
@main_bp.route('/dashboard')
|
|
@login_required
|
|
def dashboard():
|
|
"""User dashboard with statistics and recent sets."""
|
|
# Everything below is scoped to what this user is allowed to see.
|
|
visible = Set.visible_to(current_user)
|
|
visible_ids = [row.id for row in visible.with_entities(Set.id).all()]
|
|
|
|
total_sets = len(visible_ids)
|
|
total_instructions = Instruction.query.filter(
|
|
Instruction.set_id.in_(visible_ids)).count() if visible_ids else 0
|
|
|
|
if visible_ids:
|
|
theme_stats = db.session.query(
|
|
Set.theme,
|
|
func.count(Set.id).label('count')
|
|
).filter(Set.id.in_(visible_ids)).group_by(Set.theme).order_by(
|
|
func.count(Set.id).desc()).limit(5).all()
|
|
|
|
year_stats = db.session.query(
|
|
Set.year_released,
|
|
func.count(Set.id).label('count')
|
|
).filter(Set.id.in_(visible_ids)).group_by(Set.year_released).order_by(
|
|
Set.year_released.desc()).limit(10).all()
|
|
else:
|
|
theme_stats = []
|
|
year_stats = []
|
|
|
|
recent_sets = Set.visible_to(current_user).order_by(
|
|
Set.created_at.desc()).limit(6).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
|