4b7156061e
study_topic 워크스페이스에 4지선다 문제은행 자산 트랙 추가. 기사시험 필기
대비 시나리오 — 빠른 반복 입력 + 과목별 균등 추출 복습 + 정오답 누적.
데이터 모델 (migrations 186~190):
- study_questions: study_topic 1:N, soft delete, is_active 토글, correct_choice
SMALLINT CHECK 1~4
- study_question_attempts: 답 제출 1행 누적. study_question_id FK는 ON DELETE
RESTRICT (이력 보존 원칙 — hard delete 실수로 풀이 기록 소실 차단)
설계 원칙:
- 문제 삭제는 API 에서 soft delete only. attempts FK RESTRICT 로 DB 레벨도 보호
- correct_choice 변경 시 기존 attempts.is_correct 재계산 안 함 (시점 사실 보존)
- 복습 default = 과목별 target_per_subject(20) 무작위 균등 추출. 한 과목이
부족하면 가용한 만큼만
- wrong_only=true 정의 = 가장 최근 attempt 가 오답인 문제 (latest-wrong, ever-wrong 아님)
- 출제 응답에서 정답·해설 비공개. 답 제출 시점에만 노출
- subject/scope 강한 enum 미사용 (자유 텍스트, 자동완성은 후속)
API: /api/study-topics/{id}/questions, /review/questions, /api/study-questions/{id},
/attempt. 통합뷰(/study-topics/{id}) 응답에 sections.questions / stats.question_count
추가. 기존 question_set_count 는 후속 PR(회차/모의고사 묶음)용으로 보존.
프론트: /study/topics/[id]에 문제 섹션 + "새 문제"/"복습 시작" 진입.
/questions/new (저장 후 계속 입력 + sessionStorage persistent),
/questions/[qid]/edit (정답 변경 시 attempts 재계산 안 됨 안내 배너),
/review (시작 옵션 → 풀이 → 마지막 요약).
후속 PR 예정: 오답노트/취약 과목 리포트, AI 해설/클러스터링, spaced
repetition, 이미지 OCR 입력, CSV import, study_question_sets 묶음.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
88 lines
3.9 KiB
Python
88 lines
3.9 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 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)
|
|
|
|
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")
|