distribusi-verse/verse/distribusikan/forms/uploadform.py

86 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 (
2024-05-28 23:02:17 +02:00
SelectField,
StringField,
SubmitField,
validators,
)
from wtforms.validators import (
2024-05-28 23:02:17 +02:00
DataRequired,
Length,
ValidationError,
)
2024-04-28 13:04:07 +02:00
from app import settings
class UploadForm(FlaskForm):
2024-05-28 23:02:17 +02:00
"""File upload class for a new site in distribusi-verse"""
2024-05-28 23:02:17 +02:00
def distribusiname(form, field):
if field.data.lower() == "new":
raise ValidationError("Name has to be unique and not just new.")
2022-03-04 22:15:08 +01:00
2024-05-28 23:02:17 +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:34:33 +02:00
2024-05-28 23:02:17 +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
2024-04-22 20:59:26 +02:00
2024-05-28 23:02:17 +02:00
sitename = StringField(
"Name of your archive section:",
validators=[
validators.InputRequired(),
Length(2, 100),
distribusiname,
],
)
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()],
)
2024-04-27 11:26:19 +02:00
2024-05-28 23:02:17 +02:00
tags = StringField(
"Add search tags, seperated by commas. No need for the '#' sign:",
validators=[validators.InputRequired(), Length(3, 500)],
)
2024-05-28 23:02:17 +02:00
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 100MB",
),
],
)
2024-05-28 23:02:17 +02:00
submit = SubmitField("Upload")