- Migrated all schemas to Pydantic v2 syntax (model_config, ConfigDict) - Fixed mutable default in ProtoMessageBase using Field(default_factory=datetime.now) - Consolidated CardCollection and Wishlist schemas in user_card_collection.py - Created game_schemas.py with GameCreate, GameResponse, GameJoinRequest, etc. - Created mtg_card_schemas.py with MtgCardResponse, MtgCardSearchRequest, etc. - Added CardImportBatchCreate, CardImportBatchResponse, UserCardImportCreate/Response schemas - Fixed duplicate UserCardImportRecord class between card_import_batch.py and user_card_import_record.py - Updated __init__.py with comprehensive schema exports - Created verify_schemas.py for schema-model matching verification
75 lines
2.7 KiB
Python
75 lines
2.7 KiB
Python
"""
|
|
SQLAlchemy ORM models for card import tracking.
|
|
|
|
This module provides models for tracking card import batches
|
|
and individual card import records.
|
|
"""
|
|
from sqlalchemy import (
|
|
Column, Integer, String, BigInteger, Boolean, DateTime, Text,
|
|
ForeignKey, UniqueConstraint
|
|
)
|
|
from sqlalchemy.orm import relationship
|
|
from sqlalchemy.sql import func
|
|
from app.core.database import Base
|
|
|
|
|
|
class CardImportBatch(Base):
|
|
"""
|
|
Card import batch tracking.
|
|
|
|
Tracks a batch of card imports from a file, including
|
|
matching results and error information.
|
|
"""
|
|
__tablename__ = "card_import_batches"
|
|
|
|
id = Column(BigInteger, primary_key=True, autoincrement=True)
|
|
user_id = Column(Integer, ForeignKey("mtgonline_users.id", ondelete="CASCADE"), nullable=False)
|
|
filename = Column(String(255), nullable=False)
|
|
file_type = Column(String(50), nullable=True) # 'mtgjson', 'csv', etc.
|
|
file_size = Column(BigInteger, nullable=True) # Size in bytes
|
|
status = Column(String(20), default="PENDING") # PENDING, PROCESSING, COMPLETED, FAILED
|
|
total_cards = Column(Integer, default=0)
|
|
matched_cards = Column(Integer, default=0)
|
|
unmatched_cards = Column(Integer, default=0)
|
|
match_results = Column(Text, nullable=True) # JSON of match details
|
|
error_message = Column(Text, nullable=True)
|
|
created_at = Column(DateTime, server_default=func.now())
|
|
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())
|
|
|
|
# Relationships
|
|
user = relationship("User", backref="card_import_batches")
|
|
records = relationship(
|
|
"UserCardImportRecord",
|
|
back_populates="batch",
|
|
cascade="all, delete-orphan"
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<CardImportBatch {self.filename} (status={self.status})>"
|
|
|
|
|
|
class UserCardImportRecord(Base):
|
|
"""
|
|
Individual card import record within a batch.
|
|
|
|
Tracks whether each card in an import batch was confirmed
|
|
and when the confirmation happened.
|
|
"""
|
|
__tablename__ = "user_card_imports_confirmed"
|
|
|
|
id = Column(BigInteger, primary_key=True, autoincrement=True)
|
|
user_id = Column(Integer, ForeignKey("mtgonline_users.id", ondelete="CASCADE"), nullable=False)
|
|
batch_id = Column(BigInteger, ForeignKey("card_import_batches.id", ondelete="CASCADE"), nullable=False)
|
|
is_confirmed = Column(Boolean, default=False)
|
|
confirmed_at = Column(DateTime, nullable=True)
|
|
|
|
# Relationships
|
|
batch = relationship("CardImportBatch", back_populates="records")
|
|
|
|
__table_args__ = (
|
|
UniqueConstraint('user_id', 'batch_id', name='uq_user_card_import_batch'),
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<UserCardImportRecord user={self.user_id} batch={self.batch_id} confirmed={self.is_confirmed}>"
|