from flask_wtf import FlaskForm from flask_wtf.file import FileAllowed, FileField, FileRequired, FileSize from wtforms import ( SelectField, StringField, SubmitField, validators, ) from wtforms.validators import ( DataRequired, Length, ValidationError, ) from wtforms.widgets import TextArea from app import settings class UploadForm(FlaskForm): """File upload class for a new site in distribusi-verse""" def distribusiname(form, field): if field.data.lower() == "new": raise ValidationError("Name has to be unique and not just new.") def category_choices(): APP = settings() config_categories = APP.config["categories"] categories = [] for config_category in config_categories: categories.append((config_category, config_category)) return categories def year_choices(): APP = settings() start_time = APP.config["start_time"] end_time = APP.config["end_time"] year_range = range(start_time.year, end_time.year, 1) year_choices = [] for year in year_range: year_choices.append((str(year), str(year))) return year_choices sitename = StringField( "Name of your archive section:", validators=[ validators.InputRequired(), Length(2, 100), distribusiname, ], ) description = StringField( "Description of this distribusi:", validators=[ Length(10, 32000), ], widget=TextArea(), ) year = SelectField( "Year:", validate_choice=True, coerce=str, choices=year_choices, option_widget=None, validators=[DataRequired()], ) category = SelectField( "Category:", validate_choice=True, coerce=str, choices=category_choices, option_widget=None, validators=[DataRequired()], ) tags = StringField( "Add search tags, seperated by commas. No need for the '#' sign:", validators=[validators.InputRequired(), Length(3, 500)], ) zipfile = FileField( "Upload your zip file with content here:", validators=[ FileAllowed(["zip"], "Zip archives only!"), FileRequired(), FileSize( max_size=1073741824, message="Zipfile size must be smaller than 1024MB", ), ], ) submit = SubmitField("Upload")