from datetime import datetime from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Text from sqlalchemy.orm import relationship from app.database import Base from app.models.quiz_category import QuizCategory # noqa — ensures mapper resolves "QuizCategory" from app.models.quiz_question_link import QuizQuestionLink # noqa #: `Quiz.origin` for a quiz that backs a study-plan block rather than standing #: on its own. Kept here because both the study-plan builder that sets it and #: the session list that filters on it need the same spelling. PLAN_ORIGIN = "plan" class Quiz(Base): __tablename__ = "quizzes" id = Column(Integer, primary_key=True, index=True) section_id = Column(Integer, ForeignKey("sections.id", ondelete="SET NULL"), nullable=True) # Cascades: deleting an account is documented as removing what it made. # Declared here because the database has always done it, and a model that # says nothing is a model that will be believed. user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=True) category_id = Column(Integer, ForeignKey("quiz_categories.id", ondelete="SET NULL"), nullable=True) title = Column(String, nullable=False) questions_count = Column(Integer, default=0) time_limit_minutes = Column(Integer, nullable=True) # null = no limit mode = Column(String, default="timed") # timed, learning skipped_questions = Column(Text, nullable=True) # JSON list of skipped question texts created_at = Column(DateTime, default=datetime.utcnow) deleted_at = Column(DateTime, nullable=True) # soft delete — null = active is_published = Column(Integer, default=1) # 1 = visible to users, 0 = hidden (admin only) is_shared = Column(Integer, default=0) # 0=private, 1=shared (visible to other users) # A repetition is practice, not a new measurement. You have already seen # these questions and their answers, so getting them right the second time # says nothing about whether you knew them — it cannot raise a percentage # that is meant to mean "how much of this do you know". is_repetition = Column(Integer, default=0, index=True) max_attempts = Column(Integer, nullable=True) # null = unlimited questions_per_attempt = Column(Integer, nullable=True) # null = all; set = random subset from pool share_token = Column(String(64), unique=True, nullable=True) # public /share/{token} link when set # bank | upload | ai | sample | plan. # "plan" marks a quiz that exists to back a study-plan block. It is still a # real quiz — it just is not something the learner picks off a list. origin = Column(String(20), default="bank") section = relationship("Section", back_populates="quizzes") user = relationship("User", back_populates="quizzes") category = relationship("QuizCategory", back_populates="quizzes", foreign_keys=[category_id]) questions = relationship( "Question", secondary="quiz_question_links", primaryjoin="Quiz.id == foreign(QuizQuestionLink.quiz_id)", secondaryjoin="Question.id == foreign(QuizQuestionLink.question_id)", order_by="QuizQuestionLink.position", viewonly=True, # mutations handled explicitly via QuizQuestionLink ) attempts = relationship("QuizAttempt", back_populates="quiz", cascade="all, delete-orphan")