This repository has been archived on 2026-02-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
shared/infrastructure/cart_identity.py
giles ef806f8fbb feat: extract shared infrastructure from shared_lib
Phase 1-3 of decoupling plan:
- Shared DB, models, infrastructure, browser, config, utils
- Event infrastructure (domain_events outbox, bus, processor)
- Structured logging
- Generic container concept (container_type/container_id)
- Alembic migrations for all schema changes

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 12:45:56 +00:00

35 lines
944 B
Python

"""
Cart identity resolution — shared across all apps that need to know
who the current cart owner is (user_id or anonymous session_id).
"""
from __future__ import annotations
import secrets
from typing import TypedDict, Optional
from quart import g, session as qsession
class CartIdentity(TypedDict):
user_id: Optional[int]
session_id: Optional[str]
def current_cart_identity() -> CartIdentity:
"""
Decide how to identify the cart:
- If user is logged in -> use user_id (and ignore session_id)
- Else -> generate / reuse an anonymous session_id stored in Quart's session
"""
user = getattr(g, "user", None)
if user is not None and getattr(user, "id", None) is not None:
return {"user_id": user.id, "session_id": None}
sid = qsession.get("cart_sid")
if not sid:
sid = secrets.token_hex(16)
qsession["cart_sid"] = sid
return {"user_id": None, "session_id": sid}