✨ Added kahoot-import
This commit is contained in:
+4
-4
@@ -14,14 +14,14 @@ async def cache_account(criteria: str, content: str) -> Union[User, None]:
|
|||||||
|
|
||||||
if criteria == "email":
|
if criteria == "email":
|
||||||
try:
|
try:
|
||||||
res = await User.objects.search()
|
res = await User.objects.get(email=content, verified=True)
|
||||||
except ormar.exceptions.NoMatch:
|
except ormar.exceptions.NoMatch:
|
||||||
return None
|
return None
|
||||||
await insert_into_redis(res, content)
|
await insert_into_redis(res, content)
|
||||||
return res
|
return res
|
||||||
elif criteria == "username":
|
elif criteria == "username":
|
||||||
try:
|
try:
|
||||||
res = await User.objects.search()
|
res = await User.objects.get(username=content, verified=True)
|
||||||
except ormar.exceptions.NoMatch:
|
except ormar.exceptions.NoMatch:
|
||||||
return None
|
return None
|
||||||
await insert_into_redis(res, content)
|
await insert_into_redis(res, content)
|
||||||
@@ -29,7 +29,7 @@ async def cache_account(criteria: str, content: str) -> Union[User, None]:
|
|||||||
elif criteria == "id":
|
elif criteria == "id":
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res = await User.objects.search()
|
res = await User.objects.get(id=uuid.UUID(content), verified=True)
|
||||||
except ormar.exceptions.NoMatch:
|
except ormar.exceptions.NoMatch:
|
||||||
return None
|
return None
|
||||||
await insert_into_redis(res, content)
|
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]:
|
async def get_from_redis(key: str) -> Union[None, User]:
|
||||||
user = await redis.search(key)
|
user = await redis.get(key)
|
||||||
if user is None:
|
if user is None:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -9,7 +9,12 @@ class _Response(BaseModel):
|
|||||||
kahoot: _Kahoot
|
kahoot: _Kahoot
|
||||||
|
|
||||||
|
|
||||||
async def get(game_id: str) -> _Response:
|
async def get(game_id: str) -> _Response | None:
|
||||||
async with ClientSession() as session:
|
async with ClientSession() as session:
|
||||||
async with session.get(f"https://create.kahoot.it/rest/kahoots/{game_id}/card/?includeKahoot=true") as response:
|
async with session.get(f"https://create.kahoot.it/rest/kahoots/{game_id}/card/?includeKahoot=true") as response:
|
||||||
|
if response.status == 200:
|
||||||
return _Response(**await response.json())
|
return _Response(**await response.json())
|
||||||
|
elif response.status == 404:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
raise Exception(f"Unexpected response status: {response.status}")
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
from aiohttp import ClientSession
|
|
||||||
import pydantic
|
|
||||||
|
|
||||||
@@ -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()
|
||||||
@@ -3,6 +3,7 @@ import uuid
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
|
from classquiz.kahoot_importer.import_quiz import import_quiz
|
||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
from fastapi.responses import JSONResponse
|
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.updated_at = datetime.now()
|
||||||
quiz.questions = quiz_input.dict()["questions"]
|
quiz.questions = quiz_input.dict()["questions"]
|
||||||
return await quiz.update()
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user