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
+32
-20
+32
-20
@@ -1,7 +1,8 @@
|
|||||||
from flask import Blueprint, render_template, redirect, url_for
|
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.set import Set
|
||||||
from app.models.instruction import Instruction
|
from app.models.instruction import Instruction
|
||||||
|
from app.models.site_setting import SiteSetting
|
||||||
from sqlalchemy import func
|
from sqlalchemy import func
|
||||||
|
|
||||||
main_bp = Blueprint('main', __name__)
|
main_bp = Blueprint('main', __name__)
|
||||||
@@ -9,7 +10,10 @@ main_bp = Blueprint('main', __name__)
|
|||||||
|
|
||||||
@main_bp.route('/')
|
@main_bp.route('/')
|
||||||
def index():
|
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')
|
return render_template('index.html')
|
||||||
|
|
||||||
|
|
||||||
@@ -17,24 +21,32 @@ def index():
|
|||||||
@login_required
|
@login_required
|
||||||
def dashboard():
|
def dashboard():
|
||||||
"""User dashboard with statistics and recent sets."""
|
"""User dashboard with statistics and recent sets."""
|
||||||
# Get statistics
|
# Everything below is scoped to what this user is allowed to see.
|
||||||
total_sets = Set.query.count()
|
visible = Set.visible_to(current_user)
|
||||||
total_instructions = Instruction.query.count()
|
visible_ids = [row.id for row in visible.with_entities(Set.id).all()]
|
||||||
|
|
||||||
# Get theme statistics
|
total_sets = len(visible_ids)
|
||||||
theme_stats = db.session.query(
|
total_instructions = Instruction.query.filter(
|
||||||
Set.theme,
|
Instruction.set_id.in_(visible_ids)).count() if visible_ids else 0
|
||||||
func.count(Set.id).label('count')
|
|
||||||
).group_by(Set.theme).order_by(func.count(Set.id).desc()).limit(5).all()
|
if visible_ids:
|
||||||
|
theme_stats = db.session.query(
|
||||||
# Get recent sets
|
Set.theme,
|
||||||
recent_sets = Set.query.order_by(Set.created_at.desc()).limit(6).all()
|
func.count(Set.id).label('count')
|
||||||
|
).filter(Set.id.in_(visible_ids)).group_by(Set.theme).order_by(
|
||||||
# Get sets by year
|
func.count(Set.id).desc()).limit(5).all()
|
||||||
year_stats = db.session.query(
|
|
||||||
Set.year_released,
|
year_stats = db.session.query(
|
||||||
func.count(Set.id).label('count')
|
Set.year_released,
|
||||||
).group_by(Set.year_released).order_by(Set.year_released.desc()).limit(10).all()
|
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',
|
return render_template('dashboard.html',
|
||||||
total_sets=total_sets,
|
total_sets=total_sets,
|
||||||
|
|||||||
Reference in new issue
Block a user