diff --git a/ref-test/app/models/entry.py b/ref-test/app/models/entry.py index bbbbb81..e9795fd 100644 --- a/ref-test/app/models/entry.py +++ b/ref-test/app/models/entry.py @@ -1,5 +1,4 @@ from ..extensions import db, mail -from ..tools.forms import JsonEncodedDict from ..tools.encryption import decrypt, encrypt from ..tools.logs import write from .test import Test @@ -8,6 +7,7 @@ from flask_login import current_user from flask_mail import Message from smtplib import SMTPException from sqlalchemy.exc import SQLAlchemyError +from sqlalchemy_json import MutableJson from datetime import datetime, timedelta from uuid import uuid4 @@ -25,8 +25,8 @@ class Entry(db.Model): end_time = db.Column(db.DateTime, nullable=True) status = db.Column(db.String(16), nullable=True) valid = db.Column(db.Boolean, default=True, nullable=True) - answers = db.Column(JsonEncodedDict, nullable=True) - result = db.Column(JsonEncodedDict, nullable=True) + answers = db.Column(MutableJson, nullable=True) + result = db.Column(MutableJson, nullable=True) def __repr__(self): return f' was added with .' diff --git a/ref-test/app/models/test.py b/ref-test/app/models/test.py index e2a53fc..8919157 100644 --- a/ref-test/app/models/test.py +++ b/ref-test/app/models/test.py @@ -1,9 +1,9 @@ from ..extensions import db -from ..tools.forms import JsonEncodedDict from ..tools.logs import write from flask_login import current_user from sqlalchemy.exc import SQLAlchemyError +from sqlalchemy_json import MutableJson from datetime import date, datetime import secrets @@ -18,7 +18,7 @@ class Test(db.Model): time_limit = db.Column(db.Integer, nullable=True) creator_id = db.Column(db.String(36), db.ForeignKey('user.id')) dataset_id = db.Column(db.String(36), db.ForeignKey('dataset.id')) - adjustments = db.Column(JsonEncodedDict, nullable=True) + adjustments = db.Column(MutableJson, nullable=True) entries = db.relationship('Entry', backref='test') def __repr__(self): diff --git a/ref-test/app/tools/forms.py b/ref-test/app/tools/forms.py index 7299647..f9b9fb1 100644 --- a/ref-test/app/tools/forms.py +++ b/ref-test/app/tools/forms.py @@ -9,24 +9,6 @@ from wtforms.validators import ValidationError import json from sqlalchemy.ext import mutable -class JsonEncodedDict(db.TypeDecorator): - """Enables JSON storage by encoding and decoding on the fly.""" - impl = db.Text - - def process_bind_param(self, value, dialect): - if value is None: - return '{}' - else: - return json.dumps(value) - - def process_result_value(self, value, dialect): - if value is None: - return {} - else: - return json.loads(value) - -mutable.MutableDict.associate_with(JsonEncodedDict) - def value(min:int=0, max:int=None): if not max: message = f'Value must be greater than {min}.' diff --git a/ref-test/requirements.txt b/ref-test/requirements.txt index 898dd2d..f55da7c 100644 --- a/ref-test/requirements.txt +++ b/ref-test/requirements.txt @@ -22,6 +22,7 @@ PyMySQL==1.0.2 python-dotenv==0.20.0 six==1.16.0 SQLAlchemy==1.4.40 +sqlalchemy-json==0.5.0 SQLAlchemy-Utils==0.38.3 visitor==0.1.3 Werkzeug==2.2.2