diff --git a/server/app/database/controller/copy.py b/server/app/database/controller/copy.py new file mode 100644 index 0000000000000000000000000000000000000000..ef6f0ecff5304fcfd037e7e39045fd9dde4724a8 --- /dev/null +++ b/server/app/database/controller/copy.py @@ -0,0 +1,75 @@ +""" +This file contains functionality to copy and duplicate data to the database. +""" + +from app.database.controller import add, utils +from app.database.models import Question, Slide +from flask_sqlalchemy import utils + + +def _question(item_question_old, slide_id): + """ + Internal function. Makes a copy of the provided question item to the + specified slide. Does not copy team, question answers or alternatives. + """ + + item_question_new = add.db_add( + Question( + item_question_old.name, + item_question_old.total_score, + item_question_old.type_id, + slide_id, + ) + ) + + # TODO: Add question alternatives + # for item_alternatives in item_question_old.alternatives: + # dbc.add.alternatives() + + return item_question_new + + +def _component(item_component, item_slide_new): + """ + Internal function. Makes a copy of the provided + component item to the specified slide. + """ + + add.component( + item_component.type_id, + item_slide_new, + item_component.data, + item_component.x, + item_component.y, + item_component.w, + item_component.h, + ) + + +def slide(item_slide_old): + """ + Deep copies a slide to the same competition. + Does not copy team, question answers or alternatives. + """ + + order = Slide.query.filter( + Slide.competition_id == item_slide_old.item_competition.id + ).count() # first element has index 0 + item_slide_new = add.db_add(Slide(order, item_slide_old.item_competition.id)) + + # Copy all fields + item_slide_new.title = item_slide_old.title + item_slide_new.body = item_slide_old.body + item_slide_new.timer = item_slide_old.timer + item_slide_new.settings = item_slide_old.settings + + # TODO: Add background image + + for item_component in item_slide_old.components: + _component(item_slide_new, item_component) + + for item_question in item_slide_old.questions: + _question(item_question, item_slide_new.id) + + utils.commit_and_refresh(item_slide_new) + return item_slide_new