distribusi-verse/verse/forms/uploadform.py

88 lines
2.3 KiB
Python
Raw Normal View History

from flask_wtf import FlaskForm
2024-04-21 20:44:08 +02:00
from flask_wtf.file import FileAllowed, FileField, FileRequired, FileSize
from wtforms import (
IntegerField,
SelectField,
StringField,
SubmitField,
validators,
)
from wtforms.validators import (
DataRequired,
Length,
NumberRange,
ValidationError,
)
2024-04-28 13:04:07 +02:00
from app import settings
class UploadForm(FlaskForm):
"""File upload class for a new site in distribusi-verse"""
2024-04-22 20:34:33 +02:00
def distribusiname(form, field):
2022-03-04 22:15:08 +01:00
if field.data.lower() == "new":
raise ValidationError("Name has to be unique and not just new.")
2024-04-22 20:34:33 +02:00
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
2024-04-22 20:59:26 +02:00
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(
2024-04-19 22:33:52 +02:00
"Name of your archive section:",
2024-04-21 20:44:08 +02:00
validators=[
validators.InputRequired(),
Length(2, 100),
2024-04-22 20:34:33 +02:00
distribusiname,
2024-04-21 20:44:08 +02:00
],
)
2024-04-19 22:55:07 +02:00
year = SelectField(
"Year:",
validate_choice=True,
coerce=str,
2024-04-22 20:59:26 +02:00
choices=year_choices,
option_widget=None,
2024-04-21 20:44:08 +02:00
validators=[DataRequired()],
)
2024-04-19 22:55:07 +02:00
category = SelectField(
2024-04-19 22:33:52 +02:00
"Category:",
validate_choice=True,
coerce=str,
2024-04-22 20:34:33 +02:00
choices=category_choices,
option_widget=None,
2024-04-21 20:44:08 +02:00
validators=[DataRequired()],
)
2024-04-27 11:26:19 +02:00
tags = StringField(
2022-03-25 12:29:02 +01:00
"Add tags, seperated by commas. No need for the '#' sign:",
validators=[validators.InputRequired(), Length(2, 500)],
)
zipfile = FileField(
"Upload your zip file with content here:",
validators=[
FileAllowed(["zip"], "Zip archives only!"),
FileRequired(),
FileSize(
2024-04-27 11:26:19 +02:00
max_size=1073741824,
message="Zipfile size must be smaller than 100MB",
),
],
)
submit = SubmitField("Upload")