diff --git a/server/app/apis/components.py b/server/app/apis/components.py index d301ff53b5e327f80388c9a1f4699bb61680b0ed..d0cce56595a969f31e0f384cf28987de86708203 100644 --- a/server/app/apis/components.py +++ b/server/app/apis/components.py @@ -25,7 +25,7 @@ class ComponentByID(Resource): def put(self, CID, SOrder, component_id): args = component_parser.parse_args() item = dbc.get.one(Component, component_id) - item = dbc.edit.component(item,**args) + item = dbc.edit.component(item, **args) return item_response(schema.dump(item)) @jwt_required diff --git a/server/app/apis/teams.py b/server/app/apis/teams.py index 6729ccf8984e6ba85de5b79e2cfa0c3edde0f4ee..913e06fba6d2faf3657ebdfc4e275f66def4e0bd 100644 --- a/server/app/apis/teams.py +++ b/server/app/apis/teams.py @@ -23,7 +23,7 @@ class TeamsList(Resource): @jwt_required def post(self, CID): args = team_parser.parse_args(strict=True) - item_comp = dbc.get.one(Competition,CID) + item_comp = dbc.get.one(Competition, CID) item_team = dbc.add.team(args["name"], item_comp) return item_response(schema.dump(item_team)) diff --git a/server/app/database/controller/get.py b/server/app/database/controller/get.py index 269640b3c9af00d03aaa9d1a7d132154f4af66e2..c290b38ae7ba3499fbcbadb7563b4755f71ed38e 100644 --- a/server/app/database/controller/get.py +++ b/server/app/database/controller/get.py @@ -1,49 +1,84 @@ +""" +This file contains functionality to get data from the database. +""" + from app.core import db -from app.database.models import (City, Code, Competition, Component, - ComponentType, MediaType, Question, - QuestionType, Role, Slide, Team, User, - ViewType) +from app.database.models import ( + City, + Code, + Competition, + Component, + ComponentType, + MediaType, + Question, + QuestionType, + Role, + Slide, + Team, + User, + ViewType, +) def all(db_type): + """ Gets all elements in the provided table. """ + return db_type.query.all() def one(db_type, id, required=True, error_msg=None): + """ Gets the element in the table that has the same id. """ + return db_type.query.filter(db_type.id == id).first_extended(required, error_msg) def user_exists(email): + """ Checks if an user has that email. """ + return User.query.filter(User.email == email).count() > 0 + def code_by_code(code): + """ Gets the code object associated with the provided code. """ + return Code.query.filter(Code.code == code.upper()).first() def user(UID, required=True, error_msg=None): + """ Gets the user object associated with the provided id. """ + return User.query.filter(User.id == UID).first_extended(required, error_msg) def user_by_email(email, required=True, error_msg=None): + """ Gets the user object associated with the provided email. """ + return User.query.filter(User.email == email).first_extended(required, error_msg) def slide(CID, SOrder, required=True, error_msg=None): + """ Gets the slide object associated with the provided id and order. """ + filters = (Slide.competition_id == CID) & (Slide.order == SOrder) return Slide.query.filter(filters).first_extended(required, error_msg) def team(CID, TID, required=True, error_msg=None): + """ Gets the team object associated with the provided id and competition id. """ + return Team.query.filter((Team.competition_id == CID) & (Team.id == TID)).first_extended(required, error_msg) def question(CID, SOrder, QID, required=True, error_msg=None): + """ Gets the question object associated with the provided id, slide order and competition id. """ + join_filters = (Slide.competition_id == CID) & (Slide.order == SOrder) & (Slide.id == Question.slide_id) return Question.query.join(Slide, join_filters).filter(Question.id == QID).first_extended(required, error_msg) - def code_list(competition_id): + """ Gets a list of all code objects associated with a the provided competition. """ + team_view_id = 1 join_filters = (Code.view_type_id == team_view_id) & (Team.id == Code.pointer) filters = ((Code.view_type_id != team_view_id) & (Code.pointer == competition_id))( @@ -53,22 +88,32 @@ def code_list(competition_id): def question_list(CID): + """ Gets a list of all question objects associated with a the provided competition. """ + join_filters = (Slide.competition_id == CID) & (Slide.id == Question.slide_id) return Question.query.join(Slide, join_filters).all() def component_list(CID, SOrder): + """ Gets a list of all component objects associated with a the provided competition id and slide order. """ + join_filters = (Slide.competition_id == CID) & (Slide.order == SOrder) & (Component.slide_id == Slide.id) return Component.query.join(Slide, join_filters).all() def team_list(CID): + """ Gets a list of all team objects associated with a the provided competition. """ + return Team.query.filter(Team.competition_id == CID).all() def slide_list(CID): + """ Gets a list of all slide objects associated with a the provided competition. """ + return Slide.query.filter(Slide.competition_id == CID).all() def slide_count(CID): + """ Gets the number of slides in the provided competition. """ + return Slide.query.filter(Slide.competition_id == CID).count()