Skip to content

Commit

Permalink
🐛 Fixed overwriting images when importing from excel (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
mawoka-myblock committed Dec 20, 2023
1 parent 10adb16 commit 6571fff
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions classquiz/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ async def handle_import_from_excel(data: BinaryIO, user: User) -> Quiz:
raise HTTPException(status_code=400, detail="Description doesn't have the correct length")
if not 3 <= len(title) <= 500:
raise HTTPException(status_code=400, detail="Title doesn't have the correct length")
existing_quiz: Quiz | None = await Quiz.objects.get_or_none(user_id=user, title=title)
for i, row in enumerate(ws.iter_rows(min_row=14, min_col=2, max_row=100, max_col=8, values_only=True)):
if row[0] is None:
continue
Expand All @@ -154,8 +155,15 @@ async def handle_import_from_excel(data: BinaryIO, user: User) -> Quiz:
if answer is None:
continue
answers_list.append(ABCDQuizAnswer(answer=answer, right=str(a + 1) in correct_answers))
questions.append(QuizQuestion(question=question, answers=answers_list, time=str(time)).dict())
existing_quiz: Quiz | None = await Quiz.objects.get_or_none(user_id=user, title=title)
existing_question: dict = existing_quiz.questions[i]
questions.append(
QuizQuestion(
question=question,
answers=answers_list,
time=str(time),
image=existing_question["image"] if existing_question is not None else None,
).dict()
)
if existing_quiz is None:
quiz = Quiz(
title=title,
Expand Down

0 comments on commit 6571fff

Please sign in to comment.