From b56c81e4222629969719ca08cda5bee4f90347b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20L=C3=B6fgren?= <viclo211@student.liu.se> Date: Wed, 28 Apr 2021 12:44:10 +0000 Subject: [PATCH] add: question component --- server/app/apis/auth.py | 7 +++++++ server/app/apis/components.py | 6 ++++-- server/app/core/schemas.py | 1 + server/app/database/controller/add.py | 10 ++++++++-- server/app/database/controller/copy.py | 7 +++++-- server/app/database/models.py | 9 ++++++++- server/app/database/types.py | 1 + 7 files changed, 34 insertions(+), 7 deletions(-) diff --git a/server/app/apis/auth.py b/server/app/apis/auth.py index d97eab2a..c09b0495 100644 --- a/server/app/apis/auth.py +++ b/server/app/apis/auth.py @@ -39,6 +39,13 @@ def get_code_claims(item_code): return {"view": item_code.view_type.name, "competition_id": item_code.competition_id, "team_id": item_code.team_id} +@api.route("/test") +class AuthSignup(Resource): + @protect_route(allowed_roles=["Admin"], allowed_views=["*"]) + def get(self): + return "ok" + + @api.route("/signup") class AuthSignup(Resource): @protect_route(allowed_roles=["Admin"]) diff --git a/server/app/apis/components.py b/server/app/apis/components.py index 32ab03c7..a1982763 100644 --- a/server/app/apis/components.py +++ b/server/app/apis/components.py @@ -18,7 +18,8 @@ component_parser_add.add_argument("h", type=int, default=1, location="json") component_parser_add.add_argument("type_id", type=int, required=True, location="json") component_parser_add.add_argument("view_type_id", type=int, required=True, location="json") component_parser_add.add_argument("text", type=str, default=None, location="json") -component_parser_add.add_argument("media_id", type=str, default=None, location="json") +component_parser_add.add_argument("media_id", type=int, default=None, location="json") +component_parser_add.add_argument("question_id", type=int, default=None, location="json") component_parser_edit = reqparse.RequestParser() component_parser_edit.add_argument("x", type=str, default=sentinel, location="json") @@ -26,7 +27,8 @@ component_parser_edit.add_argument("y", type=int, default=sentinel, location="js component_parser_edit.add_argument("w", type=int, default=sentinel, location="json") component_parser_edit.add_argument("h", type=int, default=sentinel, location="json") component_parser_edit.add_argument("text", type=str, default=sentinel, location="json") -component_parser_edit.add_argument("media_id", type=str, default=sentinel, location="json") +component_parser_edit.add_argument("media_id", type=int, default=sentinel, location="json") +component_parser_edit.add_argument("question_id", type=int, default=sentinel, location="json") @api.route("/<component_id>") diff --git a/server/app/core/schemas.py b/server/app/core/schemas.py index 31e79e69..4008b886 100644 --- a/server/app/core/schemas.py +++ b/server/app/core/schemas.py @@ -165,3 +165,4 @@ class ComponentSchema(BaseSchema): text = fields.fields.String() media = fields.Nested(MediaSchema, many=False) + question_id = fields.fields.Integer() \ No newline at end of file diff --git a/server/app/database/controller/add.py b/server/app/database/controller/add.py index 50321626..6c5ea14b 100644 --- a/server/app/database/controller/add.py +++ b/server/app/database/controller/add.py @@ -20,6 +20,7 @@ from app.database.models import ( Question, QuestionAlternative, QuestionAnswer, + QuestionComponent, QuestionType, Role, Slide, @@ -37,6 +38,8 @@ from sqlalchemy.orm import relation from sqlalchemy.orm.session import sessionmaker from flask import current_app +from app.database.types import ID_IMAGE_COMPONENT, ID_QUESTION_COMPONENT, ID_TEXT_COMPONENT + def db_add(item): """ @@ -79,12 +82,15 @@ def component(type_id, slide_id, view_type_id, x=0, y=0, w=0, h=0, **data): w *= ratio h *= ratio - if type_id == 1: + if type_id == ID_TEXT_COMPONENT: item = db_add(TextComponent(slide_id, type_id, view_type_id, x, y, w, h)) item.text = data.get("text") - elif type_id == 2: + elif type_id == ID_IMAGE_COMPONENT: item = db_add(ImageComponent(slide_id, type_id, view_type_id, x, y, w, h)) item.media_id = data.get("media_id") + elif type_id == ID_QUESTION_COMPONENT: + item = db_add(QuestionComponent(slide_id, type_id, view_type_id, x, y, w, h)) + item.question_id = data.get("question_id") else: abort(codes.BAD_REQUEST, f"Invalid type_id{type_id}") diff --git a/server/app/database/controller/copy.py b/server/app/database/controller/copy.py index 1874b5dc..a32ba1de 100644 --- a/server/app/database/controller/copy.py +++ b/server/app/database/controller/copy.py @@ -4,6 +4,7 @@ This file contains functionality to copy and duplicate data to the database. from app.database.controller import add, get, search, utils from app.database.models import Question +from app.database.types import ID_IMAGE_COMPONENT, ID_QUESTION_COMPONENT, ID_TEXT_COMPONENT def _alternative(item_old, question_id): @@ -38,10 +39,12 @@ def _component(item_component, item_slide_new): component item to the specified slide. """ data = {} - if item_component.type_id == 1: + if item_component.type_id == ID_TEXT_COMPONENT: data["text"] = item_component.text - elif item_component.type_id == 2: + elif item_component.type_id == ID_IMAGE_COMPONENT: data["media_id"] = item_component.media_id + elif item_component.type_id == ID_QUESTION_COMPONENT: + data["question_id"] = item_component.question_id add.component( item_component.type_id, item_slide_new.id, diff --git a/server/app/database/models.py b/server/app/database/models.py index c56aa3f0..f9f18438 100644 --- a/server/app/database/models.py +++ b/server/app/database/models.py @@ -1,7 +1,7 @@ from app.core import bcrypt, db from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property -from app.database.types import ID_IMAGE_COMPONENT, ID_TEXT_COMPONENT +from app.database.types import ID_IMAGE_COMPONENT, ID_QUESTION_COMPONENT, ID_TEXT_COMPONENT STRING_SIZE = 254 @@ -224,6 +224,13 @@ class ImageComponent(Component): __mapper_args__ = {"polymorphic_identity": ID_IMAGE_COMPONENT} +class QuestionComponent(Component): + question_id = db.Column(db.Integer, db.ForeignKey("question.id"), nullable=True) + + # __tablename__ = None + __mapper_args__ = {"polymorphic_identity": ID_QUESTION_COMPONENT} + + class Code(db.Model): id = db.Column(db.Integer, primary_key=True) code = db.Column(db.Text, unique=True) diff --git a/server/app/database/types.py b/server/app/database/types.py index 236e50f5..f53835ec 100644 --- a/server/app/database/types.py +++ b/server/app/database/types.py @@ -1,2 +1,3 @@ ID_TEXT_COMPONENT = 1 ID_IMAGE_COMPONENT = 2 +ID_QUESTION_COMPONENT = 3 \ No newline at end of file -- GitLab