63 lines
2.4 KiB
Python
63 lines
2.4 KiB
Python
"""
|
|
SQLAlchemy model for user_predictions.
|
|
|
|
This module defines the database model for tracking which predictions
|
|
each user has viewed and their accuracy.
|
|
"""
|
|
|
|
from datetime import datetime
|
|
from sqlalchemy import Column, Integer, DateTime, Boolean, ForeignKey, Index, UniqueConstraint
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from app.database import Base
|
|
|
|
|
|
class UserPrediction(Base):
|
|
"""
|
|
Model for tracking predictions viewed by users.
|
|
|
|
This table tracks:
|
|
- Which predictions each user has viewed
|
|
- When they viewed them
|
|
- Whether the prediction was correct (when match result is known)
|
|
|
|
Attributes:
|
|
id: Primary key
|
|
user_id: Foreign key to users table
|
|
prediction_id: Foreign key to predictions table
|
|
viewed_at: Timestamp when user viewed the prediction
|
|
was_correct: True if prediction was correct, False if incorrect, NULL if match not completed
|
|
"""
|
|
__tablename__ = "user_predictions"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True)
|
|
prediction_id = Column(Integer, ForeignKey("predictions.id", ondelete="CASCADE"), nullable=False, index=True)
|
|
viewed_at = Column(DateTime, nullable=False, index=True)
|
|
was_correct = Column(Boolean, nullable=True, comment='True if prediction was correct, False if incorrect, NULL if match not completed')
|
|
|
|
# Relationships
|
|
user = relationship("User", back_populates="user_predictions")
|
|
prediction = relationship("Prediction", back_populates="user_predictions")
|
|
|
|
# Constraints and indexes
|
|
__table_args__ = (
|
|
Index('idx_user_predictions_user_id', 'user_id'),
|
|
Index('idx_user_predictions_prediction_id', 'prediction_id'),
|
|
Index('idx_user_predictions_viewed_at', 'viewed_at'),
|
|
UniqueConstraint('user_id', 'prediction_id', name='uq_user_predictions_user_prediction'),
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<UserPrediction(id={self.id}, user_id={self.user_id}, prediction_id={self.prediction_id}, viewed_at={self.viewed_at})>"
|
|
|
|
def to_dict(self) -> dict:
|
|
"""Convert user prediction model to dictionary."""
|
|
return {
|
|
'id': self.id,
|
|
'user_id': self.user_id,
|
|
'prediction_id': self.prediction_id,
|
|
'viewed_at': self.viewed_at.isoformat() if self.viewed_at else None,
|
|
'was_correct': self.was_correct
|
|
}
|