from datetime import datetime from sqlalchemy import JSON, Column, DateTime, ForeignKey, Integer, String, Text from sqlalchemy.orm import relationship from app.database import Base class DraftBatch(Base): """One run of extraction, held apart from the question bank. Questions pulled out of a PDF used to be written straight into `questions`, which meant a machine's first attempt took a permanent id the moment it was produced. Ids come from a sequence and are never reissued, so every rejected draft burned one, and every draft that needed fixing was already in the bank while it was being fixed. A batch is where a run lands instead: readable, editable, and rejectable, with nothing in the bank until somebody says so. """ __tablename__ = "draft_batches" id = Column(Integer, primary_key=True, index=True) title = Column(String(200), nullable=False) #: Where it came from, so a draft can be checked against its source. document_id = Column(Integer, ForeignKey("pdf_documents.id", ondelete="SET NULL"), nullable=True) section_id = Column(Integer, ForeignKey("sections.id", ondelete="SET NULL"), nullable=True) job_id = Column(String(64), nullable=True, index=True) #: Which model produced it, and how it was asked. Two batches from the same #: pages can differ entirely on these, and an educator comparing them #: should not have to guess which was which. model_id = Column(String(160), nullable=True) extraction_mode = Column(String(40), nullable=True) #: Where the questions go when they are accepted. Chosen up front so a #: batch has a home before anyone reads it. category_id = Column(Integer, ForeignKey("question_categories.id", ondelete="SET NULL"), nullable=True) created_by = Column(Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True) created_at = Column(DateTime, default=datetime.utcnow) #: open — being worked through. closed — nothing left to decide. status = Column(String(20), default="open", index=True) drafts = relationship("DraftQuestion", back_populates="batch", cascade="all, delete-orphan", order_by="DraftQuestion.position") class DraftQuestion(Base): """A proposed question. It has an id here, and no id in the bank. The fields mirror `Question` because promoting one is a copy, not a translation — anything that cannot be expressed here would be lost at the moment of acceptance, which is the worst possible time to discover it. """ __tablename__ = "draft_questions" id = Column(Integer, primary_key=True, index=True) batch_id = Column(Integer, ForeignKey("draft_batches.id", ondelete="CASCADE"), nullable=False, index=True) position = Column(Integer, default=0) question_text = Column(Text, nullable=False) question_type = Column(String, nullable=False, default="mcq") options = Column(JSON, nullable=True) correct_answer = Column(String, nullable=True) explanation = Column(Text, nullable=True) option_explanations = Column(JSON, nullable=True) key_points = Column(JSON, nullable=True) attending_tip = Column(Text, nullable=True) difficulty = Column(String(10), nullable=True) page_reference = Column(Integer, nullable=True) image_path = Column(String, nullable=True) explanation_image_path = Column(String, nullable=True) #: Overrides the batch's category for this one draft. category_id = Column(Integer, ForeignKey("question_categories.id", ondelete="SET NULL"), nullable=True) #: pending | accepted | rejected. An accepted draft keeps its row and #: records which question it became, so the batch still reads as a history #: of what was decided rather than emptying as it is worked through. status = Column(String(20), default="pending", index=True) question_id = Column(Integer, ForeignKey("questions.id", ondelete="SET NULL"), nullable=True) #: Why it was turned down, for whoever tunes the extraction next. note = Column(Text, nullable=True) decided_by = Column(Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True) decided_at = Column(DateTime, nullable=True) #: Whether a human changed it before deciding — the number worth knowing #: about a model's output. edited = Column(Integer, default=0) created_at = Column(DateTime, default=datetime.utcnow) batch = relationship("DraftBatch", back_populates="drafts")