Redirect logged-in users to dashboard; scope dashboard stats to visible sets
This commit is contained in:
1 parent
dcd205fc6a
commit
aafdaa5045
1 file changed
+24
-12
+24
-12
@@ -1,7 +1,8 @@
|
||||
from flask import Blueprint, render_template, redirect, url_for
|
||||
from flask_login import login_required
|
||||
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__)
|
||||
@@ -9,7 +10,10 @@ main_bp = Blueprint('main', __name__)
|
||||
|
||||
@main_bp.route('/')
|
||||
def index():
|
||||
"""Homepage."""
|
||||
"""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')
|
||||
|
||||
|
||||
@@ -17,24 +21,32 @@ def index():
|
||||
@login_required
|
||||
def dashboard():
|
||||
"""User dashboard with statistics and recent sets."""
|
||||
# Get statistics
|
||||
total_sets = Set.query.count()
|
||||
total_instructions = Instruction.query.count()
|
||||
# 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()]
|
||||
|
||||
# Get theme statistics
|
||||
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')
|
||||
).group_by(Set.theme).order_by(func.count(Set.id).desc()).limit(5).all()
|
||||
).filter(Set.id.in_(visible_ids)).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()
|
||||
).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,
|
||||
|
||||
Reference in new issue
Block a user