diff --git a/server/app/database/controller/add.py b/server/app/database/controller/add.py index 128a3398aa375fb1b59732fc75e1349540e0826f..18bcd4b130a1f6aebd49df520c83e741a819d8cc 100644 --- a/server/app/database/controller/add.py +++ b/server/app/database/controller/add.py @@ -179,10 +179,13 @@ def _competition_no_slides(name, year, city_id, font=None): item_competition.font = font # Add code for Judge view - code(item_competition.id, 2) + code(2, item_competition.id) # Add code for Audience view - code(item_competition.id, 3) + code(3, item_competition.id) + + # Add code for Operator view + code(4, item_competition.id) item_competition = utils.refresh(item_competition) return item_competition diff --git a/server/app/database/controller/copy.py b/server/app/database/controller/copy.py index 9a1aa67168c1e232f8b2e0d2742de6bd1b87837e..08d743472035034b3b45eda3bdc6553f26377f60 100644 --- a/server/app/database/controller/copy.py +++ b/server/app/database/controller/copy.py @@ -103,7 +103,6 @@ def competition(item_competition_old): name = "Kopia av " + item_competition_old.name item_competition, total = search.competition(name=name) if item_competition: - print(f"{item_competition[total-1].name}, {total=}") name = "Kopia av " + item_competition[total - 1].name item_competition_new = add._competition_no_slides( diff --git a/server/app/database/controller/delete.py b/server/app/database/controller/delete.py index f3dc2dd51b680bc8834d9cf734001ac7291883d8..93a4c3393bf57fed22f2d672338f6cc592ecc543 100644 --- a/server/app/database/controller/delete.py +++ b/server/app/database/controller/delete.py @@ -56,10 +56,13 @@ def slide(item_slide): def team(item_team): - """ Deletes team and its question answers. """ + """ Deletes team, its question answers and the code. """ for item_question_answer in item_team.question_answers: question_answers(item_question_answer) + for item_code in item_team.code: + code(item_code) + default(item_team) @@ -86,12 +89,19 @@ def question_answers(item_question_answers): def competition(item_competition): - """ Deletes competition and its slides and teams. """ + """ Deletes competition, its slides, teams and codes. """ for item_slide in item_competition.slides: _slide(item_slide) for item_team in item_competition.teams: team(item_team) + for item_code in item_competition.codes: + code(item_code) - # TODO codes default(item_competition) + + +def code(item_code): + """ Deletes competition code. """ + + default(item_code) diff --git a/server/app/database/models.py b/server/app/database/models.py index 4bb95967aaeef34707e3defe7e38a173020dce30..0f909aee7f7ebf33efba3b5145601c9a6f9bced5 100644 --- a/server/app/database/models.py +++ b/server/app/database/models.py @@ -104,6 +104,7 @@ class Competition(db.Model): slides = db.relationship("Slide", backref="competition") teams = db.relationship("Team", backref="competition") + codes = db.relationship("Code", backref="competition") background_image = db.relationship("Media", uselist=False) @@ -121,6 +122,7 @@ class Team(db.Model): competition_id = db.Column(db.Integer, db.ForeignKey("competition.id"), nullable=False) question_answers = db.relationship("QuestionAnswer", backref="team") + code = db.relationship("Code", backref="team") def __init__(self, name, competition_id): self.name = name diff --git a/server/tests/test_db.py b/server/tests/test_db.py index dd13427c1c62689a88edefc96a6c16b8aa9db682..c504ed14ff39224f9bb76fe8fd32ca99e12dd725 100644 --- a/server/tests/test_db.py +++ b/server/tests/test_db.py @@ -3,10 +3,10 @@ This file tests the database controller functions. """ import app.database.controller as dbc -from app.database.models import City, Media, MediaType, Role, User +from app.database.models import City, Competition, Media, MediaType, Role, User, Code from tests import app, client, db -from tests.test_helpers import add_default_values, assert_exists, assert_insert_fail +from tests.test_helpers import add_default_values, assert_exists, assert_insert_fail, delete def test_user(client): @@ -58,7 +58,7 @@ def test_copy(client): item_slide_original = item_slides[0] # Inserts several copies of the same slide - num_copies = 10 + num_copies = 3 for _ in range(num_copies): item_slide_copy = dbc.copy.slide(item_slide_original) num_slides += 1 @@ -73,6 +73,23 @@ def test_copy(client): item_slide_original = item_competition_original.slides[order] check_slides_copy(item_slide_original, item_slide, num_slides, order) assert item_slide.competition_id != item_slide_original.competition_id + # TODO: Check that all codes are corectly created + + # Deleting competition deletes all corresponding codes + item_competitions = dbc.get.all(Competition) + for item_competition in item_competitions: + dbc.delete.competition(item_competition) + assert len(dbc.get.all(Code)) == 0 + + # Deleting team deletes the right code + item_competition = dbc.add.competition("tom", 1971, 1) + item_team_1 = dbc.add.team("Lag 1", item_competition.id) + item_team_2 = dbc.add.team("Lag 2", item_competition.id) + assert len(dbc.get.all(Code)) == 5 + dbc.delete.team(item_team_1) + assert len(dbc.get.all(Code)) == 4 + dbc.delete.team(item_team_2) + assert len(dbc.get.all(Code)) == 3 def check_slides_copy(item_slide_original, item_slide_copy, num_slides, order):