Updated SQL Json support

This commit is contained in:
Vivek Santayana 2022-08-20 13:01:32 +01:00
parent 62fac48904
commit d9837246de
4 changed files with 6 additions and 23 deletions

View File

@ -1,5 +1,4 @@
from ..extensions import db, mail from ..extensions import db, mail
from ..tools.forms import JsonEncodedDict
from ..tools.encryption import decrypt, encrypt from ..tools.encryption import decrypt, encrypt
from ..tools.logs import write from ..tools.logs import write
from .test import Test from .test import Test
@ -8,6 +7,7 @@ from flask_login import current_user
from flask_mail import Message from flask_mail import Message
from smtplib import SMTPException from smtplib import SMTPException
from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy_json import MutableJson
from datetime import datetime, timedelta from datetime import datetime, timedelta
from uuid import uuid4 from uuid import uuid4
@ -25,8 +25,8 @@ class Entry(db.Model):
end_time = db.Column(db.DateTime, nullable=True) end_time = db.Column(db.DateTime, nullable=True)
status = db.Column(db.String(16), nullable=True) status = db.Column(db.String(16), nullable=True)
valid = db.Column(db.Boolean, default=True, nullable=True) valid = db.Column(db.Boolean, default=True, nullable=True)
answers = db.Column(JsonEncodedDict, nullable=True) answers = db.Column(MutableJson, nullable=True)
result = db.Column(JsonEncodedDict, nullable=True) result = db.Column(MutableJson, nullable=True)
def __repr__(self): def __repr__(self):
return f'<New entry by {self.first_name} {self.surname}> was added with <id {self.id}>.' return f'<New entry by {self.first_name} {self.surname}> was added with <id {self.id}>.'

View File

@ -1,9 +1,9 @@
from ..extensions import db from ..extensions import db
from ..tools.forms import JsonEncodedDict
from ..tools.logs import write from ..tools.logs import write
from flask_login import current_user from flask_login import current_user
from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy_json import MutableJson
from datetime import date, datetime from datetime import date, datetime
import secrets import secrets
@ -18,7 +18,7 @@ class Test(db.Model):
time_limit = db.Column(db.Integer, nullable=True) time_limit = db.Column(db.Integer, nullable=True)
creator_id = db.Column(db.String(36), db.ForeignKey('user.id')) creator_id = db.Column(db.String(36), db.ForeignKey('user.id'))
dataset_id = db.Column(db.String(36), db.ForeignKey('dataset.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') entries = db.relationship('Entry', backref='test')
def __repr__(self): def __repr__(self):

View File

@ -9,24 +9,6 @@ from wtforms.validators import ValidationError
import json import json
from sqlalchemy.ext import mutable 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): def value(min:int=0, max:int=None):
if not max: if not max:
message = f'Value must be greater than {min}.' message = f'Value must be greater than {min}.'

View File

@ -22,6 +22,7 @@ PyMySQL==1.0.2
python-dotenv==0.20.0 python-dotenv==0.20.0
six==1.16.0 six==1.16.0
SQLAlchemy==1.4.40 SQLAlchemy==1.4.40
sqlalchemy-json==0.5.0
SQLAlchemy-Utils==0.38.3 SQLAlchemy-Utils==0.38.3
visitor==0.1.3 visitor==0.1.3
Werkzeug==2.2.2 Werkzeug==2.2.2