Files
atst/atst/forms/forms.py
2019-03-06 15:36:53 -05:00

33 lines
1.0 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
EMPTY_LIST_FIELD = ["", None]
class BaseForm(FlaskForm):
def __init__(self, formdata=None, **kwargs):
# initialize the form with data from the cache
formdata = formdata or {}
cached_data = current_app.form_cache.from_request(http_request)
cached_data.update(formdata)
super().__init__(cached_data, **kwargs)
@property
def data(self):
# remove 'csrf_token' key/value pair
# remove empty strings and None from list fields
_data = super().data
_data.pop("csrf_token", None)
for field in _data:
if _data[field].__class__.__name__ == "list":
_data[field] = [el for el in _data[field] if el not in EMPTY_LIST_FIELD]
return _data
def validate(self, *args, **kwargs):
valid = super().validate(*args, **kwargs)
if not valid:
flash("form_errors")
return valid