4a79c5cacb
- Added `is_admin` field to User model for role management. - Updated user context middleware to include current user in templates. - Enhanced session management with improved debug middleware. - Refactored dashboard view to fetch user-specific data and recent events. - Improved login and registration templates for better user experience. - Added admin routes with access control for admin users. - Updated Docker configuration for better error logging and dependency management. - Updated requirements to include new dependencies and specify versions.
85 lines
3.1 KiB
Python
85 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
from fastapi import APIRouter, Request, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy.sql import func
|
|
from fastapi.responses import HTMLResponse, RedirectResponse
|
|
|
|
from ..db import get_db
|
|
from ..templates_config import templates
|
|
from ..auth import require_login
|
|
from .. import models
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/")
|
|
@require_login
|
|
def user_dashboard(request: Request, db: Session = Depends(get_db)):
|
|
"""User dashboard showing teams, events and stats"""
|
|
try:
|
|
user = request.session.get("user")
|
|
user_id = user.get("id")
|
|
|
|
# Initialize default values in case of errors
|
|
team_count = 0
|
|
total_points = 0
|
|
event_count = 0
|
|
recent_events = []
|
|
user_teams = []
|
|
|
|
# Check if TeamMember model exists before querying
|
|
if hasattr(models, "TeamMember"):
|
|
# Get the team count for this user
|
|
team_count = db.query(func.count(models.TeamMember.team_id))\
|
|
.filter(models.TeamMember.user_id == user_id)\
|
|
.scalar() or 0
|
|
|
|
# Get user teams
|
|
user_teams = db.query(models.Team)\
|
|
.join(models.TeamMember)\
|
|
.filter(models.TeamMember.user_id == user_id)\
|
|
.all()
|
|
|
|
# Check if UserPoints model exists before querying
|
|
if hasattr(models, "UserPoints"):
|
|
# Get the total points safely
|
|
total_points_result = db.query(func.sum(models.UserPoints.points))\
|
|
.filter(models.UserPoints.user_id == user_id)\
|
|
.first()
|
|
|
|
if total_points_result and total_points_result[0]:
|
|
total_points = total_points_result[0]
|
|
|
|
# Check if EventAttendee model exists before querying
|
|
if hasattr(models, "EventAttendee") and hasattr(models, "Event"):
|
|
# Get event count safely
|
|
event_count_result = db.query(func.count(models.EventAttendee.event_id))\
|
|
.filter(models.EventAttendee.user_id == user_id)\
|
|
.first()
|
|
|
|
if event_count_result and event_count_result[0]:
|
|
event_count = event_count_result[0]
|
|
|
|
# Recent events - only if both models exist
|
|
recent_events = db.query(models.Event)\
|
|
.join(models.EventAttendee)\
|
|
.filter(models.EventAttendee.user_id == user_id)\
|
|
.order_by(models.Event.event_date.desc())\
|
|
.limit(5)\
|
|
.all()
|
|
|
|
return templates.TemplateResponse(
|
|
"dashboard/index.html",
|
|
{
|
|
"request": request,
|
|
"user": user,
|
|
"team_count": team_count,
|
|
"total_points": total_points,
|
|
"event_count": event_count,
|
|
"recent_events": recent_events,
|
|
"user_teams": user_teams
|
|
}
|
|
)
|
|
except Exception as e:
|
|
print(f"Dashboard error: {str(e)}")
|
|
raise HTTPException(status_code=500, detail=f"Dashboard error: {str(e)}")
|