From 76311ed0ee67dfa1c0a48b819eb848cd53b18ed5 Mon Sep 17 00:00:00 2001 From: Mawoka Date: Fri, 27 Jan 2023 23:02:53 +0100 Subject: [PATCH] :sparkles: Added button to view imported quiz on Kahoot!. --- classquiz/db/models.py | 1 + classquiz/kahoot_importer/import_quiz.py | 5 +- classquiz/oauth/github.py | 1 - classquiz/routers/quiz.py | 1 - classquiz/routers/users/__init__.py | 3 +- .../src/lib/components/buttons/gray.svelte | 3 + .../src/routes/view/[quiz_id]/+page.svelte | 164 ++++++++++-------- .../versions/7ad8502af419_added_kahoot_id.py | 29 ++++ 8 files changed, 124 insertions(+), 83 deletions(-) create mode 100644 migrations/versions/7ad8502af419_added_kahoot_id.py diff --git a/classquiz/db/models.py b/classquiz/db/models.py index 2249a6f..b6cbef1 100644 --- a/classquiz/db/models.py +++ b/classquiz/db/models.py @@ -181,6 +181,7 @@ class Quiz(ormar.Model): cover_image: Optional[str] = ormar.Text(nullable=True, unique=False) background_color: str | None = ormar.Text(nullable=True, unique=False) background_image: str | None = ormar.Text(nullable=True, unique=False) + kahoot_id: uuid.UUID | None = ormar.UUID(nullable=True, default=None) @validator("background_image") def must_come_from_local_cdn(cls, v): diff --git a/classquiz/kahoot_importer/import_quiz.py b/classquiz/kahoot_importer/import_quiz.py index d31351c..88f42a8 100644 --- a/classquiz/kahoot_importer/import_quiz.py +++ b/classquiz/kahoot_importer/import_quiz.py @@ -30,7 +30,8 @@ async def import_quiz(quiz_id: str, user: User) -> Quiz | str: :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) + kahoot_quiz_id = quiz_id + quiz = await get_quiz(kahoot_quiz_id) if quiz is None: return "quiz not found" quiz_questions: list[dict] = [] @@ -60,7 +61,6 @@ async def import_quiz(quiz_id: str, user: User) -> Quiz | str: ).dict() ) cover = None - print(quiz.kahoot.cover) if quiz.kahoot.cover != "" and quiz.kahoot.cover is not None: image_bytes = await _download_image(quiz.kahoot.cover) image_name = f"{quiz_id}--{uuid.uuid4()}" @@ -77,6 +77,7 @@ async def import_quiz(quiz_id: str, user: User) -> Quiz | str: questions=json.dumps(quiz_questions), imported_from_kahoot=True, cover_image=cover, + 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() diff --git a/classquiz/oauth/github.py b/classquiz/oauth/github.py index 10d92c0..0bad0e7 100644 --- a/classquiz/oauth/github.py +++ b/classquiz/oauth/github.py @@ -105,7 +105,6 @@ async def auth(request: Request, response: Response): if user_data.email is None: return RedirectResponse("/account/oauth-error?error=email") user_in_db = await User.objects.get_or_none(email=user_data.email) - print(user_data.id) if user_in_db is None: # REGISTER USER try: diff --git a/classquiz/routers/quiz.py b/classquiz/routers/quiz.py index c6d9946..b21abd3 100644 --- a/classquiz/routers/quiz.py +++ b/classquiz/routers/quiz.py @@ -92,7 +92,6 @@ async def get_public_quiz(quiz_id: str): except ValueError: raise HTTPException(status_code=400, detail="badly formed quiz id") quiz = await Quiz.objects.select_related("user_id").get_or_none(id=quiz_id) - print(quiz.dict(exclude={"user_id": {"avatar"}})) if quiz is None: return JSONResponse(status_code=404, content={"detail": "quiz not found"}) else: diff --git a/classquiz/routers/users/__init__.py b/classquiz/routers/users/__init__.py index b28573f..514efbc 100644 --- a/classquiz/routers/users/__init__.py +++ b/classquiz/routers/users/__init__.py @@ -331,8 +331,7 @@ async def get_email_from_jwt(data: GetEmailFromJWT): try: payload = jwt.decode(data.jwt, settings.secret_key, algorithms=["HS256"]) return payload.get("sub") - except JWTError as e: - print(e) + except JWTError: raise HTTPException(status_code=401) diff --git a/frontend/src/lib/components/buttons/gray.svelte b/frontend/src/lib/components/buttons/gray.svelte index fd0972a..a8d5c4c 100644 --- a/frontend/src/lib/components/buttons/gray.svelte +++ b/frontend/src/lib/components/buttons/gray.svelte @@ -3,14 +3,17 @@ export let flex = false; export let href: undefined | string = undefined; + export let target: undefined | string = '_self'; {#if href} diff --git a/frontend/src/routes/view/[quiz_id]/+page.svelte b/frontend/src/routes/view/[quiz_id]/+page.svelte index 890aab1..e343b4e 100644 --- a/frontend/src/routes/view/[quiz_id]/+page.svelte +++ b/frontend/src/routes/view/[quiz_id]/+page.svelte @@ -12,6 +12,7 @@ import StartGamePopup from '$lib/dashboard/start_game.svelte'; import { onMount } from 'svelte'; import Spinner from '$lib/Spinner.svelte'; + import GrayButton from '$lib/components/buttons/gray.svelte'; const tippy = createTippy({ arrow: true, @@ -56,6 +57,7 @@ user_id: string; imported_from_kahoot?: boolean; questions: Question[]; + kahoot_id?: string; } @@ -82,87 +84,95 @@ {/if} -
+
- -
- {#if logged_in} - - {:else} -
- -
- {/if} -
-
-
- {#if logged_in} - - - - - {$t('words.download')} - - {:else} -
- + View on Kahoot! + +
+ {/if} + {#if logged_in} +
+ { + start_game = quiz.id; + }} + > + {$t('words.start')} + +
+ {:else} +
+
+ + {$t('words.start')} + +
+
+ {/if} +
+ + {$t('words.practice')} +
- {/if} -
-
- - {$t('words.report')} - +
+ {#if logged_in} + + + + + {$t('words.download')} + + {:else} +
+ + + + + {$t('words.download')} + +
+ {/if} +
+
+
{#each quiz.questions as question, index_question} diff --git a/migrations/versions/7ad8502af419_added_kahoot_id.py b/migrations/versions/7ad8502af419_added_kahoot_id.py new file mode 100644 index 0000000..7b73038 --- /dev/null +++ b/migrations/versions/7ad8502af419_added_kahoot_id.py @@ -0,0 +1,29 @@ +"""Added kahoot_id + +Revision ID: 7ad8502af419 +Revises: 820e06ef2c2a +Create Date: 2023-01-27 22:30:00.331395 + +""" +from alembic import op +import sqlalchemy as sa +import ormar + + +# revision identifiers, used by Alembic. +revision = "7ad8502af419" +down_revision = "820e06ef2c2a" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column("quiz", sa.Column("kahoot_id", ormar.fields.sqlalchemy_uuid.CHAR(32), nullable=True)) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column("quiz", "kahoot_id") + # ### end Alembic commands ###