This commit is contained in:
team3
2026-09-09 18:15:10 +02:00
parent 847b747870
commit 3dc091d04a
8 changed files with 103 additions and 155 deletions

View File

@@ -10,8 +10,9 @@ auch wenn der Film später aus dem Katalog fällt.
from datetime import datetime, timezone
from . import users
from .db import connect
from .users import besitzer, gehoert_mir
from .users import WEM, besitzer, gehoert_mir
SCHEMA = """
CREATE TABLE IF NOT EXISTS watched (
@@ -23,38 +24,28 @@ CREATE TABLE IF NOT EXISTS watched (
watched_at TEXT NOT NULL,
hidden INTEGER NOT NULL DEFAULT 0
);
"""
RATINGS = """
CREATE TABLE IF NOT EXISTS ratings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
watched_id INTEGER NOT NULL,
session_id TEXT NOT NULL,
user_id INTEGER,
stars INTEGER NOT NULL,
name TEXT,
created_at TEXT NOT NULL,
UNIQUE (watched_id, session_id)
created_at TEXT NOT NULL
);
"""
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")
conn.executescript(SCHEMA + RATINGS)
# Eine Stimme je Person und Film
users.je_person(conn, "ratings", RATINGS, "watched_id")
# 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:
@@ -165,21 +156,16 @@ def bewerten(watched_id, session_id, stars, name=None):
).fetchone()
if not da:
return None
# Eine Stimme je Person — die eigene alte fliegt vorher raus.
# Mit Namen zählt der Name, ohne Namen die Sitzung.
# Eine Stimme je Person — die eigene alte fliegt vorher raus
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 = ?))",
f"DELETE FROM ratings WHERE watched_id = ? AND {WEM}",
(watched_id, ich, ich, session_id),
)
conn.execute(
"INSERT INTO ratings "
"(watched_id, session_id, user_id, stars, name, created_at) "
"VALUES (?, ?, ?, ?, ?, ?) "
"ON CONFLICT (watched_id, session_id) DO UPDATE SET "
"user_id = excluded.user_id, stars = excluded.stars, "
"name = excluded.name, created_at = excluded.created_at",
"VALUES (?, ?, ?, ?, ?, ?)",
(
watched_id,
session_id,