feat(ui): redesign dashboard as global overview with stats, progress bar, recent activity
This commit is contained in:
@@ -148,20 +148,35 @@ def root(user: User = Depends(get_current_user_web)):
|
||||
@router.get("/dashboard", response_class=HTMLResponse)
|
||||
def dashboard(request: Request, user: User = Depends(get_current_user_web), db: Session = Depends(get_db)):
|
||||
profiles = db.query(Profile).filter(Profile.user_id == user.id).all()
|
||||
stats = {}
|
||||
extras = {}
|
||||
all_cards = []
|
||||
for p in profiles:
|
||||
cards = db.query(Postcard).filter(Postcard.profile_id == p.id).order_by(Postcard.created_at.desc()).all()
|
||||
stats[p.id] = {
|
||||
"total": len(cards),
|
||||
"sent": sum(1 for c in cards if c.status == "sent"),
|
||||
"delivered": sum(1 for c in cards if c.status == "delivered"),
|
||||
"received": sum(1 for c in cards if c.status == "received"),
|
||||
}
|
||||
countries = sorted({c.country for c in cards if c.country})
|
||||
recent = cards[:3]
|
||||
extras[p.id] = {"countries": countries, "recent": recent}
|
||||
return templates.TemplateResponse("dashboard.html", {"request": request, "user": user, "profiles": profiles, "stats": stats, "extras": extras})
|
||||
cards = db.query(Postcard).filter(Postcard.profile_id == p.id).all()
|
||||
all_cards.extend(cards)
|
||||
|
||||
total = len(all_cards)
|
||||
sent = sum(1 for c in all_cards if c.status == "sent")
|
||||
delivered = sum(1 for c in all_cards if c.status == "delivered")
|
||||
received = sum(1 for c in all_cards if c.status == "received")
|
||||
countries = len({c.country for c in all_cards if c.country})
|
||||
|
||||
# recent postcards across all profiles (last 8)
|
||||
all_cards.sort(key=lambda c: c.created_at, reverse=True)
|
||||
recent = all_cards[:8]
|
||||
|
||||
# status distribution for progress bar
|
||||
if total > 0:
|
||||
sent_pct = round(sent / total * 100)
|
||||
delivered_pct = round(delivered / total * 100)
|
||||
received_pct = round(received / total * 100)
|
||||
else:
|
||||
sent_pct = delivered_pct = received_pct = 0
|
||||
|
||||
return templates.TemplateResponse("dashboard.html", {
|
||||
"request": request, "user": user, "profiles": profiles,
|
||||
"total": total, "sent": sent, "delivered": delivered, "received": received,
|
||||
"countries": countries, "recent": recent,
|
||||
"sent_pct": sent_pct, "delivered_pct": delivered_pct, "received_pct": received_pct,
|
||||
})
|
||||
|
||||
|
||||
# ---------- Profiles ----------
|
||||
|
||||
Reference in New Issue
Block a user