Download tasks/columbia/environment/reference/clone/site/models.py from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 4.97 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/columbia/environment/reference/clone/site/models.py
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/columbia/environment/reference/clone/site/models.py
-
curl -L -o models.py https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/columbia/environment/reference/clone/site/models.py
4.97 kB
| """Deterministic local store for cart, sessions and saved items. | |
| Every mutation lands in the site-bound SQLite database created by the generated | |
| site_backend integration; nothing is kept in process memory. | |
| """ | |
| from __future__ import annotations | |
| import sqlite3 | |
| from dataclasses import dataclass | |
| SCHEMA = """ | |
| CREATE TABLE IF NOT EXISTS cart_item ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| session_id TEXT NOT NULL, | |
| product_id TEXT NOT NULL, | |
| variant_id TEXT, | |
| color_code TEXT, | |
| size TEXT, | |
| quantity INTEGER NOT NULL DEFAULT 1, | |
| saved_for_later INTEGER NOT NULL DEFAULT 0, | |
| created_at TEXT NOT NULL DEFAULT (datetime('now')), | |
| UNIQUE (session_id, product_id, color_code, size, saved_for_later) | |
| ); | |
| CREATE INDEX IF NOT EXISTS cart_item_session ON cart_item (session_id); | |
| CREATE TABLE IF NOT EXISTS promo_code ( | |
| code TEXT PRIMARY KEY, | |
| label TEXT NOT NULL, | |
| percent_off REAL NOT NULL | |
| ); | |
| CREATE TABLE IF NOT EXISTS applied_promo ( | |
| session_id TEXT PRIMARY KEY, | |
| code TEXT NOT NULL | |
| ); | |
| """ | |
| # Source-observed member promotion: the Women grid and cart both render | |
| # "Extra 30% Off Applied" for Greater Rewards members on sale styles. | |
| SEED_PROMOS = ( | |
| ("MEMBER30", "Extra 30% Off Applied", 30.0), | |
| ) | |
| class CartLine: | |
| id: int | |
| product_id: str | |
| variant_id: str | None | |
| color_code: str | None | |
| size: str | None | |
| quantity: int | |
| saved_for_later: bool | |
| def ensure_schema(connection: sqlite3.Connection) -> None: | |
| connection.executescript(SCHEMA) | |
| connection.executemany( | |
| "INSERT OR IGNORE INTO promo_code (code, label, percent_off) VALUES (?, ?, ?)", | |
| SEED_PROMOS, | |
| ) | |
| connection.commit() | |
| def add_item( | |
| connection: sqlite3.Connection, | |
| session_id: str, | |
| product_id: str, | |
| variant_id: str | None, | |
| color_code: str | None, | |
| size: str | None, | |
| quantity: int = 1, | |
| ) -> None: | |
| connection.execute( | |
| """ | |
| INSERT INTO cart_item | |
| (session_id, product_id, variant_id, color_code, size, quantity, saved_for_later) | |
| VALUES (?, ?, ?, ?, ?, ?, 0) | |
| ON CONFLICT (session_id, product_id, color_code, size, saved_for_later) | |
| DO UPDATE SET quantity = quantity + excluded.quantity | |
| """, | |
| (session_id, product_id, variant_id, color_code, size, quantity), | |
| ) | |
| connection.commit() | |
| def set_quantity( | |
| connection: sqlite3.Connection, session_id: str, line_id: int, quantity: int | |
| ) -> None: | |
| if quantity <= 0: | |
| remove_item(connection, session_id, line_id) | |
| return | |
| connection.execute( | |
| "UPDATE cart_item SET quantity = ? WHERE id = ? AND session_id = ?", | |
| (quantity, line_id, session_id), | |
| ) | |
| connection.commit() | |
| def remove_item(connection: sqlite3.Connection, session_id: str, line_id: int) -> None: | |
| connection.execute( | |
| "DELETE FROM cart_item WHERE id = ? AND session_id = ?", (line_id, session_id) | |
| ) | |
| connection.commit() | |
| def set_saved( | |
| connection: sqlite3.Connection, session_id: str, line_id: int, saved: bool | |
| ) -> None: | |
| connection.execute( | |
| "UPDATE cart_item SET saved_for_later = ? WHERE id = ? AND session_id = ?", | |
| (1 if saved else 0, line_id, session_id), | |
| ) | |
| connection.commit() | |
| def lines( | |
| connection: sqlite3.Connection, session_id: str, saved: bool = False | |
| ) -> list[CartLine]: | |
| rows = connection.execute( | |
| """ | |
| SELECT id, product_id, variant_id, color_code, size, quantity, saved_for_later | |
| FROM cart_item WHERE session_id = ? AND saved_for_later = ? | |
| ORDER BY created_at, id | |
| """, | |
| (session_id, 1 if saved else 0), | |
| ).fetchall() | |
| return [ | |
| CartLine( | |
| id=row[0], product_id=row[1], variant_id=row[2], color_code=row[3], | |
| size=row[4], quantity=row[5], saved_for_later=bool(row[6]), | |
| ) | |
| for row in rows | |
| ] | |
| def apply_promo(connection: sqlite3.Connection, session_id: str, code: str) -> bool: | |
| row = connection.execute( | |
| "SELECT code FROM promo_code WHERE code = ?", (code.strip().upper(),) | |
| ).fetchone() | |
| if not row: | |
| return False | |
| connection.execute( | |
| "INSERT INTO applied_promo (session_id, code) VALUES (?, ?) " | |
| "ON CONFLICT (session_id) DO UPDATE SET code = excluded.code", | |
| (session_id, row[0]), | |
| ) | |
| connection.commit() | |
| return True | |
| def clear_promo(connection: sqlite3.Connection, session_id: str) -> None: | |
| connection.execute("DELETE FROM applied_promo WHERE session_id = ?", (session_id,)) | |
| connection.commit() | |
| def active_promo( | |
| connection: sqlite3.Connection, session_id: str | |
| ) -> tuple[str, str, float] | None: | |
| row = connection.execute( | |
| """ | |
| SELECT p.code, p.label, p.percent_off | |
| FROM applied_promo a JOIN promo_code p ON p.code = a.code | |
| WHERE a.session_id = ? | |
| """, | |
| (session_id,), | |
| ).fetchone() | |
| return (row[0], row[1], row[2]) if row else None | |