pdf-quiz-generator/backend/app/models/draft_question.py
Daniel c9abc4f310 feat: extracted questions are staged, and only acceptance takes an id
Extraction wrote straight into `questions`, so a machine's first attempt
took a permanent id the moment it was produced. Ids come from a sequence
and are never reissued: every rejected draft burned one, and every draft
that needed fixing was sitting in the bank while it was being fixed.

A run now lands in a batch of drafts with their own table and their own
sequence. They are read, corrected and decided there, and `accept` is
the only place a Question is created — a copy rather than a translation,
because every field a draft holds is a field a question has, so nothing
is lost at the moment of acceptance.

Accepting is all or nothing, and everything is checked before anything
is created: a call that reports failure must not leave questions behind
from the drafts it got through first. My own test caught that — the
first question existed before the second draft was refused.

Readiness is reported for every draft rather than only on the attempt to
accept it, so a reviewer sees what needs work before opening anything.
A decided draft keeps its row and records what it became, so a batch
reads as a history of what was decided rather than emptying as it is
worked through. An acceptance cannot be undone from here: the question
exists, and deciding twice would make a second one.

No embeddings for drafts. A vector is for finding a question in the
bank, and a draft is not in the bank.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TqXevQJhxFrM7jJg82cgZN
2026-09-11 22:57:08 +02:00

90 lines
4.4 KiB
Python

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")