ska-referee-test/ref-test/app/tools/forms.py

42 lines
1.2 KiB
Python
Raw Normal View History

from ..tools.logs import write
2022-06-12 21:03:51 +01:00
2022-06-20 11:27:05 +01:00
from flask import jsonify
2022-06-12 21:03:51 +01:00
from wtforms.validators import ValidationError
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)
return length
def get_time_options():
time_options = [
('none', 'None'),
('60', '1 hour'),
('90', '1 hour 30 minutes'),
('120', '2 hours')
]
return time_options
def get_dataset_choices():
2022-06-15 23:54:44 +01:00
from ..models import Dataset
try: datasets = Dataset.query.all()
2022-08-20 14:47:46 +01:00
except Exception as exception:
write('system.log', f'Database error when fetching dataset lists: {exception}')
return []
dataset_choices = []
for dataset in datasets:
2022-06-22 01:56:13 +01:00
label = dataset.get_name()
label = f'{label} (Default)' if dataset.default else label
2022-06-15 23:54:44 +01:00
choice = (dataset.id, label)
dataset_choices.append(choice)
2022-06-20 11:27:05 +01:00
return dataset_choices
def send_errors_to_client(form):
2022-08-11 16:58:00 +01:00
errors = [*form.errors.values()]
2022-06-20 11:27:05 +01:00
return jsonify({ 'error': errors}), 400