39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
from flask_wtf import FlaskForm
|
|
from flask import current_app, request as http_request
|
|
|
|
from atst.utils.flash import formatted_flash as flash
|
|
|
|
|
|
class ValidatedForm(FlaskForm):
|
|
EMPTY_LIST_FIELD = ["", None]
|
|
|
|
def perform_extra_validation(self, *args, **kwargs):
|
|
"""Performs any applicable extra validation. Must
|
|
return True if the form is valid or False otherwise."""
|
|
return True
|
|
|
|
@property
|
|
def data(self):
|
|
_data = super().data
|
|
for field in _data:
|
|
if _data[field].__class__.__name__ == "list":
|
|
_data[field] = [
|
|
el for el in _data[field] if el not in self.EMPTY_LIST_FIELD
|
|
]
|
|
_data.pop("csrf_token", None)
|
|
return _data
|
|
|
|
def validate(self, *args, **kwargs):
|
|
valid = super().validate(*args, **kwargs)
|
|
if not valid:
|
|
flash("form_errors")
|
|
return valid
|
|
|
|
|
|
class CacheableForm(ValidatedForm):
|
|
def __init__(self, formdata=None, **kwargs):
|
|
formdata = formdata or {}
|
|
cached_data = current_app.form_cache.from_request(http_request)
|
|
cached_data.update(formdata)
|
|
super().__init__(cached_data, **kwargs)
|