diff --git a/backend/routes/dogs.py b/backend/routes/dogs.py index 1c37b99..9c414d8 100644 --- a/backend/routes/dogs.py +++ b/backend/routes/dogs.py @@ -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(