"""Durable local customer reviews for the Amazon clone. This module is deliberately independent from :mod:`store` so its schema can be installed into an existing clone database without rewriting the core schema. Call :func:`install_schema` once during database startup, after ``schema.sql`` has created ``accounts``, ``browser_sessions``, ``catalog_products``, ``commerce_offers``, ``orders``, and ``order_items``. Request handlers must pass the server-side session digest, never an account id from a form. Review ownership is resolved through ``browser_sessions`` and ``verified_purchase`` is derived at read time from placed orders. Consequently neither value can be asserted by a client. For a full benchmark reset, call :func:`reset_review_data` before clearing the core tables. Deleting ``accounts`` with foreign keys enabled also cascades all reviews and votes, but the explicit reset is clearer and remains safe if the core reset order changes. Schema installation is idempotent and refuses an unknown review-schema version instead of destructively guessing a migration. """ from __future__ import annotations import re import sqlite3 from datetime import datetime, timezone from typing import Any LOCAL_REVIEW_PROVENANCE = "local_user_review" REVIEW_SCHEMA_VERSION = 1 MAX_HEADLINE_LENGTH = 200 MAX_BODY_LENGTH = 10_000 REVIEW_SORT_RECENT = "recent" REVIEW_SORT_HELPFUL = "helpful" REVIEW_SORTS = frozenset({REVIEW_SORT_RECENT, REVIEW_SORT_HELPFUL}) REVIEW_PRODUCT_SCOPES = frozenset( {"catalog_product", "commerce_offer", "home_snapshot"} ) _ASIN_PATTERN = re.compile(r"^[A-Z0-9]{10}$") class ReviewStoreError(ValueError): """Base class for expected review-domain failures.""" class ReviewValidationError(ReviewStoreError): """A submitted review field or query option is invalid.""" class ReviewAuthenticationRequired(ReviewStoreError): """The browser session exists but is not signed in.""" class ReviewNotFound(ReviewStoreError): """A referenced session, product, or review does not exist.""" class ReviewPermissionDenied(ReviewStoreError): """The current actor is not allowed to perform the operation.""" class ReviewSchemaError(RuntimeError): """The installed review schema is incompatible with this module.""" _BASE_SCHEMA = """ CREATE TABLE IF NOT EXISTS local_review_schema_meta ( singleton INTEGER PRIMARY KEY CHECK (singleton = 1), schema_version INTEGER NOT NULL CHECK (schema_version > 0) ); INSERT OR IGNORE INTO local_review_schema_meta(singleton, schema_version) VALUES (1, 1); CREATE TABLE IF NOT EXISTS product_reviews ( review_id INTEGER PRIMARY KEY AUTOINCREMENT, account_id INTEGER NOT NULL REFERENCES accounts(account_id) ON DELETE CASCADE, asin TEXT NOT NULL COLLATE NOCASE, rating INTEGER NOT NULL CHECK (rating BETWEEN 1 AND 5), headline TEXT NOT NULL CHECK ( length(trim(headline)) BETWEEN 1 AND 200 ), body TEXT NOT NULL CHECK ( length(trim(body)) BETWEEN 1 AND 10000 ), created_at TEXT NOT NULL, updated_at TEXT NOT NULL, UNIQUE (account_id, asin) ); -- Browse-only products intentionally stay out of commerce_offers. This -- server-owned registry lets those known PDPs accept local reviews without -- turning them into purchasable inventory or trusting an ASIN from a form. CREATE TABLE IF NOT EXISTS review_product_catalog ( asin TEXT PRIMARY KEY COLLATE NOCASE CHECK ( length(asin) = 10 AND asin NOT GLOB '*[^A-Z0-9]*' ), source_scope TEXT NOT NULL CHECK ( source_scope IN ('catalog_product', 'commerce_offer', 'home_snapshot') ) ); CREATE TABLE IF NOT EXISTS review_helpful_votes ( vote_id INTEGER PRIMARY KEY AUTOINCREMENT, review_id INTEGER NOT NULL REFERENCES product_reviews(review_id) ON DELETE CASCADE, voter_account_id INTEGER REFERENCES accounts(account_id) ON DELETE CASCADE, voter_session_digest TEXT REFERENCES browser_sessions(session_digest) ON DELETE CASCADE, created_at TEXT NOT NULL, CHECK ( (voter_account_id IS NOT NULL AND voter_session_digest IS NULL) OR (voter_account_id IS NULL AND voter_session_digest IS NOT NULL) ) ); """ _INDEXES_AND_TRIGGERS = """ CREATE INDEX IF NOT EXISTS product_reviews_asin_recent_idx ON product_reviews(asin, created_at DESC, review_id DESC); CREATE INDEX IF NOT EXISTS product_reviews_asin_rating_idx ON product_reviews(asin, rating, created_at DESC, review_id DESC); CREATE INDEX IF NOT EXISTS review_helpful_votes_review_idx ON review_helpful_votes(review_id); CREATE UNIQUE INDEX IF NOT EXISTS review_helpful_votes_account_unique_idx ON review_helpful_votes(review_id, voter_account_id) WHERE voter_account_id IS NOT NULL; CREATE UNIQUE INDEX IF NOT EXISTS review_helpful_votes_session_unique_idx ON review_helpful_votes(review_id, voter_session_digest) WHERE voter_account_id IS NULL; -- Trigger bodies changed when the browse-only review scope was added. Drop -- the old v1 definitions so existing databases receive the additive guard. DROP TRIGGER IF EXISTS product_reviews_product_guard_insert; DROP TRIGGER IF EXISTS product_reviews_product_guard_update; DROP TRIGGER IF EXISTS product_reviews_catalog_product_cleanup; DROP TRIGGER IF EXISTS product_reviews_commerce_offer_cleanup; DROP TRIGGER IF EXISTS product_reviews_scope_cleanup; CREATE TRIGGER product_reviews_product_guard_insert BEFORE INSERT ON product_reviews WHEN NOT EXISTS ( SELECT 1 FROM catalog_products WHERE asin = NEW.asin ) AND NOT EXISTS ( SELECT 1 FROM commerce_offers WHERE asin = NEW.asin ) AND NOT EXISTS ( SELECT 1 FROM review_product_catalog WHERE asin = NEW.asin ) BEGIN SELECT RAISE(ABORT, 'review product does not exist'); END; CREATE TRIGGER product_reviews_product_guard_update BEFORE UPDATE OF asin ON product_reviews WHEN NOT EXISTS ( SELECT 1 FROM catalog_products WHERE asin = NEW.asin ) AND NOT EXISTS ( SELECT 1 FROM commerce_offers WHERE asin = NEW.asin ) AND NOT EXISTS ( SELECT 1 FROM review_product_catalog WHERE asin = NEW.asin ) BEGIN SELECT RAISE(ABORT, 'review product does not exist'); END; CREATE TRIGGER IF NOT EXISTS review_helpful_votes_no_self_insert BEFORE INSERT ON review_helpful_votes WHEN NEW.voter_account_id IS NOT NULL AND EXISTS ( SELECT 1 FROM product_reviews WHERE review_id = NEW.review_id AND account_id = NEW.voter_account_id ) BEGIN SELECT RAISE(ABORT, 'review authors cannot vote for their own review'); END; -- Reviews disappear only when an ASIN is absent from every server-owned -- product scope. Deleting one overlapping source cannot erase valid rows. CREATE TRIGGER product_reviews_catalog_product_cleanup AFTER DELETE ON catalog_products WHEN NOT EXISTS ( SELECT 1 FROM commerce_offers WHERE asin = OLD.asin ) AND NOT EXISTS ( SELECT 1 FROM review_product_catalog WHERE asin = OLD.asin ) BEGIN DELETE FROM product_reviews WHERE asin = OLD.asin; END; CREATE TRIGGER product_reviews_commerce_offer_cleanup AFTER DELETE ON commerce_offers WHEN NOT EXISTS ( SELECT 1 FROM catalog_products WHERE asin = OLD.asin ) AND NOT EXISTS ( SELECT 1 FROM review_product_catalog WHERE asin = OLD.asin ) BEGIN DELETE FROM product_reviews WHERE asin = OLD.asin; END; CREATE TRIGGER product_reviews_scope_cleanup AFTER DELETE ON review_product_catalog WHEN NOT EXISTS ( SELECT 1 FROM catalog_products WHERE asin = OLD.asin ) AND NOT EXISTS ( SELECT 1 FROM commerce_offers WHERE asin = OLD.asin ) BEGIN DELETE FROM product_reviews WHERE asin = OLD.asin; END; """ _EXPECTED_COLUMNS = { "review_product_catalog": {"asin", "source_scope"}, "product_reviews": { "review_id", "account_id", "asin", "rating", "headline", "body", "created_at", "updated_at", }, "review_helpful_votes": { "vote_id", "review_id", "voter_account_id", "voter_session_digest", "created_at", }, } def install_schema(conn: sqlite3.Connection) -> None: """Install the additive v1 schema and validate an existing installation. Run this outside an active request transaction: ``executescript`` owns the DDL transaction boundary in Python's SQLite driver. No core row is changed. """ conn.execute("PRAGMA foreign_keys = ON") conn.executescript(_BASE_SCHEMA) version_row = conn.execute( "SELECT schema_version FROM local_review_schema_meta WHERE singleton=?", (1,), ).fetchone() if version_row is None or int(version_row[0]) != REVIEW_SCHEMA_VERSION: found = None if version_row is None else version_row[0] raise ReviewSchemaError( f"unsupported local review schema version: {found!r}" ) for table, expected in _EXPECTED_COLUMNS.items(): actual = { str(row[1]) for row in conn.execute(f"PRAGMA table_info({table})") } missing = expected - actual if missing: raise ReviewSchemaError( f"{table} is missing required columns: {', '.join(sorted(missing))}" ) conn.executescript(_INDEXES_AND_TRIGGERS) def reset_review_data(conn: sqlite3.Connection) -> None: """Remove mutable review data while preserving the installed schema.""" conn.execute("DELETE FROM review_helpful_votes") conn.execute("DELETE FROM product_reviews") conn.execute("DELETE FROM review_product_catalog") def _normalize_asin(value: object) -> str: if not isinstance(value, str): raise ReviewValidationError("asin must be text") asin = value.strip().upper() if not _ASIN_PATTERN.fullmatch(asin): raise ReviewValidationError("asin must contain 10 ASCII letters or digits") return asin def register_review_product( conn: sqlite3.Connection, *, asin: str, source_scope: str ) -> str: """Register one server-known browse/product scope for local reviews. This is a privileged catalog operation, not a request-form operation. It deliberately stores no offer, rating, review count, author, or excerpt. """ normalized_asin = _normalize_asin(asin) if source_scope not in REVIEW_PRODUCT_SCOPES: raise ReviewValidationError("unsupported review product source scope") conn.execute( """ INSERT INTO review_product_catalog(asin,source_scope) VALUES (?,?) ON CONFLICT(asin) DO UPDATE SET source_scope=excluded.source_scope """, (normalized_asin, source_scope), ) return normalized_asin def _normalize_session_digest(value: object) -> str: if not isinstance(value, str) or not value.strip(): raise ReviewValidationError("session_digest must be non-empty text") return value.strip() def _normalize_review_id(value: object) -> int: if isinstance(value, bool): raise ReviewValidationError("review_id must be a positive integer") if isinstance(value, int): review_id = value elif isinstance(value, str) and value.isascii() and value.isdecimal(): review_id = int(value) else: raise ReviewValidationError("review_id must be a positive integer") if review_id <= 0: raise ReviewValidationError("review_id must be a positive integer") return review_id def _normalize_rating(value: object) -> int: if isinstance(value, bool) or not isinstance(value, int) or not 1 <= value <= 5: raise ReviewValidationError("rating must be an integer from 1 to 5") return value def _normalize_headline(value: object) -> str: if not isinstance(value, str): raise ReviewValidationError("headline must be text") headline = " ".join(value.split()) if not headline: raise ReviewValidationError("headline must not be empty") if len(headline) > MAX_HEADLINE_LENGTH: raise ReviewValidationError( f"headline must be at most {MAX_HEADLINE_LENGTH} characters" ) return headline def _normalize_body(value: object) -> str: if not isinstance(value, str): raise ReviewValidationError("body must be text") body = value.replace("\r\n", "\n").replace("\r", "\n").strip() if not body: raise ReviewValidationError("body must not be empty") if len(body) > MAX_BODY_LENGTH: raise ReviewValidationError( f"body must be at most {MAX_BODY_LENGTH} characters" ) return body def _timestamp(value: str | None) -> str: if value is None: return ( datetime.now(timezone.utc) .isoformat(timespec="seconds") .replace("+00:00", "Z") ) if not isinstance(value, str) or not value.strip() or len(value.strip()) > 64: raise ReviewValidationError("at must be a non-empty timestamp string") return value.strip() def _account_for_session( conn: sqlite3.Connection, session_digest: str, *, require_authenticated: bool, ) -> int | None: row = conn.execute( "SELECT account_id FROM browser_sessions WHERE session_digest=?", (session_digest,), ).fetchone() if row is None: raise ReviewNotFound("browser session does not exist") account_id = None if row[0] is None else int(row[0]) if require_authenticated and account_id is None: raise ReviewAuthenticationRequired("sign in before writing a review") return account_id def _product_exists(conn: sqlite3.Connection, asin: str) -> bool: row = conn.execute( """ SELECT EXISTS( SELECT 1 FROM catalog_products WHERE asin=? UNION ALL SELECT 1 FROM commerce_offers WHERE asin=? UNION ALL SELECT 1 FROM review_product_catalog WHERE asin=? ) """, (asin, asin, asin), ).fetchone() return bool(row and row[0]) def _review_query( conn: sqlite3.Connection, *, where_sql: str, where_params: tuple[object, ...], viewer_session_digest: str | None, order_sql: str, ) -> list[dict[str, Any]]: viewer_account_id: int | None = None viewer_guest_session: str | None = None if viewer_session_digest is not None: normalized_session = _normalize_session_digest(viewer_session_digest) viewer_account_id = _account_for_session( conn, normalized_session, require_authenticated=False ) if viewer_account_id is None: viewer_guest_session = normalized_session cursor = conn.execute( f""" SELECT r.review_id, r.account_id AS reviewer_account_id, a.display_name AS author_display_name, r.rating, r.headline, r.body, r.created_at, r.updated_at, EXISTS( SELECT 1 FROM orders o JOIN order_items oi ON oi.order_id=o.order_id LEFT JOIN shipments shipment ON shipment.order_id=o.order_id WHERE o.account_id=r.account_id AND o.status='PLACED' AND COALESCE(shipment.lifecycle_status,'PREPARING')<>'CANCELLED' AND oi.asin=r.asin ) AS verified_purchase, ( SELECT COUNT(*) FROM review_helpful_votes hv WHERE hv.review_id=r.review_id ) AS helpful_count, CASE WHEN ? IS NOT NULL THEN EXISTS( SELECT 1 FROM review_helpful_votes own_vote WHERE own_vote.review_id=r.review_id AND own_vote.voter_account_id=? ) WHEN ? IS NOT NULL THEN EXISTS( SELECT 1 FROM review_helpful_votes own_vote WHERE own_vote.review_id=r.review_id AND own_vote.voter_session_digest=? ) ELSE 0 END AS viewer_found_helpful FROM product_reviews r JOIN accounts a ON a.account_id=r.account_id WHERE {where_sql} ORDER BY {order_sql} """, ( viewer_account_id, viewer_account_id, viewer_guest_session, viewer_guest_session, *where_params, ), ) names = [str(description[0]) for description in cursor.description] result: list[dict[str, Any]] = [] for raw_row in cursor.fetchall(): row = dict(zip(names, raw_row)) reviewer_account_id = int(row.pop("reviewer_account_id")) owned_by_viewer = ( viewer_account_id is not None and reviewer_account_id == viewer_account_id ) result.append( { "id": str(row["review_id"]), "provenance": LOCAL_REVIEW_PROVENANCE, "author_display_name": str(row["author_display_name"]), "rating": int(row["rating"]), "title": str(row["headline"]), "body": str(row["body"]), "created_at": str(row["created_at"]), "updated_at": str(row["updated_at"]), "verified_purchase": bool(row["verified_purchase"]), "helpful_count": int(row["helpful_count"]), "viewer_found_helpful": bool(row["viewer_found_helpful"]), "owned_by_viewer": owned_by_viewer, "can_mark_helpful": not owned_by_viewer, } ) return result def upsert_review( conn: sqlite3.Connection, *, session_digest: str, asin: str, rating: int, headline: str, body: str, at: str | None = None, ) -> dict[str, Any]: """Create or update the signed-in account's one review for an ASIN. ``verified_purchase`` is intentionally absent from this signature. The returned value is already shaped for ``review_catalog.normalize_local_reviews``. The caller owns commit/rollback. """ normalized_session = _normalize_session_digest(session_digest) normalized_asin = _normalize_asin(asin) normalized_rating = _normalize_rating(rating) normalized_headline = _normalize_headline(headline) normalized_body = _normalize_body(body) timestamp = _timestamp(at) account_id = _account_for_session( conn, normalized_session, require_authenticated=True ) assert account_id is not None if not _product_exists(conn, normalized_asin): raise ReviewNotFound("product does not exist") conn.execute( """ INSERT INTO product_reviews( account_id,asin,rating,headline,body,created_at,updated_at ) VALUES (?,?,?,?,?,?,?) ON CONFLICT(account_id,asin) DO UPDATE SET rating=excluded.rating, headline=excluded.headline, body=excluded.body, updated_at=excluded.updated_at """, ( account_id, normalized_asin, normalized_rating, normalized_headline, normalized_body, timestamp, timestamp, ), ) rows = _review_query( conn, where_sql="r.account_id=? AND r.asin=?", where_params=(account_id, normalized_asin), viewer_session_digest=normalized_session, order_sql="r.review_id", ) if len(rows) != 1: # pragma: no cover - defensive database invariant raise ReviewSchemaError("review upsert did not produce one row") return rows[0] def get_review( conn: sqlite3.Connection, review_id: int | str, *, viewer_session_digest: str | None = None, ) -> dict[str, Any] | None: """Return one review in review-catalog shape, or ``None`` if absent.""" normalized_id = _normalize_review_id(review_id) rows = _review_query( conn, where_sql="r.review_id=?", where_params=(normalized_id,), viewer_session_digest=viewer_session_digest, order_sql="r.review_id", ) return rows[0] if rows else None def list_reviews( conn: sqlite3.Connection, asin: str, *, star: int | None = None, sort: str = REVIEW_SORT_RECENT, viewer_session_digest: str | None = None, ) -> list[dict[str, Any]]: """List local reviews, optionally filtering by star and sorting by recency/helpfulness.""" normalized_asin = _normalize_asin(asin) if star is not None: normalized_star = _normalize_rating(star) else: normalized_star = None if sort not in REVIEW_SORTS: raise ReviewValidationError("sort must be 'recent' or 'helpful'") where_sql = "r.asin=?" params: tuple[object, ...] = (normalized_asin,) if normalized_star is not None: where_sql += " AND r.rating=?" params += (normalized_star,) order_sql = ( "helpful_count DESC, r.created_at DESC, r.review_id DESC" if sort == REVIEW_SORT_HELPFUL else "r.created_at DESC, r.review_id DESC" ) return _review_query( conn, where_sql=where_sql, where_params=params, viewer_session_digest=viewer_session_digest, order_sql=order_sql, ) def toggle_helpful_vote( conn: sqlite3.Connection, *, session_digest: str, review_id: int | str, at: str | None = None, ) -> dict[str, Any]: """Toggle one helpful vote for the authenticated account or guest session. A signed-in account has one durable identity across browser sessions. An anonymous visitor is keyed by the server-created browser session. Authors can never vote for their own review. The caller owns the outer commit. """ normalized_session = _normalize_session_digest(session_digest) normalized_id = _normalize_review_id(review_id) timestamp = _timestamp(at) voter_account_id = _account_for_session( conn, normalized_session, require_authenticated=False ) review_row = conn.execute( "SELECT account_id FROM product_reviews WHERE review_id=?", (normalized_id,), ).fetchone() if review_row is None: raise ReviewNotFound("review does not exist") reviewer_account_id = int(review_row[0]) if voter_account_id is not None and voter_account_id == reviewer_account_id: raise ReviewPermissionDenied("review authors cannot vote for their own review") conn.execute("SAVEPOINT amazon_review_vote") try: if voter_account_id is not None: cursor = conn.execute( """ DELETE FROM review_helpful_votes WHERE review_id=? AND voter_account_id=? """, (normalized_id, voter_account_id), ) found_helpful = cursor.rowcount == 0 if found_helpful: conn.execute( """ INSERT INTO review_helpful_votes( review_id,voter_account_id,voter_session_digest,created_at ) VALUES (?,?,NULL,?) """, (normalized_id, voter_account_id, timestamp), ) else: cursor = conn.execute( """ DELETE FROM review_helpful_votes WHERE review_id=? AND voter_account_id IS NULL AND voter_session_digest=? """, (normalized_id, normalized_session), ) found_helpful = cursor.rowcount == 0 if found_helpful: conn.execute( """ INSERT INTO review_helpful_votes( review_id,voter_account_id,voter_session_digest,created_at ) VALUES (?,NULL,?,?) """, (normalized_id, normalized_session, timestamp), ) count_row = conn.execute( "SELECT COUNT(*) FROM review_helpful_votes WHERE review_id=?", (normalized_id,), ).fetchone() helpful_count = int(count_row[0]) except Exception: conn.execute("ROLLBACK TO amazon_review_vote") conn.execute("RELEASE amazon_review_vote") raise else: conn.execute("RELEASE amazon_review_vote") return { "review_id": str(normalized_id), "found_helpful": found_helpful, "helpful_count": helpful_count, } __all__ = [ "LOCAL_REVIEW_PROVENANCE", "MAX_BODY_LENGTH", "MAX_HEADLINE_LENGTH", "REVIEW_SCHEMA_VERSION", "REVIEW_PRODUCT_SCOPES", "REVIEW_SORT_HELPFUL", "REVIEW_SORT_RECENT", "ReviewAuthenticationRequired", "ReviewNotFound", "ReviewPermissionDenied", "ReviewSchemaError", "ReviewStoreError", "ReviewValidationError", "get_review", "install_schema", "list_reviews", "register_review_product", "reset_review_data", "toggle_helpful_vote", "upsert_review", ]