5c8fe9a62b
- 001: guard has_table check; skip if session_data already TEXT - 002: guard each ADD COLUMN / CREATE TABLE; guard password migration - 003: guard has_table; guard nullable check - 004: guard has_table; skip if users.id already TEXT - env.py: add Base.metadata.create_all after run_migrations to bootstrap fresh DBs - api/user.py: make hashed_password nullable; add email_verified, image, email_inbound_token fields Co-Authored-By: Paperclip <noreply@paperclip.ing>
66 lines
2.6 KiB
Python
66 lines
2.6 KiB
Python
"""User and UserStoreAccount models."""
|
|
|
|
import secrets
|
|
from datetime import datetime
|
|
from typing import TYPE_CHECKING
|
|
|
|
import sqlalchemy as sa
|
|
from sqlalchemy import Boolean, DateTime, ForeignKey, String, Text, UniqueConstraint
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from cartsnitch_api.constants import AccountStatus
|
|
from cartsnitch_api.models.base import Base, TimestampMixin, UUIDPrimaryKeyMixin
|
|
from cartsnitch_api.types import EncryptedJSON
|
|
|
|
if TYPE_CHECKING:
|
|
from cartsnitch_api.models.purchase import Purchase
|
|
from cartsnitch_api.models.store import Store
|
|
|
|
|
|
class User(TimestampMixin, Base):
|
|
"""Application user."""
|
|
|
|
__tablename__ = "users"
|
|
|
|
id: Mapped[str] = mapped_column(Text, primary_key=True)
|
|
email: Mapped[str] = mapped_column(String(255), nullable=False, unique=True)
|
|
hashed_password: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
|
display_name: Mapped[str | None] = mapped_column(String(100))
|
|
email_verified: Mapped[bool] = mapped_column(
|
|
Boolean, nullable=False, server_default="false"
|
|
)
|
|
image: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
email_inbound_token: Mapped[str] = mapped_column(
|
|
String(22),
|
|
nullable=False,
|
|
unique=True,
|
|
default=lambda: secrets.token_urlsafe(16),
|
|
server_default=sa.text(
|
|
"replace(replace(trim(trailing '=' from encode(gen_random_bytes(16), 'base64')), '+', '-'), '/', '_')"
|
|
),
|
|
)
|
|
|
|
# Relationships
|
|
store_accounts: Mapped[list["UserStoreAccount"]] = relationship(back_populates="user")
|
|
purchases: Mapped[list["Purchase"]] = relationship(back_populates="user")
|
|
|
|
|
|
class UserStoreAccount(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
|
"""Link between a user and their retailer account credentials."""
|
|
|
|
__tablename__ = "user_store_accounts"
|
|
__table_args__ = (UniqueConstraint("user_id", "store_id", name="uq_user_store_account"),)
|
|
|
|
user_id: Mapped[str] = mapped_column(ForeignKey("users.id"), nullable=False)
|
|
store_id: Mapped[str] = mapped_column(ForeignKey("stores.id"), nullable=False)
|
|
session_data: Mapped[dict | None] = mapped_column(EncryptedJSON)
|
|
session_expires_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
|
last_sync_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
|
status: Mapped[AccountStatus] = mapped_column(
|
|
String(20), nullable=False, default=AccountStatus.ACTIVE
|
|
)
|
|
|
|
# Relationships
|
|
user: Mapped["User"] = relationship(back_populates="store_accounts")
|
|
store: Mapped["Store"] = relationship(back_populates="user_accounts")
|