feat(ui): add landing page with random public postcards for unauthenticated visitors
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from pathlib import Path
|
||||
from random import shuffle
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import APIRouter, Depends, File, Form, Query, Request, UploadFile
|
||||
@@ -17,12 +18,11 @@ from app.auth import (
|
||||
)
|
||||
from app.config import SECRET_KEY, SESSION_COOKIE_NAME, UPLOAD_DIR
|
||||
from app.database import get_db
|
||||
from datetime import datetime
|
||||
from random import choice
|
||||
from string import ascii_letters, digits
|
||||
|
||||
from app.models import InviteCode, Postcard, Profile, User
|
||||
from app.i18n import t as _t
|
||||
from app.i18n import t as _t, get_languages as _get_languages
|
||||
|
||||
router = APIRouter(tags=["web"])
|
||||
templates = Jinja2Templates(directory=str(Path(__file__).resolve().parent.parent / "templates"))
|
||||
@@ -109,6 +109,48 @@ def _redirect(url: str) -> RedirectResponse:
|
||||
return RedirectResponse(url, status_code=303)
|
||||
|
||||
|
||||
# ---------- Root / Index ----------
|
||||
|
||||
@router.get("/", response_class=HTMLResponse)
|
||||
def index_page(request: Request, db: Session = Depends(get_db)):
|
||||
user = redirect_if_not_logged_in(request, db)
|
||||
if user:
|
||||
return _redirect("/dashboard")
|
||||
|
||||
# Collect all public postcards from all users with showcase enabled
|
||||
all_postcards: list[Postcard] = []
|
||||
users = db.query(User).filter(User.showcase_mode.isnot(None)).all()
|
||||
for u in users:
|
||||
profiles = db.query(Profile).filter(
|
||||
Profile.user_id == u.id, Profile.showcase_enabled == True
|
||||
).all()
|
||||
for p in profiles:
|
||||
cards = db.query(Postcard).filter(
|
||||
Postcard.profile_id == p.id, Postcard.showcase_hidden == False
|
||||
).all()
|
||||
for c in cards:
|
||||
visible = True
|
||||
if c.status in ("sent", "delivered") and p.showcase_visible not in ("sent", "both"):
|
||||
visible = False
|
||||
if c.status == "received" and p.showcase_visible not in ("received", "both"):
|
||||
visible = False
|
||||
if visible:
|
||||
all_postcards.append(c)
|
||||
|
||||
shuffle(all_postcards)
|
||||
|
||||
lang = request.query_params.get("lang")
|
||||
avail = _get_languages()
|
||||
if not lang or lang not in avail:
|
||||
lang = request.cookies.get("showcase_lang")
|
||||
if not lang or lang not in avail:
|
||||
lang = "zh"
|
||||
|
||||
return templates.TemplateResponse("index.html", {
|
||||
"request": request, "postcards": all_postcards, "lang": lang,
|
||||
})
|
||||
|
||||
|
||||
# ---------- Auth ----------
|
||||
|
||||
@router.get("/login", response_class=HTMLResponse)
|
||||
|
||||
Reference in New Issue
Block a user