diff --git a/server/app/apis/components.py b/server/app/apis/components.py index 360463dc249eaef9662c99168eef960240bc7afb..73e2a9071967f484d677ccf7496057ace2ff1f44 100644 --- a/server/app/apis/components.py +++ b/server/app/apis/components.py @@ -1,7 +1,7 @@ import app.database.controller as dbc from app.apis import admin_required, item_response, list_response from app.core.dto import ComponentDTO -from app.core.parsers import competition_parser, competition_search_parser +from app.core.parsers import component_parser from app.database.models import Competition from flask.globals import request from flask_jwt_extended import jwt_required @@ -24,7 +24,14 @@ class ComponentByID(Resource): @api.route("/") @api.param("CID, SID") class ComponentList(Resource): + @jwt_required + def get(self, CID, SID): + items = dbc.get.component_list(SID) + return list_response(list_schema.dump(items)) + @jwt_required def post(self, CID, SID): - item = dbc.add.component(**request.args) + args = component_parser.parse_args() + item_slide = dbc.get.slide(CID, SID) + item = dbc.add.component(item_slide=item_slide, **args) return item_response(schema.dump(item)) diff --git a/server/app/core/parsers.py b/server/app/core/parsers.py index 7a1c6089c1893a01d70b858e13097eb84a74029b..a8136c9cfcb6437a6062f22acc3ac657f41deb0d 100644 --- a/server/app/core/parsers.py +++ b/server/app/core/parsers.py @@ -68,3 +68,13 @@ team_parser.add_argument("name", type=str, location="json") ###SEARCH_COMPETITION#### media_parser_search = search_parser.copy() media_parser_search.add_argument("filename", type=str, default=None, location="args") + + +###COMPONENT### +component_parser = reqparse.RequestParser() +component_parser.add_argument("x", type=str, default=None, location="json") +component_parser.add_argument("y", type=int, default=None, location="json") +component_parser.add_argument("w", type=int, default=None, location="json") +component_parser.add_argument("h", type=int, default=None, location="json") +component_parser.add_argument("data", type=dict, default=None, location="json") +component_parser.add_argument("type_id", type=int, default=None, location="json") diff --git a/server/app/core/schemas.py b/server/app/core/schemas.py index fca727b2ddfb589eb24b8e10a202d14034657315..2cd14a31649f2d34d3ac61efac46ce0e71e65bf3 100644 --- a/server/app/core/schemas.py +++ b/server/app/core/schemas.py @@ -124,6 +124,6 @@ class ComponentSchema(BaseSchema): y = ma.auto_field() w = ma.auto_field() h = ma.auto_field() + data = ma.auto_field() # TODO: Convert this to dict, or save as dict to begin with slide_id = ma.auto_field() - text = ma.auto_field() - image_id = ma.auto_field() + type_id = ma.auto_field() diff --git a/server/app/database/controller/add.py b/server/app/database/controller/add.py index 8d17cdb6d8df70284e5ba5af69c5a77491bc7fe9..162bbd8cff03b159b80adda2a8de957e9574bcbc 100644 --- a/server/app/database/controller/add.py +++ b/server/app/database/controller/add.py @@ -5,7 +5,6 @@ from app.database.models import ( City, Competition, Component, - ImageComponent, Media, MediaType, Question, @@ -13,7 +12,6 @@ from app.database.models import ( Role, Slide, Team, - TextComponent, User, ) from flask_restx import abort diff --git a/server/app/database/controller/get.py b/server/app/database/controller/get.py index 02e6df6adf0f238980dc4897888f8335d853968f..fa6914b2dacb9b050b4fbb15c4272a3c0525b5ca 100644 --- a/server/app/database/controller/get.py +++ b/server/app/database/controller/get.py @@ -1,12 +1,12 @@ -from app.database.models import Competition, Component, ImageComponent, Question, Slide, Team, TextComponent, User +from app.database.models import Competition, Component, Question, Slide, Team, User def user_exists(email): return User.query.filter(User.email == email).count() > 0 -def component(ID): - return Component.query.filter(Component.id == ID) +def component(ID, required=True, error_msg=None): + return Component.query.filter(Component.id == ID).first_extended(required, error_msg) def competition(CID, required=True, error_msg=None): @@ -55,5 +55,10 @@ def slide_list(CID): return Slide.query.filter(Slide.competition_id == CID).all() +def component_list(SID): + # TODO: Maybe take CID as argument and make sure that SID is in that competition? + return Component.query.filter(Component.slide_id == SID).all() + + def slide_count(CID): return Slide.query.filter(Slide.competition_id == CID).count() diff --git a/server/app/database/models.py b/server/app/database/models.py index cc492222bbbe4b57eb764c3b37396e58872df983..ec29860fa3151f9e46b994fba771e9435bc5413b 100644 --- a/server/app/database/models.py +++ b/server/app/database/models.py @@ -190,9 +190,8 @@ class Component(db.Model): y = db.Column(db.Integer, nullable=False, default=0) w = db.Column(db.Integer, nullable=False, default=1) h = db.Column(db.Integer, nullable=False, default=1) - data = db.Column(db.Text) + data = db.Column(db.Text) # TODO: Don't save this as text type_id = db.Column(db.Integer, db.ForeignKey("component_type.id"), nullable=False) - slide_id = db.Column(db.Integer, db.ForeignKey("slide.id"), nullable=False) def __init__(self, x, y, w, h, data, slide_id, type_id): @@ -200,7 +199,7 @@ class Component(db.Model): self.y = y self.w = w self.h = h - self.data = data + self.data = str(data) self.slide_id = slide_id self.type_id = type_id