mirror of
https://github.com/handsomezhuzhu/QQuiz.git
synced 2026-02-20 20:10:14 +00:00
第一阶段bug修复完毕
This commit is contained in:
@@ -11,6 +11,7 @@ import os
|
||||
import aiofiles
|
||||
import json
|
||||
import magic
|
||||
import random
|
||||
|
||||
from database import get_db
|
||||
from models import User, Exam, Question, ExamStatus, SystemConfig
|
||||
@@ -142,9 +143,8 @@ async def generate_ai_reference_answer(
|
||||
# Build prompt based on question type
|
||||
if question_type in ["single", "multiple"] and options:
|
||||
options_text = "\n".join(options)
|
||||
prompt = f"""这是一道{
|
||||
'单选题' if question_type == 'single' else '多选题'
|
||||
},但文档中没有提供答案。请根据题目内容,推理出最可能的正确答案。
|
||||
q_type_text = '单选题' if question_type == 'single' else '多选题'
|
||||
prompt = f"""这是一道{q_type_text},但文档中没有提供答案。请根据题目内容,推理出最可能的正确答案。
|
||||
|
||||
题目:{question_content}
|
||||
|
||||
@@ -205,7 +205,8 @@ async def process_questions_with_dedup(
|
||||
exam_id: int,
|
||||
questions_data: List[dict],
|
||||
db: AsyncSession,
|
||||
llm_service=None
|
||||
llm_service=None,
|
||||
is_random: bool = False
|
||||
) -> ParseResult:
|
||||
"""
|
||||
Process parsed questions with fuzzy deduplication logic.
|
||||
@@ -238,6 +239,11 @@ async def process_questions_with_dedup(
|
||||
|
||||
print(f"[Dedup] Checking against {len(existing_questions)} existing questions in database")
|
||||
|
||||
# Shuffle questions if random mode is enabled
|
||||
if is_random:
|
||||
print(f"[Dedup] Random mode enabled - shuffling {len(questions_data)} questions before saving")
|
||||
random.shuffle(questions_data)
|
||||
|
||||
# Insert only new questions
|
||||
for q_data in questions_data:
|
||||
content_hash = q_data.get("content_hash")
|
||||
@@ -313,7 +319,8 @@ async def async_parse_and_save(
|
||||
exam_id: int,
|
||||
file_content: bytes,
|
||||
filename: str,
|
||||
db_url: str
|
||||
db_url: str,
|
||||
is_random: bool = False
|
||||
):
|
||||
"""
|
||||
Background task to parse document and save questions with deduplication.
|
||||
@@ -487,7 +494,7 @@ async def async_parse_and_save(
|
||||
))
|
||||
|
||||
print(f"[Exam {exam_id}] Processing questions with deduplication...")
|
||||
parse_result = await process_questions_with_dedup(exam_id, questions_data, db, llm_service)
|
||||
parse_result = await process_questions_with_dedup(exam_id, questions_data, db, llm_service, is_random)
|
||||
|
||||
# Update exam status and total questions
|
||||
result = await db.execute(select(Exam).where(Exam.id == exam_id))
|
||||
@@ -540,6 +547,7 @@ async def create_exam_with_upload(
|
||||
request: Request,
|
||||
background_tasks: BackgroundTasks,
|
||||
title: str = Form(...),
|
||||
is_random: bool = Form(False),
|
||||
file: UploadFile = File(...),
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db)
|
||||
@@ -574,7 +582,8 @@ async def create_exam_with_upload(
|
||||
new_exam.id,
|
||||
file_content,
|
||||
file.filename,
|
||||
os.getenv("DATABASE_URL")
|
||||
os.getenv("DATABASE_URL"),
|
||||
is_random
|
||||
)
|
||||
|
||||
return ExamUploadResponse(
|
||||
|
||||
@@ -19,6 +19,53 @@ from services.config_service import load_llm_config
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/", response_model=QuestionListResponse)
|
||||
async def get_all_questions(
|
||||
skip: int = 0,
|
||||
limit: int = 50,
|
||||
exam_id: Optional[int] = None,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
"""Get all questions with optional exam filter"""
|
||||
|
||||
# Build query
|
||||
query = select(Question).order_by(Question.id)
|
||||
count_query = select(func.count(Question.id))
|
||||
|
||||
# Apply exam filter if provided
|
||||
if exam_id is not None:
|
||||
# Verify exam ownership/access
|
||||
result = await db.execute(
|
||||
select(Exam).where(
|
||||
and_(Exam.id == exam_id, Exam.user_id == current_user.id)
|
||||
)
|
||||
)
|
||||
exam = result.scalar_one_or_none()
|
||||
if not exam:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Exam not found"
|
||||
)
|
||||
|
||||
query = query.where(Question.exam_id == exam_id)
|
||||
count_query = count_query.where(Question.exam_id == exam_id)
|
||||
else:
|
||||
# If no exam filter, only show questions from exams owned by user
|
||||
query = query.join(Exam).where(Exam.user_id == current_user.id)
|
||||
count_query = count_query.join(Exam).where(Exam.user_id == current_user.id)
|
||||
|
||||
# Get total count
|
||||
result = await db.execute(count_query)
|
||||
total = result.scalar()
|
||||
|
||||
# Get questions
|
||||
result = await db.execute(query.offset(skip).limit(limit))
|
||||
questions = result.scalars().all()
|
||||
|
||||
return QuestionListResponse(questions=questions, total=total)
|
||||
|
||||
|
||||
@router.get("/exam/{exam_id}/questions", response_model=QuestionListResponse)
|
||||
async def get_exam_questions(
|
||||
exam_id: int,
|
||||
|
||||
Reference in New Issue
Block a user