diff --git a/server/app/database/controller/add.py b/server/app/database/controller/add.py index 3aa0ab709324e21ed69e8db5f1902f9447c9c5f3..9c8827e29fc4a4640085d54819c16ec29020a3dd 100644 --- a/server/app/database/controller/add.py +++ b/server/app/database/controller/add.py @@ -49,25 +49,34 @@ def db_add(item): except (exc.SQLAlchemyError, exc.DBAPIError): db.session.rollback() # SQL errors such as item already exists - abort(codes.INTERNAL_SERVER_ERROR, f"Item of type {type(item)} could not be created") + abort( + codes.INTERNAL_SERVER_ERROR, + f"Item of type {type(item)} could not be created", + ) except: db.session.rollback() # Catching other errors - abort(codes.INTERNAL_SERVER_ERROR, f"Something went wrong when creating {type(item)}") + abort( + codes.INTERNAL_SERVER_ERROR, + f"Something went wrong when creating {type(item)}", + ) return item def component(type_id, slide_id, view_type_id, x=0, y=0, w=0, h=0, **data): """ - Adds a component to the slide at the specified coordinates with the - provided size and data . + Adds a component to the slide at the specified + coordinates with the provided size and data. """ if type_id == 2: # 2 is image item_image = get.one(Media, data["media_id"]) filename = item_image.filename - path = os.path.join(current_app.config["UPLOADED_PHOTOS_DEST"], filename) + path = os.path.join( + current_app.config["UPLOADED_PHOTOS_DEST"], + filename, + ) with Image.open(path) as im: h = im.height w = im.width @@ -79,13 +88,19 @@ def component(type_id, slide_id, view_type_id, x=0, y=0, w=0, h=0, **data): h *= ratio if type_id == ID_TEXT_COMPONENT: - item = db_add(TextComponent(slide_id, type_id, view_type_id, x, y, w, h)) + item = db_add( + TextComponent(slide_id, type_id, view_type_id, x, y, w, h), + ) item.text = data.get("text") elif type_id == ID_IMAGE_COMPONENT: - item = db_add(ImageComponent(slide_id, type_id, view_type_id, x, y, w, h)) + 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 = 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}") @@ -258,8 +273,18 @@ def question(name, total_score, type_id, slide_id, correcting_instructions=None) def question_alternative(text, value, question_id): + """ + Adds a question alternative to the specified + question using the provided arguments. + """ + return db_add(QuestionAlternative(text, value, question_id)) def question_answer(answer, score, question_id, team_id): + """ + Adds a question answer to the specified team + and question using the provided arguments. + """ + return db_add(QuestionAnswer(answer, score, question_id, team_id)) diff --git a/server/app/database/controller/copy.py b/server/app/database/controller/copy.py index 48dab2db79c33cb1ca819a59f0e934ab23dcc191..dd8073342ecfb0460e54783e70e50c0e194d437a 100644 --- a/server/app/database/controller/copy.py +++ b/server/app/database/controller/copy.py @@ -8,7 +8,9 @@ from app.database.types import ID_IMAGE_COMPONENT, ID_QUESTION_COMPONENT, ID_TEX def _alternative(item_old, question_id): - """Internal function. Makes a copy of the provided question alternative""" + """ + Internal function. Makes a copy of the provided question alternative. + """ return add.question_alternative(item_old.text, item_old.value, question_id) @@ -73,7 +75,7 @@ def component(item_component, slide_id_new, view_type_id): def slide(item_slide_old): """ Deep copies a slide to the same competition. - Does not copy team, question answers. + Does not copy team and question answers. """ item_competition = get.competition(item_slide_old.competition_id) @@ -98,7 +100,6 @@ def slide_to_competition(item_slide_old, item_competition): for item_component in item_slide_old.components: _component(item_component, item_slide_new) - for item_question in item_slide_old.questions: _question(item_question, item_slide_new.id) @@ -123,7 +124,7 @@ def competition(item_competition_old): item_competition_old.city_id, item_competition_old.font, ) - # TODO: Add background image + item_competition_new.background_image_id = item_competition_old.background_image_id for item_slide in item_competition_old.slides: diff --git a/server/app/database/controller/delete.py b/server/app/database/controller/delete.py index b0b36fbaf79843eac05bd4340d0a633a61e325d0..65737cb69d3ace8af493d71d245805b8f4869446 100644 --- a/server/app/database/controller/delete.py +++ b/server/app/database/controller/delete.py @@ -11,19 +11,25 @@ from flask_restx import abort def default(item): """ Deletes item and commits. """ + try: db.session.delete(item) db.session.commit() except: db.session.rollback() - abort(codes.INTERNAL_SERVER_ERROR, f"Item of type {type(item)} could not be deleted") + abort( + codes.INTERNAL_SERVER_ERROR, + f"Item of type {type(item)} could not be deleted", + ) def whitelist_to_blacklist(filters): """ - Remove whitelist by condition(filters) and insert those into blacklist - Example: When delete user all whitelisted tokens for that user should be blacklisted + Remove whitelist by condition(filters) and insert those into blacklist. + Example: When delete user all whitelisted tokens for that user should + be blacklisted. """ + whitelist = Whitelist.query.filter(filters).all() for item in whitelist: dbc.add.blacklist(item.jti) @@ -43,7 +49,6 @@ def _slide(item_slide): for item_question in item_slide.questions: question(item_question) - for item_component in item_slide.components: default(item_component) @@ -85,6 +90,7 @@ def question(item_question): question_answers(item_question_answer) for item_alternative in item_question.alternatives: alternatives(item_alternative) + default(item_question) diff --git a/server/app/database/controller/utils.py b/server/app/database/controller/utils.py index 14eaa48d0295515e3f53c93dae963eb0ebc6bc92..f27ce32bae1429ec606f1b895535f3e0aea74d50 100644 --- a/server/app/database/controller/utils.py +++ b/server/app/database/controller/utils.py @@ -10,6 +10,8 @@ from flask_restx import abort def move_slides(item_competition, start_order, end_order): + """ Changes a slide order and then arranges other affected slides. """ + slides = item_competition.slides # Move up if start_order < end_order: @@ -40,6 +42,7 @@ def generate_unique_code(): def refresh(item): """ Refreshes the provided item. """ + try: db.session.refresh(item) except Exception as e: @@ -49,7 +52,8 @@ def refresh(item): def commit(): - """ Commits. """ + """ Commits to the database. """ + try: db.session.commit() except Exception as e: