From 4f4b38d8b016d85de0356304fccbeb4e0db22264 Mon Sep 17 00:00:00 2001 From: Mawoka Date: Tue, 8 Mar 2022 21:42:44 +0100 Subject: [PATCH] :sparkles: Added kahoot-import --- classquiz/cache.py | 8 +++---- classquiz/kahoot_importer/get.py | 9 +++++-- classquiz/kahoot_importer/import.py | 3 --- classquiz/kahoot_importer/import_quiz.py | 30 ++++++++++++++++++++++++ classquiz/routers/quiz.py | 6 +++++ 5 files changed, 47 insertions(+), 9 deletions(-) delete mode 100644 classquiz/kahoot_importer/import.py create mode 100644 classquiz/kahoot_importer/import_quiz.py diff --git a/classquiz/cache.py b/classquiz/cache.py index 7ad4092..8cef3f9 100644 --- a/classquiz/cache.py +++ b/classquiz/cache.py @@ -14,14 +14,14 @@ async def cache_account(criteria: str, content: str) -> Union[User, None]: if criteria == "email": try: - res = await User.objects.search() + res = await User.objects.get(email=content, verified=True) except ormar.exceptions.NoMatch: return None await insert_into_redis(res, content) return res elif criteria == "username": try: - res = await User.objects.search() + res = await User.objects.get(username=content, verified=True) except ormar.exceptions.NoMatch: return None await insert_into_redis(res, content) @@ -29,7 +29,7 @@ async def cache_account(criteria: str, content: str) -> Union[User, None]: elif criteria == "id": try: - res = await User.objects.search() + res = await User.objects.get(id=uuid.UUID(content), verified=True) except ormar.exceptions.NoMatch: return None await insert_into_redis(res, content) @@ -39,7 +39,7 @@ async def cache_account(criteria: str, content: str) -> Union[User, None]: async def get_from_redis(key: str) -> Union[None, User]: - user = await redis.search(key) + user = await redis.get(key) if user is None: return None else: diff --git a/classquiz/kahoot_importer/get.py b/classquiz/kahoot_importer/get.py index e64fced..399a0c6 100644 --- a/classquiz/kahoot_importer/get.py +++ b/classquiz/kahoot_importer/get.py @@ -9,7 +9,12 @@ class _Response(BaseModel): kahoot: _Kahoot -async def get(game_id: str) -> _Response: +async def get(game_id: str) -> _Response | None: async with ClientSession() as session: async with session.get(f"https://create.kahoot.it/rest/kahoots/{game_id}/card/?includeKahoot=true") as response: - return _Response(**await response.json()) + if response.status == 200: + return _Response(**await response.json()) + elif response.status == 404: + return None + else: + raise Exception(f"Unexpected response status: {response.status}") diff --git a/classquiz/kahoot_importer/import.py b/classquiz/kahoot_importer/import.py deleted file mode 100644 index 9f2c1bc..0000000 --- a/classquiz/kahoot_importer/import.py +++ /dev/null @@ -1,3 +0,0 @@ -from aiohttp import ClientSession -import pydantic - diff --git a/classquiz/kahoot_importer/import_quiz.py b/classquiz/kahoot_importer/import_quiz.py new file mode 100644 index 0000000..8b4eb7f --- /dev/null +++ b/classquiz/kahoot_importer/import_quiz.py @@ -0,0 +1,30 @@ +import json +import uuid +from datetime import datetime + +from aiohttp import ClientSession +import pydantic +from classquiz.db.models import Quiz, QuizAnswer, QuizQuestion, User +from classquiz.kahoot_importer.get import get as get_quiz + + +async def import_quiz(quiz_id: str, user: User) -> Quiz | str: + """ + Imports a quiz from Kahoot. + :param quiz_id: The ID of the quiz to import. + :return: True if the import was successful, False otherwise. + """ + quiz = await get_quiz(quiz_id) + if quiz is None: + return "quiz not found" + quiz_questions: list[dict] = [] + + for q in quiz.kahoot.questions: + answers: list[QuizAnswer] = [] + for a in q.choices: + answers.append((QuizAnswer(right=a.correct, answer=a.answer))) + quiz_questions.append(QuizQuestion(question=q.question, answers=answers, time=str(q.time / 1000)).dict()) + quiz_data = Quiz(id=uuid.uuid4(), public=False, title=quiz.kahoot.title, description=quiz.kahoot.description, + created_at=datetime.now(), updated_at=datetime.now(), user_id=user.id, + questions=json.dumps(quiz_questions)) + return await quiz_data.save() diff --git a/classquiz/routers/quiz.py b/classquiz/routers/quiz.py index c4c1ef5..e6ccf1a 100644 --- a/classquiz/routers/quiz.py +++ b/classquiz/routers/quiz.py @@ -3,6 +3,7 @@ import uuid from datetime import datetime from random import randint +from classquiz.kahoot_importer.import_quiz import import_quiz from fastapi import APIRouter, Depends, HTTPException from fastapi.responses import JSONResponse @@ -84,3 +85,8 @@ async def update_quiz(quiz_id: str, quiz_input: QuizInput, user: User = Depends( quiz.updated_at = datetime.now() quiz.questions = quiz_input.dict()["questions"] return await quiz.update() + + +@router.post("/import/{quiz_id}") +async def import_quiz_route(quiz_id: str, user: User = Depends(get_current_user)): + return await import_quiz(quiz_id, user)