mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-15 01:05:42 +00:00
27 lines
750 B
Python
27 lines
750 B
Python
from flask_wtf import FlaskForm
|
|
from wtforms.validators import ValidationError
|
|
import re
|
|
|
|
|
|
form_prefix_pattern = re.compile(r'(?<!^)(?=[A-Z])')
|
|
|
|
|
|
def LimitFileSize(max_size_mb):
|
|
max_size_b = max_size_mb * 1024 * 1024
|
|
def file_length_check(form, field):
|
|
if len(field.data.read()) >= max_size_b:
|
|
raise ValidationError(
|
|
f'File size must be less or equal than {max_size_mb} MB'
|
|
)
|
|
field.data.seek(0)
|
|
return file_length_check
|
|
|
|
|
|
class NopaqueForm(FlaskForm):
|
|
def __init__(self, *args, **kwargs):
|
|
if 'prefix' not in kwargs:
|
|
kwargs['prefix'] = \
|
|
form_prefix_pattern.sub('-', self.__class__.__name__).lower()
|
|
super().__init__(*args, **kwargs)
|
|
|