diff --git a/server/app/core/schemas.py b/server/app/core/schemas.py index 2cd14a31649f2d34d3ac61efac46ce0e71e65bf3..058efed71f955df963ae9fd0429c77ab4d18b95a 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 162bbd8cff03b159b80adda2a8de957e9574bcbc..9e2a8e1101f638733af40516dee888e1a24656e2 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 ec29860fa3151f9e46b994fba771e9435bc5413b..29cbe3b541f7f980472fef7f682e8ffdb0bbf8e6 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