from flask_wtf import FlaskForm from flask_wtf.file import FileField, FileAllowed from wtforms import validators from wtforms.validators import Length, ValidationError from wtforms import ( SubmitField, StringField, ) class UploadForm(FlaskForm): """File upload class for a new site in distribusi-verse""" def FileSizeLimit(max_size_in_mb): max_bytes = max_size_in_mb * 1024 * 1024 def file_length_check(form, field): if len(field.data.read()) > max_bytes: raise ValidationError( "File size must be less than {}MB".format(max_size_in_mb) ) return file_length_check sitename = StringField( "Name of your website:", validators=[validators.InputRequired(), Length(2, 100)], ) zipfile = FileField( "Upload your zip file with content here:", validators=[ FileAllowed(["zip"], "Zip archives only!"), FileSizeLimit(max_size_in_mb=100), ], ) submit = SubmitField("Upload")