From 0685f3b8ef47648af84cb4d7b612fe6d171d532e Mon Sep 17 00:00:00 2001 From: rene Date: Sat, 30 May 2026 12:55:48 +0200 Subject: [PATCH] Welcome-Dashboard: Tagesfoto wird serverseitig pro Tag gecacht MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- backend/routes/dogs.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) 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(