✨ Sentry in Backend, Image-Import, Settings-function improvement and reload detection
This commit is contained in:
@@ -2,10 +2,48 @@ import json
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from aiohttp import ClientSession
|
||||
import pydantic
|
||||
from aiohttp import ClientSession, FormData
|
||||
import html
|
||||
from classquiz.db.models import Quiz, QuizAnswer, QuizQuestion, User
|
||||
from classquiz.kahoot_importer.get import get as get_quiz
|
||||
from classquiz.config import settings
|
||||
settings = settings()
|
||||
|
||||
async def _download_image(url: str) -> bytes:
|
||||
async with ClientSession() as session:
|
||||
async with session.get(url) as resp:
|
||||
return await resp.read()
|
||||
|
||||
|
||||
async def _upload_to_imgur(image: bytes, album_id: str | None) -> str:
|
||||
"""
|
||||
Uploads an image to Imgur.
|
||||
:param image: The image to upload.
|
||||
:return: The URL of the image on Imgur.
|
||||
"""
|
||||
|
||||
formdata = FormData()
|
||||
formdata.add_field('image', image, filename='image.png')
|
||||
if album_id:
|
||||
formdata.add_field('album', album_id)
|
||||
async with ClientSession(headers={"Authorization": f"Client-ID {settings.imgur_client_id}"}) as session:
|
||||
async with session.post("https://api.imgur.com/3/image", data=image) as resp:
|
||||
data = await resp.json()
|
||||
return data["data"]["link"]
|
||||
|
||||
|
||||
async def _generate_imgur_album(quiz_id: str) -> str:
|
||||
"""
|
||||
Generates an album on Imgur.
|
||||
:param quiz_id: The ID of the quiz.
|
||||
:return: The URL of the album on Imgur.
|
||||
"""
|
||||
formdata = FormData()
|
||||
formdata.add_field('title', quiz_id)
|
||||
async with ClientSession(headers={"Authorization": f"Client-ID {settings.imgur_client_id}"}) as session:
|
||||
async with session.post("https://api.imgur.com/3/album", data={"title": "Quiz: " + quiz_id}, ) as resp:
|
||||
data = await resp.json()
|
||||
return data["data"]["id"]
|
||||
|
||||
|
||||
async def import_quiz(quiz_id: str, user: User) -> Quiz | str:
|
||||
@@ -19,12 +57,19 @@ async def import_quiz(quiz_id: str, user: User) -> Quiz | str:
|
||||
if quiz is None:
|
||||
return "quiz not found"
|
||||
quiz_questions: list[dict] = []
|
||||
imgur_album_id = await _generate_imgur_album(quiz_id)
|
||||
|
||||
for q in quiz.kahoot.questions:
|
||||
answers: list[QuizAnswer] = []
|
||||
image = None
|
||||
if q.image is not None and q.image != "":
|
||||
image_bytes = await _download_image(q.image)
|
||||
image = await _upload_to_imgur(image_bytes, imgur_album_id)
|
||||
# image = q.image
|
||||
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())
|
||||
answers.append((QuizAnswer(right=a.correct, answer=html.unescape(a.answer))))
|
||||
quiz_questions.append(
|
||||
QuizQuestion(question=q.question, answers=answers, time=str(q.time / 1000), image=image).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))
|
||||
|
||||
Reference in New Issue
Block a user