8803e6a0fd
기사시험 회차별 100문제 채워가기 시나리오. 문제 입력 페이지를 단순 폼에서
"회차 진행률 추적·재개" 도구로 보강.
데이터 모델 (migrations 195~197):
- study_topics: exam_round_size INT CHECK 1~300 (회차당 문항 수, NULL=미설정)
+ exam_subjects JSONB DEFAULT '[]' (과목 리스트, 입력 페이지 드롭다운 옵션)
- study_questions: exam_question_number SMALLINT CHECK >0 (회차 안 문항 번호)
- partial idx (study_topic_id, exam_round, exam_question_number) WHERE
deleted_at IS NULL AND exam_round IS NOT NULL — 회차별 max+count 고속화
백엔드:
- POST /questions: exam_round 명시 + exam_question_number 미명시 시 서버가
같은 토픽·회차의 max+1 자동 채움
- 신규 GET /api/study-topics/{id}/exam-rounds: 회차별 진행률 집계
{exam_round_size, items: [{exam_round, question_count, max_question_number,
next_question_number, is_complete}]}
- StudyTopic Create/Update/Response/Meta 에 exam_round_size·exam_subjects
- StudyQuestion Create/Update/Response 에 exam_question_number
- exam_question_number 변경은 embedding stale 트리거에서 제외 (의미 영향 없음)
프론트:
- 토픽 생성/편집 모달: "시험 정보" 섹션 (회차당 문항 수 + 과목 리스트
+추가/제거 칩)
- /study/topics/[id]/exam-rounds 신규 페이지: 회차 카드 + 진행 바 +
[N번부터 이어서] 버튼 + [새 회차 시작] 모달
- 통합뷰 문제 섹션 헤더에 [회차 보기] 진입점
- /questions/new 페이지 전면 개편:
- 시험명 = topic.name 자동 prefill
- 과목 드롭다운 (topic.exam_subjects + 기존 distinct, "직접 입력" 토글)
- 회차 드롭다운 (기존 distinct + "새 회차")
- 문항 번호 자동 (회차 선택 시 next_question_number, 새 회차 = 1)
- 진행률 바 (현재/exam_round_size)
- 출처/메모 자동 합성 "회차 N번" (수정 가능)
- "저장 후 계속 입력" → 본문/보기/정답 reset, 회차 유지, 문항 +1
- 회차 변경 감지 시 문항 번호 1로 reset
- exam_round_size 도달 시 회차 강조 + "저장 후 계속 입력" 비활성
- query string ?exam_round=&start_qnum= 지원 (회차 목록에서 재개 진입)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
115 lines
5.2 KiB
Python
115 lines
5.2 KiB
Python
"""study_questions / study_question_attempts ORM — 학습 워크스페이스의 문제은행 트랙
|
|
|
|
PR-2 가드레일:
|
|
- study_topic 1차 컨테이너에 자산 타입별 조인 테이블 추가 방식. polymorphic 단일 테이블 영구 금지.
|
|
- subject/scope 는 강한 enum 미사용 (jlpt 등 어학 분류 확장 여지).
|
|
- 문제 삭제는 API 에서 soft delete only. attempts FK 는 ON DELETE RESTRICT 로 DB 레벨 보호 (hard delete 실수 차단, 이력 보존).
|
|
- correct_choice 변경 시 기존 attempt.is_correct 재계산 안 함 (기록은 그 시점의 사실).
|
|
"""
|
|
|
|
from datetime import datetime
|
|
|
|
from pgvector.sqlalchemy import Vector
|
|
from sqlalchemy import BigInteger, Boolean, DateTime, ForeignKey, Integer, SmallInteger, String, Text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from core.database import Base
|
|
|
|
|
|
class StudyQuestion(Base):
|
|
__tablename__ = "study_questions"
|
|
|
|
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(
|
|
BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
study_topic_id: Mapped[int] = mapped_column(
|
|
BigInteger, ForeignKey("study_topics.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
|
|
question_text: Mapped[str] = mapped_column(Text, nullable=False)
|
|
choice_1: Mapped[str] = mapped_column(Text, nullable=False)
|
|
choice_2: Mapped[str] = mapped_column(Text, nullable=False)
|
|
choice_3: Mapped[str] = mapped_column(Text, nullable=False)
|
|
choice_4: Mapped[str] = mapped_column(Text, nullable=False)
|
|
correct_choice: Mapped[int] = mapped_column(SmallInteger, nullable=False)
|
|
|
|
subject: Mapped[str | None] = mapped_column(String(120))
|
|
scope: Mapped[str | None] = mapped_column(String(200))
|
|
exam_name: Mapped[str | None] = mapped_column(String(120))
|
|
exam_round: Mapped[str | None] = mapped_column(String(120))
|
|
explanation: Mapped[str | None] = mapped_column(Text)
|
|
source_note: Mapped[str | None] = mapped_column(Text)
|
|
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
|
|
|
|
# PR-6: 회차 안 문항 번호 (1~exam_round_size). NULL 허용 — 기존 행 + 회차 미설정 입력
|
|
exam_question_number: Mapped[int | None] = mapped_column(SmallInteger)
|
|
|
|
# PR-3: AI 풀이 캐시 (수동 트리거)
|
|
# status: none | pending | ready | failed | stale (강한 enum 미사용, VARCHAR 권장값)
|
|
ai_explanation: Mapped[str | None] = mapped_column(Text)
|
|
ai_explanation_status: Mapped[str] = mapped_column(
|
|
String(20), default="none", nullable=False
|
|
)
|
|
ai_explanation_generated_at: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True)
|
|
)
|
|
ai_explanation_model: Mapped[str | None] = mapped_column(String(120))
|
|
|
|
# PR-4: 자동 임베딩 (bge-m3 1024차원). status 가 큐 역할.
|
|
# 재계산 트리거 = question_text / choice_1~4 변경.
|
|
# correct_choice / subject / scope / explanation 변경은 재계산 안 함.
|
|
embedding = mapped_column(Vector(1024), nullable=True)
|
|
embedding_status: Mapped[str] = mapped_column(
|
|
String(20), default="none", nullable=False
|
|
)
|
|
embedding_updated_at: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True)
|
|
)
|
|
embedding_model: Mapped[str | None] = mapped_column(String(120))
|
|
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), default=datetime.now, nullable=False
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), default=datetime.now, onupdate=datetime.now, nullable=False
|
|
)
|
|
deleted_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
|
|
|
# 연관 — 통합 뷰/통계 조회 시 selectinload 으로 끌어옴
|
|
topic: Mapped["StudyTopic | None"] = relationship( # type: ignore[name-defined] # noqa: F821
|
|
"StudyTopic", back_populates="questions", lazy="noload"
|
|
)
|
|
attempts: Mapped[list["StudyQuestionAttempt"]] = relationship(
|
|
back_populates="question",
|
|
cascade="all, delete-orphan", # ORM 레벨 cascade — 실 hard delete 는 RESTRICT FK 가 막음
|
|
order_by="StudyQuestionAttempt.answered_at.desc()",
|
|
lazy="noload",
|
|
)
|
|
|
|
|
|
class StudyQuestionAttempt(Base):
|
|
__tablename__ = "study_question_attempts"
|
|
|
|
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(
|
|
BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
study_question_id: Mapped[int] = mapped_column(
|
|
BigInteger,
|
|
ForeignKey("study_questions.id", ondelete="RESTRICT"),
|
|
nullable=False,
|
|
)
|
|
study_topic_id: Mapped[int] = mapped_column(
|
|
BigInteger, ForeignKey("study_topics.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
selected_choice: Mapped[int] = mapped_column(SmallInteger, nullable=False)
|
|
correct_choice: Mapped[int] = mapped_column(SmallInteger, nullable=False)
|
|
is_correct: Mapped[bool] = mapped_column(Boolean, nullable=False)
|
|
answered_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), default=datetime.now, nullable=False
|
|
)
|
|
|
|
question: Mapped["StudyQuestion"] = relationship(back_populates="attempts")
|