🐛 Fixed a bug in import function and in PrivateStorageItem

This commit is contained in:
Mawoka
2023-06-11 13:22:10 +02:00
parent e7eb0a7621
commit 0b256e2896
6 changed files with 963 additions and 871 deletions
+1 -1
View File
@@ -54,7 +54,7 @@ pre-commit = "*"
python-socketio = {extras = ["client"], version = "*"}
[requires]
python_version = "3.10"
python_version = "3.11"
[scripts]
format = "black ."
Generated
+18 -26
View File
@@ -1,11 +1,11 @@
{
"_meta": {
"hash": {
"sha256": "c3de18e25234c98fd1b33d4df4464fd6e05a485416c76eb6c4f64e78c497af64"
"sha256": "14b53f70a0556b62779c7f2a76c7850c0e5477005a0e462c587b30a8eecbe4fb"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.10"
"python_version": "3.11"
},
"sources": [
{
@@ -196,7 +196,7 @@
"sha256:2163e1640ddb52b7a8c80d0a67a08587e5d245cc9c553a74a847056bc2976b15",
"sha256:8ca1e4fcf50d07413d66d1a5e416e42cfdf5851c981d679a09851a6853383b3c"
],
"markers": "python_full_version <= '3.11.2'",
"markers": "python_version >= '3.6'",
"version": "==4.0.2"
},
"asyncpg": {
@@ -480,7 +480,7 @@
"sha256:f8303414c7b03f794347ad062c0516cee0e15f7a612abd0ce1e25caf6ceb47df",
"sha256:fca62a8301b605b954ad2e9c3666f9d97f63872aa4efcae5492baca2056b74ab"
],
"markers": "python_version >= '3.7'",
"markers": "python_full_version >= '3.7.0'",
"version": "==3.1.0"
},
"click": {
@@ -562,21 +562,13 @@
"index": "pypi",
"version": "==2.0.0.post2"
},
"exceptiongroup": {
"hashes": [
"sha256:232c37c63e4f682982c8b6459f33a8981039e5fb8756b2074364e5055c498c9e",
"sha256:d484c3090ba2889ae2928419117447a14daf3c1231d5e30d0aae34f354f01785"
],
"markers": "python_version < '3.11'",
"version": "==1.1.1"
},
"fastapi": {
"hashes": [
"sha256:71232d47c2787446991c81c41c249f8a16238d52d779c0e6b43927d3773dbe3c",
"sha256:b8e11fe81e81eab4e1504209917338e0b80f783878a42c2b99467e5e1019a1e9"
"sha256:22d773ce95f14f04f8f37a0c8998fc163e67af83b65510d2879de6cbaaa10215",
"sha256:5c1d243030e63089ccfc0aec69c2da6d619943917727e8e82ee502358d5119bf"
],
"index": "pypi",
"version": "==0.96.0"
"version": "==0.96.1"
},
"frozenlist": {
"hashes": [
@@ -1470,7 +1462,7 @@
"sha256:90260d9058e514786967344d0ef75fa8727eed8a7d2e43ce9f4bcf1b536174f7",
"sha256:e38464a49c6c85d7f1351b0126661487a7e0a14a50f1675ec50eb34d4f20ef21"
],
"markers": "python_version >= '3.6' and python_version < '4.0'",
"markers": "python_version >= '3.6' and python_version < '4'",
"version": "==4.9"
},
"scheduler": {
@@ -2108,7 +2100,7 @@
"sha256:f8303414c7b03f794347ad062c0516cee0e15f7a612abd0ce1e25caf6ceb47df",
"sha256:fca62a8301b605b954ad2e9c3666f9d97f63872aa4efcae5492baca2056b74ab"
],
"markers": "python_version >= '3.7'",
"markers": "python_full_version >= '3.7.0'",
"version": "==3.1.0"
},
"click": {
@@ -2194,11 +2186,11 @@
},
"filelock": {
"hashes": [
"sha256:ad98852315c2ab702aeb628412cbf7e95b7ce8c3bf9565670b4eaecf1db370a9",
"sha256:fc03ae43288c013d2ea83c8597001b1129db351aad9c57fe2409327916b8e718"
"sha256:42f1e4ff2b497311213d61ad7aac5fed9050608e5309573f101eefa94143134a",
"sha256:82b1f7da46f0ae42abf1bc78e548667f484ac59d2bcec38c713cee7e2eb51e83"
],
"markers": "python_version >= '3.7'",
"version": "==3.12.0"
"version": "==3.12.1"
},
"flake8": {
"hashes": [
@@ -2274,11 +2266,11 @@
},
"platformdirs": {
"hashes": [
"sha256:412dae91f52a6f84830f39a8078cecd0e866cb72294a5c66808e74d5e88d251f",
"sha256:e2378146f1964972c03c085bb5662ae80b2b8c06226c54b2ff4aa9483e8a13a5"
"sha256:0ade98a4895e87dc51d47151f7d2ec290365a585151d97b4d8d6312ed6132fed",
"sha256:e48fabd87db8f3a7df7150a4a5ea22c546ee8bc39bc2473244730d4b56d2cc4e"
],
"markers": "python_version >= '3.7'",
"version": "==3.5.1"
"version": "==3.5.3"
},
"pluggy": {
"hashes": [
@@ -2314,11 +2306,11 @@
},
"pytest": {
"hashes": [
"sha256:3799fa815351fea3a5e96ac7e503a96fa51cc9942c3753cda7651b93c1cfa362",
"sha256:434afafd78b1d78ed0addf160ad2b77a30d35d4bdf8af234fe621919d9ed15e3"
"sha256:cdcbd012c9312258922f8cd3f1b62a6580fdced17db6014896053d47cddf9295",
"sha256:ee990a3cc55ba808b80795a79944756f315c67c12b56abd3ac993a7b8c17030b"
],
"index": "pypi",
"version": "==7.3.1"
"version": "==7.3.2"
},
"pytest-asyncio": {
"hashes": [
+1 -1
View File
@@ -451,7 +451,7 @@ class PrivateStorageItem(PublicStorageItem):
quizzes = []
for quiz in data.quizzes:
quizzes.append(OnlyId(id=quiz.id))
for quiztivity in data.quizzes:
for quiztivity in data.quiztivities:
quiztivities.append(OnlyId(id=quiztivity.id))
return cls(
id=data.id,
+13 -5
View File
@@ -27,7 +27,7 @@ async def _download_image(url: str) -> bytes:
DEFAULT_COLORS = ["#D6EDC9", "#B07156", "#7F7057", "#4E6E58"]
async def handle_image_upload(url: str, user: User) -> str:
async def handle_image_upload(url: str, user: User) -> StorageItem:
image_bytes = await _download_image(url)
file_obj = StorageItem(
id=uuid.uuid4(),
@@ -43,7 +43,7 @@ async def handle_image_upload(url: str, user: User) -> str:
await file_obj.save()
await storage.upload(file_name=file_obj.id.hex, file_data=io.BytesIO(image_bytes))
await arq.enqueue_job("calculate_hash", file_obj.id.hex)
return file_obj.id.hex
return file_obj
async def import_quiz(quiz_id: str, user: User) -> Quiz | str:
@@ -61,12 +61,15 @@ async def import_quiz(quiz_id: str, user: User) -> Quiz | str:
quiz_id = uuid.uuid4()
meilisearch.delete_index(settings.meilisearch_index)
meilisearch.create_index(settings.meilisearch_index)
uploaded_images: list[StorageItem] = []
for q in quiz.kahoot.questions:
answers: list[ABCDQuizAnswer] = []
image = None
if q.image is not None and q.image != "":
image = await handle_image_upload(q.image, user)
image_obj = await handle_image_upload(q.image, user)
uploaded_images.append(image_obj)
image = image_obj.id.hex
for i, a in enumerate(q.choices):
answers.append(
(
@@ -88,7 +91,9 @@ async def import_quiz(quiz_id: str, user: User) -> Quiz | str:
)
cover = None
if quiz.kahoot.cover != "" and quiz.kahoot.cover is not None:
cover = await handle_image_upload(quiz.kahoot.cover, user)
img_obj = await handle_image_upload(quiz.kahoot.cover, user)
uploaded_images.append(img_obj)
cover = img_obj.id.hex
quiz_data = Quiz(
id=quiz_id,
public=True,
@@ -103,4 +108,7 @@ async def import_quiz(quiz_id: str, user: User) -> Quiz | str:
kahoot_id=uuid.UUID(kahoot_quiz_id),
)
meilisearch.index(settings.meilisearch_index).add_documents([await get_meili_data(quiz_data)])
return await quiz_data.save()
await quiz_data.save()
for img in uploaded_images:
await quiz_data.storageitems.add(img)
return quiz_data
+2 -1
View File
@@ -190,7 +190,8 @@ async def import_quiz_route(quiz_id: str, user: User = Depends(get_current_user)
raise HTTPException(status_code=409, detail="Storage limit reached")
try:
return await import_quiz(quiz_id, user)
except ValidationError:
except ValidationError as e:
print(e)
raise HTTPException(status_code=400, detail="This quiz isn't (yet) supported")
+928 -837
View File
File diff suppressed because it is too large Load Diff