Spaces:
Build error
Build error
File size: 4,149 Bytes
b7a7f32 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
import math
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from models import User
from utils import deps
from datetime import datetime, timedelta
from cruds import crud_quiz_answer, crud_question, crud_quiz
from schemas import (
QuizAnswer,
QuizAnswerCreate,
QuizAnswerUpdate,
QuizAnsweronlySelected,
QuizAnswerwithName,
)
from typing import Any, Optional, List, Dict # noqa
router = APIRouter()
@router.get("/")
async def get_answers(
db: Session = Depends(deps.get_db),
*,
current_user: User = Depends(deps.get_current_active_user),
):
pass
@router.get("/{quizid}", response_model=QuizAnswer, response_model_exclude_none=True)
async def get_answers_quiz(
db: Session = Depends(deps.get_db),
*,
quizid: int,
current_user: User = Depends(deps.get_current_active_user),
):
answer = crud_quiz_answer.get_by_quiz_id(
db=db, quizId=quizid, studentId=current_user.id
)
if answer:
marks = answer.marks_obtained
answer.marks_obtained = None
quiz = crud_quiz.get(db=db, id=quizid)
if (
quiz
and quiz.end_time
and quiz.end_time <= (datetime.utcnow() - timedelta(seconds=15))
):
answer.marks_obtained = marks
return answer
raise HTTPException(status_code=404, detail="Error ID: 144")
@router.get("/{quizid}/getAnswersAsTeacher/", response_model=List[QuizAnswerwithName])
async def get_quiz_answers_as_teacher(
db: Session = Depends(deps.get_db),
*,
quizid: int,
current_user: User = Depends(deps.get_current_active_teacher_or_above),
):
if current_user.quiz:
for quiz in current_user.quiz:
if quiz.id == quizid:
answers = crud_quiz_answer.get_all_by_quiz_id_as_teacher(
db=db, quizId=quizid
)
if len(answers) >= 1:
return answers
raise HTTPException(
status_code=404,
detail="Error ID: 143", # could not populate answer
)
@router.get("/{quizid}/exists/")
async def check_existence(
db: Session = Depends(deps.get_db),
*,
quizid: int,
current_user: User = Depends(deps.get_current_active_user),
):
answer = crud_quiz_answer.get_by_quiz_id(
db=db, quizId=quizid, studentId=current_user.id
)
if not answer:
return {"exists": False}
else:
return {"exists": True}
@router.get("/{id}")
async def get_specific_answer():
pass
@router.post("/{quiz_id}", response_model=QuizAnsweronlySelected)
async def submit_answer(
db: Session = Depends(deps.get_db),
*,
questionAnswer: Dict[int, Any],
quiz_id: int,
current_user: User = Depends(deps.get_current_active_user),
):
questions = crud_question.get_all_by_quiz_id(db, quiz_id=quiz_id)
marksObtained = 0
correctCount = 0
for question in questions:
if question.id in questionAnswer.keys():
questionOption = questionAnswer[question.id]
if question.multiple:
if len(question.answer) >= len(questionOption):
for answer in questionOption:
if answer in question.answer:
correctCount = correctCount + 1
correctCount = correctCount / len(question.answer)
else:
questionAnswer[question.id] = questionOption
if questionOption == question.answer[0]:
correctCount = 1
marksObtained = marksObtained + correctCount * question.marks
correctCount = 0
questionAnswer = QuizAnswerCreate(
marks_obtained=math.ceil(marksObtained),
options_selected=questionAnswer,
quiz_id=quiz_id,
student_id=current_user.id,
)
try:
questionAnswer = crud_quiz_answer.create(db, obj_in=questionAnswer)
return questionAnswer
except Exception:
raise HTTPException(
status_code=400,
detail="Error ID: 142", # could not populate answer
)
|