feat(postcard): split create into sent/received, add pending status and mark-sent flow

This commit is contained in:
2026-07-15 19:04:54 +08:00
parent d79aaea4ae
commit de29f5445e
7 changed files with 172 additions and 91 deletions

View File

@@ -131,7 +131,7 @@ def index_page(request: Request, db: Session = Depends(get_db)):
).all()
for c in cards:
visible = True
if c.status in ("sent", "delivered") and p.showcase_visible not in ("sent", "both"):
if c.status in ("pending", "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
@@ -173,7 +173,7 @@ def api_public_cards(
).all()
for c in cards:
vis = True
if c.status in ("sent", "delivered") and p.showcase_visible not in ("sent", "both"):
if c.status in ("pending", "sent", "delivered") and p.showcase_visible not in ("sent", "both"):
vis = False
if c.status == "received" and p.showcase_visible not in ("received", "both"):
vis = False
@@ -218,7 +218,7 @@ def api_public_cards_user(
Postcard.image_front.isnot(None), Postcard.image_front != ""
).all()
if tab == "sent" and p.showcase_visible in ("sent", "both"):
all_cards.extend([c for c in cards if c.status in ("sent", "delivered")])
all_cards.extend([c for c in cards if c.status in ("pending", "sent", "delivered")])
elif tab == "received" and p.showcase_visible in ("received", "both"):
all_cards.extend([c for c in cards if c.status == "received"])
all_cards.sort(key=lambda c: c.send_time or c.created_at, reverse=True)
@@ -613,7 +613,7 @@ def dashboard(request: Request, user: User = Depends(get_current_user_web), db:
for c in all_cards:
if not c.country_to:
continue
if c.status in ("sent", "delivered"):
if c.status in ("pending", "sent", "delivered"):
sent_country_counts[c.country_to] = sent_country_counts.get(c.country_to, 0) + 1
elif c.status == "received":
received_country_counts[c.country_to] = received_country_counts.get(c.country_to, 0) + 1
@@ -712,7 +712,7 @@ def postcard_list(
postcards.sort(key=lambda pc: (_status_order.get(pc.status, 9), -(pc.send_time or pc.arrival_time or pc.receive_time or pc.created_at).timestamp()))
# split into sent and received
sent = [pc for pc in postcards if pc.status in ("sent", "delivered")]
sent = [pc for pc in postcards if pc.status in ("pending", "sent", "delivered")]
received = [pc for pc in postcards if pc.status == "received"]
return templates.TemplateResponse(
@@ -722,11 +722,12 @@ def postcard_list(
@router.get("/profiles/{profile_id}/postcards/new", response_class=HTMLResponse)
def postcard_new(request: Request, profile_id: int, user: User = Depends(get_current_user_web), db: Session = Depends(get_db)):
def postcard_new(request: Request, profile_id: int, create_type: str = Query("sent"), user: User = Depends(get_current_user_web), db: Session = Depends(get_db)):
profile = db.query(Profile).filter(Profile.id == profile_id, Profile.user_id == user.id).first()
if not profile:
return _redirect("/profiles")
return templates.TemplateResponse("postcard_form.html", {"request": request, "user": user, "profile": profile, "postcard": None})
today = datetime.now().strftime("%Y-%m-%d")
return templates.TemplateResponse("postcard_form.html", {"request": request, "user": user, "profile": profile, "postcard": None, "create_type": create_type, "today": today})
@router.post("/profiles/{profile_id}/postcards")
@@ -916,6 +917,28 @@ def mark_delivered(
return _redirect(f"/postcards/{postcard_id}")
@router.post("/postcards/{postcard_id}/mark-sent")
def mark_sent(
postcard_id: int,
user: User = Depends(get_current_user_web),
db: Session = Depends(get_db),
):
"""Transition pending → sent, auto-fill send_time if empty."""
pc = (
db.query(Postcard)
.join(Profile)
.filter(Postcard.id == postcard_id, Profile.user_id == user.id)
.first()
)
if not pc:
return _redirect("/profiles")
pc.status = "sent"
if not pc.send_time:
pc.send_time = datetime.now()
db.commit()
return _redirect(f"/postcards/{postcard_id}")
# ---------- Image Upload ----------
@router.post("/postcards/{postcard_id}/image/{side}")
@@ -1028,11 +1051,11 @@ def public_showcase(request: Request, username: str, db: Session = Depends(get_d
Postcard.image_front.isnot(None), Postcard.image_front != ""
).all()
if p.showcase_visible in ("sent", "both"):
sent_all.extend([c for c in cards if c.status in ("sent", "delivered")])
sent_all.extend([c for c in cards if c.status in ("pending", "sent", "delivered")])
if p.showcase_visible in ("received", "both"):
received_all.extend([c for c in cards if c.status == "received"])
if p.showcase_visible in ("sent", "both"):
p_sent = [c for c in cards if c.status in ("sent", "delivered")]
p_sent = [c for c in cards if c.status in ("pending", "sent", "delivered")]
else:
p_sent = []
if p.showcase_visible in ("received", "both"):