2022-06-12 21:03:51 +01:00
|
|
|
|
2022-06-15 11:23:38 +01:00
|
|
|
from ..models import Dataset
|
2022-06-12 21:03:51 +01:00
|
|
|
from ..modules import db
|
|
|
|
|
|
|
|
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}.'
|
|
|
|
else:
|
|
|
|
message = f'Value must be between {min} and {max}.'
|
|
|
|
def length(form, field):
|
|
|
|
value = field.data or 0
|
|
|
|
if value < min or max != None and value > max:
|
|
|
|
raise ValidationError(message)
|
2022-06-14 22:55:11 +01:00
|
|
|
return length
|
|
|
|
|
|
|
|
def get_time_options():
|
|
|
|
time_options = [
|
|
|
|
('none', 'None'),
|
|
|
|
('60', '1 hour'),
|
|
|
|
('90', '1 hour 30 minutes'),
|
|
|
|
('120', '2 hours')
|
|
|
|
]
|
2022-06-15 11:23:38 +01:00
|
|
|
return time_options
|
|
|
|
|
|
|
|
def get_dataset_choices():
|
|
|
|
datasets = Dataset.query.all()
|
|
|
|
dataset_choices = []
|
|
|
|
for dataset in datasets:
|
|
|
|
label = dataset['date'].strftime('%Y%m%d%H%M%S')
|
|
|
|
label = f'{label} (Default)' if dataset.default else label
|
|
|
|
choice = (dataset['id'], label)
|
|
|
|
dataset_choices.append(choice)
|
|
|
|
return dataset_choices
|