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:
parent
9b0c286aa3
commit
0685f3b8ef
1 changed files with 29 additions and 2 deletions
|
|
@ -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""",
|
ORDER BY d.datum DESC, d.id DESC, dm.id ASC""",
|
||||||
(dog_id,)
|
(dog_id,)
|
||||||
).fetchall()
|
).fetchall()
|
||||||
random_photo = None
|
# Cross-Plattform-stabiles Tagesfoto: einmal pro Tag pro Hund festgelegt
|
||||||
if photos:
|
# 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
|
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 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
|
tick = (_dt2.date.today() - _dt2.date(2024, 1, 1)).days
|
||||||
chosen_url = photos[tick % len(photos)]["url"]
|
chosen_url = photos[tick % len(photos)]["url"]
|
||||||
random_photo = {
|
random_photo = {
|
||||||
"url": chosen_url,
|
"url": chosen_url,
|
||||||
"preview_url": preview_url_from(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
|
# Neuester Tagebucheintrag
|
||||||
last_diary_row = conn.execute(
|
last_diary_row = conn.execute(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue