Download tasks/columbia/environment/reference/clone/site/session.py from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 6.54 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/columbia/environment/reference/clone/site/session.py
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/columbia/environment/reference/clone/site/session.py
-
curl -L -o session.py https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/columbia/environment/reference/clone/site/session.py
6.54 kB
| """Session identity and the cart read/write service. | |
| State lives in the site-bound SQLite database opened through the generated | |
| site_backend integration seam, so a restart keeps the bag and a reset clears it. | |
| """ | |
| from __future__ import annotations | |
| import secrets | |
| import sqlite3 | |
| import sys | |
| from pathlib import Path | |
| from typing import Any | |
| import models | |
| import render | |
| CLONE_ROOT = Path(__file__).resolve().parents[1] | |
| if str(CLONE_ROOT) not in sys.path: | |
| sys.path.insert(0, str(CLONE_ROOT)) | |
| # Source-observed delivery ladder from the cart Order Summary. | |
| DELIVERY_OPTIONS = ( | |
| {"id": "standard", "label": "Standard Delivery (4-6 Business Days)", "price": 0.0, | |
| "price_label": "FREE"}, | |
| {"id": "expedited", "label": "Expedited Delivery (2-3 Business Days)", "price": 19.0, | |
| "price_label": "$19.00"}, | |
| {"id": "rush", "label": "Rush Delivery (1-2 Business Days)", "price": 25.0, | |
| "price_label": "$25.00"}, | |
| ) | |
| # Source-observed member state for the captured account: a Greater Rewards | |
| # member with a zero rewards balance and free standard shipping. | |
| REWARDS_BALANCE = 0.0 | |
| POINTS_PER_DOLLAR = 10 | |
| def _database_path() -> Path: | |
| import json | |
| runtime = json.loads((CLONE_ROOT.parent / "backend" / "runtime.json").read_text()) | |
| database = runtime["database"] | |
| data_dir = CLONE_ROOT / database["data_dir"] | |
| data_dir.mkdir(parents=True, exist_ok=True) | |
| return data_dir / database["filename"] | |
| def session_id_for(request: Any) -> str: | |
| existing = request.cookies.get(CartService.COOKIE) | |
| if existing and len(existing) >= 16 and existing.isalnum(): | |
| return existing | |
| return secrets.token_hex(16) | |
| class CartService: | |
| COOKIE = "columbia_session" | |
| def __init__(self, catalog) -> None: | |
| self.catalog = catalog | |
| self._path = _database_path() | |
| with self._connect() as connection: | |
| models.ensure_schema(connection) | |
| def _connect(self) -> sqlite3.Connection: | |
| connection = sqlite3.connect(self._path) | |
| connection.execute("PRAGMA foreign_keys = ON") | |
| return connection | |
| # -- mutations --------------------------------------------------------- | |
| def add(self, session_id, product_id, variant_id, color_code, size, quantity): | |
| with self._connect() as connection: | |
| models.add_item(connection, session_id, product_id, variant_id, color_code, size, quantity) | |
| def set_quantity(self, session_id, line_id, quantity): | |
| with self._connect() as connection: | |
| models.set_quantity(connection, session_id, line_id, quantity) | |
| def remove(self, session_id, line_id): | |
| with self._connect() as connection: | |
| models.remove_item(connection, session_id, line_id) | |
| def set_saved(self, session_id, line_id, saved): | |
| with self._connect() as connection: | |
| models.set_saved(connection, session_id, line_id, saved) | |
| def apply_promo(self, session_id, code) -> bool: | |
| with self._connect() as connection: | |
| return models.apply_promo(connection, session_id, code) | |
| def clear_promo(self, session_id) -> None: | |
| with self._connect() as connection: | |
| models.clear_promo(connection, session_id) | |
| # -- reads ------------------------------------------------------------- | |
| def item_count(self, session_id: str) -> int: | |
| with self._connect() as connection: | |
| return sum(line.quantity for line in models.lines(connection, session_id)) | |
| def view(self, session_id: str) -> dict[str, object]: | |
| with self._connect() as connection: | |
| raw_lines = models.lines(connection, session_id) | |
| saved_raw = models.lines(connection, session_id, saved=True) | |
| promo = models.active_promo(connection, session_id) | |
| lines = [self._hydrate(line) for line in raw_lines] | |
| lines = [line for line in lines if line] | |
| saved_lines = [self._hydrate(line) for line in saved_raw] | |
| saved_lines = [line for line in saved_lines if line] | |
| subtotal = sum(line["line_total_value"] for line in lines) | |
| regular = sum(line["regular_total_value"] for line in lines) | |
| discount = subtotal * (promo[2] / 100.0) if promo else 0.0 | |
| shipping = 0.0 # members get free standard shipping (source-observed) | |
| total = max(subtotal - discount, 0.0) + shipping | |
| savings = max(regular - (subtotal - discount), 0.0) | |
| for line in lines: | |
| line["promo_label"] = promo[1] if promo and line["is_sale"] else None | |
| return { | |
| "lines": lines, | |
| "saved_lines": saved_lines, | |
| "item_count": sum(line["quantity"] for line in lines), | |
| "promo": {"code": promo[0], "label": promo[1]} if promo else None, | |
| "rewards_balance": render.money(REWARDS_BALANCE), | |
| "total_points": int(total * POINTS_PER_DOLLAR) if total else 0, | |
| "delivery_options": DELIVERY_OPTIONS, | |
| "delivery_id": "standard", | |
| "totals": { | |
| "subtotal": render.money(subtotal), | |
| "discount": render.money(discount) if discount else "", | |
| "shipping": "FREE", | |
| "tax": "—", | |
| "total": render.money(total), | |
| "savings": render.money(savings) if savings else "", | |
| }, | |
| } | |
| def _hydrate(self, line) -> dict[str, object] | None: | |
| product = self.catalog.products.get(line.product_id) | |
| if product is None: | |
| return None | |
| variant = product.variant_for_color(line.color_code) | |
| unit = product.sale_price if product.sale_price is not None else product.min_price | |
| unit = unit or 0.0 | |
| regular = product.max_price if product.max_price is not None else unit | |
| return { | |
| "id": line.id, | |
| "product": product, | |
| "variant": variant, | |
| "color_code": line.color_code, | |
| "color_name": variant.color_name if variant else None, | |
| "size": line.size, | |
| "quantity": line.quantity, | |
| "image": render.media_url(variant.primary_image) if variant else None, | |
| "unit_price": render.money(unit), | |
| "regular_price": render.money(regular), | |
| "line_total": render.money(unit * line.quantity), | |
| "line_total_value": unit * line.quantity, | |
| "regular_total_value": regular * line.quantity, | |
| "is_sale": product.is_sale, | |
| "points": int(unit * line.quantity * POINTS_PER_DOLLAR), | |
| "promo_label": None, | |
| } | |