This commit is contained in:
team3
2026-09-09 17:58:52 +02:00
parent 2bd03000e8
commit 847b747870
8 changed files with 419 additions and 55 deletions

View File

@@ -11,6 +11,7 @@ auch wenn der Film später aus dem Katalog fällt.
from datetime import datetime, timezone
from .db import connect
from .users import besitzer, gehoert_mir
SCHEMA = """
CREATE TABLE IF NOT EXISTS watched (
@@ -37,9 +38,23 @@ CREATE TABLE IF NOT EXISTS ratings (
MAX_STERNE = 5
def _spalte_user_id(conn, tabelle):
"""Nachrüsten und aus der Sitzung füllen — einmalig je Datenbank."""
spalten = {r[1] for r in conn.execute(f"PRAGMA table_info({tabelle})")}
if "user_id" in spalten:
return
conn.execute(f"ALTER TABLE {tabelle} ADD COLUMN user_id INTEGER")
conn.execute(
f"UPDATE {tabelle} SET user_id = ("
" SELECT user_id FROM user_sessions s WHERE s.session_id = "
f" {tabelle}.session_id)"
)
def init():
with connect() as conn:
conn.executescript(SCHEMA)
_spalte_user_id(conn, "ratings")
# Die Tabelle stand vor der Spalte schon in bestehenden Datenbanken
spalten = {r["name"] for r in conn.execute("PRAGMA table_info(watched)")}
if "hidden" not in spalten:
@@ -59,10 +74,11 @@ def alle(session_id):
"SELECT * FROM watched ORDER BY watched_at DESC, id DESC"
).fetchall()
stimmen = conn.execute(
"SELECT id, watched_id, session_id, stars, name FROM ratings "
"SELECT id, watched_id, session_id, user_id, stars, name FROM ratings "
"ORDER BY created_at ASC"
).fetchall()
ich = besitzer(session_id)
je_film = {}
for s in stimmen:
je_film.setdefault(s["watched_id"], []).append(s)
@@ -81,7 +97,11 @@ def alle(session_id):
"average": round(sum(werte) / len(werte), 1) if werte else None,
"count": len(werte),
"mine": next(
(s["stars"] for s in eigene if s["session_id"] == session_id),
(
s["stars"]
for s in eigene
if gehoert_mir(s, session_id, ich)
),
None,
),
}
@@ -145,17 +165,25 @@ def bewerten(watched_id, session_id, stars, name=None):
).fetchone()
if not da:
return None
# Eine Stimme je Sitzung — eine neue Bewertung ersetzt die alte
# Eine Stimme je Person — die eigene alte fliegt vorher raus.
# Mit Namen zählt der Name, ohne Namen die Sitzung.
ich = besitzer(session_id)
conn.execute(
"DELETE FROM ratings WHERE watched_id = ? AND "
"(user_id = ? OR (? IS NULL AND user_id IS NULL AND session_id = ?))",
(watched_id, ich, ich, session_id),
)
conn.execute(
"INSERT INTO ratings "
"(watched_id, session_id, stars, name, created_at) "
"VALUES (?, ?, ?, ?, ?) "
"(watched_id, session_id, user_id, stars, name, created_at) "
"VALUES (?, ?, ?, ?, ?, ?) "
"ON CONFLICT (watched_id, session_id) DO UPDATE SET "
"stars = excluded.stars, name = excluded.name, "
"created_at = excluded.created_at",
"user_id = excluded.user_id, stars = excluded.stars, "
"name = excluded.name, created_at = excluded.created_at",
(
watched_id,
session_id,
ich,
stars,
(name or "").strip()[:40] or None,
_jetzt(),