Welcome-Dashboard: Tagesfoto wird serverseitig pro Tag gecacht

Neue Tabelle daily_photo_cache(dog_id, datum, photo_url) hält die Wahl
fest, sobald irgendein Client (PWA oder iOS) das erste Mal heute fragt.
Alle nachfolgenden Aufrufe liefern dieselbe URL — kein Kippen mehr, wenn
zwischendrin neue Fotos hochgeladen werden und die tick%len-Rotation auf
einen anderen Index zeigt. CREATE TABLE IF NOT EXISTS inline, daher kein
Migrations-Eingriff nötig.
This commit is contained in:
rene 2026-05-30 12:55:48 +02:00
parent 9b0c286aa3
commit 0685f3b8ef

View file

@ -232,15 +232,42 @@ async def get_welcome_dashboard(dog_id: int, user=Depends(get_current_user)):
ORDER BY d.datum DESC, d.id DESC, dm.id ASC""",
(dog_id,)
).fetchall()
# Cross-Plattform-stabiles Tagesfoto: einmal pro Tag pro Hund festgelegt
# und in daily_photo_cache persistiert. Sobald ein Client (PWA oder
# iOS) zum ersten Mal heute zugreift, wird die Wahl gespeichert; alle
# weiteren Clients liefern dasselbe Bild.
import datetime as _dt2
conn.execute("""
CREATE TABLE IF NOT EXISTS daily_photo_cache (
dog_id INTEGER NOT NULL,
datum TEXT NOT NULL,
photo_url TEXT NOT NULL,
PRIMARY KEY (dog_id, datum)
)
""")
today_iso = _dt2.date.today().isoformat()
cached = conn.execute(
"SELECT photo_url FROM daily_photo_cache WHERE dog_id=? AND datum=?",
(dog_id, today_iso)
).fetchone()
random_photo = None
if photos:
import datetime as _dt2
if cached and cached["photo_url"]:
random_photo = {
"url": cached["photo_url"],
"preview_url": preview_url_from(cached["photo_url"]),
}
elif photos:
tick = (_dt2.date.today() - _dt2.date(2024, 1, 1)).days
chosen_url = photos[tick % len(photos)]["url"]
random_photo = {
"url": chosen_url,
"preview_url": preview_url_from(chosen_url),
}
conn.execute(
"INSERT OR IGNORE INTO daily_photo_cache (dog_id, datum, photo_url) VALUES (?, ?, ?)",
(dog_id, today_iso, chosen_url)
)
# Neuester Tagebucheintrag
last_diary_row = conn.execute(