From 819d443d05a7ee10c6b026fe894c15d7dac4c47f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Victor=20L=C3=B6fgren?= <viclo211@student.liu.se>
Date: Wed, 14 Apr 2021 16:28:42 +0200
Subject: [PATCH] Make data dict type

---
 server/app/core/schemas.py            |  2 +-
 server/app/database/controller/add.py |  2 +-
 server/app/database/models.py         | 25 ++++++++++++++++++++++---
 3 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/server/app/core/schemas.py b/server/app/core/schemas.py
index 2cd14a31..058efed7 100644
--- a/server/app/core/schemas.py
+++ b/server/app/core/schemas.py
@@ -124,6 +124,6 @@ class ComponentSchema(BaseSchema):
     y = ma.auto_field()
     w = ma.auto_field()
     h = ma.auto_field()
-    data = ma.auto_field()  # TODO: Convert this to dict, or save as dict to begin with
+    data = ma.auto_field()  # TODO: Convert this to dict
     slide_id = ma.auto_field()
     type_id = ma.auto_field()
diff --git a/server/app/database/controller/add.py b/server/app/database/controller/add.py
index 162bbd8c..9e2a8e11 100644
--- a/server/app/database/controller/add.py
+++ b/server/app/database/controller/add.py
@@ -33,7 +33,7 @@ def db_add(func):
 
 
 @db_add
-def component(x, y, w, h, data, type_id, item_slide):
+def component(x, y, w, h, data, item_slide, type_id):
     return Component(x, y, w, h, data, item_slide.id, type_id)
 
 
diff --git a/server/app/database/models.py b/server/app/database/models.py
index ec29860f..29cbe3b5 100644
--- a/server/app/database/models.py
+++ b/server/app/database/models.py
@@ -1,6 +1,9 @@
+import json
+
 from app.core import bcrypt, db
 from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property
 from sqlalchemy.orm import backref
+from sqlalchemy.types import TypeDecorator
 
 STRING_SIZE = 254
 
@@ -183,6 +186,22 @@ class QuestionAnswer(db.Model):
         self.team_id = team_id
 
 
+class Dictionary(TypeDecorator):
+
+    impl = db.Text(1024)
+
+    def process_bind_param(self, value, dialect):
+        if value is not None:
+            value = json.dumps(value).replace("'", '"')
+
+        return value
+
+    def process_result_value(self, value, dialect):
+        if value is not None:
+            value = json.loads(value)
+        return value
+
+
 class Component(db.Model):
     # __mapper_args__ = {"polymorphic_on": type, "polymorphic_identity": "component"}
     id = db.Column(db.Integer, primary_key=True)
@@ -190,16 +209,16 @@ class Component(db.Model):
     y = db.Column(db.Integer, nullable=False, default=0)
     w = db.Column(db.Integer, nullable=False, default=1)
     h = db.Column(db.Integer, nullable=False, default=1)
-    data = db.Column(db.Text)  # TODO: Don't save this as text
-    type_id = db.Column(db.Integer, db.ForeignKey("component_type.id"), nullable=False)
+    data = db.Column(Dictionary())
     slide_id = db.Column(db.Integer, db.ForeignKey("slide.id"), nullable=False)
+    type_id = db.Column(db.Integer, db.ForeignKey("component_type.id"), nullable=False)
 
     def __init__(self, x, y, w, h, data, slide_id, type_id):
         self.x = x
         self.y = y
         self.w = w
         self.h = h
-        self.data = str(data)
+        self.data = data
         self.slide_id = slide_id
         self.type_id = type_id
 
-- 
GitLab