From 6c015dd1cb088656d666fe7af85ffaaf6b629f2a Mon Sep 17 00:00:00 2001
From: robban64 <carl@schonfelder.se>
Date: Fri, 23 Apr 2021 15:06:11 +0200
Subject: [PATCH] fix: replaced code.pointer with competition_id and add
 team_id to code

---
 server/app/core/schemas.py            |  4 +++-
 server/app/database/controller/add.py | 11 +++++------
 server/app/database/controller/get.py |  2 +-
 server/app/database/models.py         | 10 +++++-----
 4 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/server/app/core/schemas.py b/server/app/core/schemas.py
index 79bdbc1c..378f99af 100644
--- a/server/app/core/schemas.py
+++ b/server/app/core/schemas.py
@@ -1,3 +1,4 @@
+from marshmallow.decorators import pre_dump
 import app.database.models as models
 from app.core import ma
 from marshmallow_sqlalchemy import fields
@@ -37,8 +38,9 @@ class CodeSchema(IdNameSchema):
 
     id = ma.auto_field()
     code = ma.auto_field()
-    pointer = ma.auto_field()
     view_type_id = ma.auto_field()
+    competition_id = fields.fields.Integer()
+    team_id = fields.fields.Integer()
 
 
 class ViewTypeSchema(IdNameSchema):
diff --git a/server/app/database/controller/add.py b/server/app/database/controller/add.py
index 1dbcf442..9ab17ce9 100644
--- a/server/app/database/controller/add.py
+++ b/server/app/database/controller/add.py
@@ -131,11 +131,11 @@ def question_answer(data, score, question_id, team_id):
     return db_add(QuestionAnswer(data, score, question_id, team_id))
 
 
-def code(pointer, view_type_id):
+def code(view_type_id, competition_id=None, team_id=None):
     """ Adds a code to the database using the provided arguments. """
 
     code_string = utils.generate_unique_code()
-    return db_add(Code(code_string, pointer, view_type_id))
+    return db_add(Code(code_string, view_type_id, competition_id, team_id))
 
 
 def team(name, competition_id):
@@ -144,7 +144,7 @@ def team(name, competition_id):
     item = db_add(Team(name, competition_id))
 
     # Add code for the team
-    code(item.id, 1)
+    code(1, competition_id, item.id)
 
     return item
 
@@ -189,10 +189,9 @@ def competition(name, year, city_id):
     slide(item_competition.id)
 
     # 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)
 
     item_competition = utils.refresh(item_competition)
     return item_competition
diff --git a/server/app/database/controller/get.py b/server/app/database/controller/get.py
index 96405b5a..eb0a7711 100644
--- a/server/app/database/controller/get.py
+++ b/server/app/database/controller/get.py
@@ -40,7 +40,7 @@ def code_by_code(code):
 def code_list(competition_id):
     """ Gets a list of all code objects associated with a the provided competition. """
     # team_view_id = 1
-    join_competition = Competition.id == Code.pointer
+    join_competition = Competition.id == Code.competition_id
     filters = Competition.id == competition_id
     return Code.query.join(Competition, join_competition).filter(filters).all()
 
diff --git a/server/app/database/models.py b/server/app/database/models.py
index cded9ddf..7174080b 100644
--- a/server/app/database/models.py
+++ b/server/app/database/models.py
@@ -205,17 +205,17 @@ class Component(db.Model):
 
 
 class Code(db.Model):
-    table_args = (db.UniqueConstraint("pointer", "type"),)
     id = db.Column(db.Integer, primary_key=True)
     code = db.Column(db.Text, unique=True)
-    pointer = db.Column(db.Integer, nullable=False)
-
     view_type_id = db.Column(db.Integer, db.ForeignKey("view_type.id"), nullable=False)
+    competition_id = db.Column(db.Integer, db.ForeignKey("competition.id"), nullable=False)
+    team_id = db.Column(db.Integer, db.ForeignKey("team.id"), nullable=True)
 
-    def __init__(self, code, pointer, view_type_id):
+    def __init__(self, code, view_type_id, competition_id=None, team_id=None):
         self.code = code
-        self.pointer = pointer
         self.view_type_id = view_type_id
+        self.competition_id = competition_id
+        self.team_id = team_id
 
 
 class ViewType(db.Model):
-- 
GitLab