theraPy/forms/SearchForm.py

52 lines
2.3 KiB
Python
Raw Normal View History

2024-08-26 21:05:34 +02:00
from wtforms import Form, StringField, validators
from wtforms.fields.choices import SelectMultipleField, RadioField
2024-08-27 23:09:19 +02:00
from wtforms.fields.numeric import IntegerField
from wtforms.widgets.core import CheckboxInput, ListWidget, Input
2024-08-26 21:05:34 +02:00
def validate_therapy_types(form, field):
valid_choices = {"A", "S", "T", "V"}
if not all(choice in valid_choices for choice in field.data):
raise validators.ValidationError("Invalid value in therapy types selection.")
def validate_therapy_phone_times(form, field):
valid_choices = {1, 2, 3, 4}
if not field.data:
raise validators.ValidationError("Please select at least one option.")
if not all(choice in valid_choices for choice in field.data):
raise validators.ValidationError("Invalid value in therapy phone types selection.")
2024-08-27 23:09:19 +02:00
class RangeInput(Input):
input_type = "range"
2024-08-26 21:05:34 +02:00
class SearchForm(Form):
location = StringField("Standort/PLZ", validators=[validators.InputRequired(), validators.Length(min=2, max=50),
2024-08-28 17:17:59 +02:00
validators.Regexp("^[a-zA-ZäöüßÄÖÜẞ0-9]+$")])
2024-08-27 23:09:19 +02:00
therapy_distance = IntegerField(
"Distanz (in km)",
widget=RangeInput(),
default=25,
validators=[validators.InputRequired(), validators.NumberRange(min=0, max=50)]
)
2024-08-26 21:05:34 +02:00
therapy_type = SelectMultipleField("Therapieverfahren", choices=[
("A", "Analytische Psychotherapie"),
("S", "Systemische Therapie"),
("T", "Tiefenpsychologisch fundierte Psychotherapie"),
("V", "Verhaltenstherapie")], option_widget=CheckboxInput(), widget=ListWidget(prefix_label=False),
validators=[validate_therapy_types])
therapy_age_range = RadioField("Altersgruppe", choices=[
("E", "Erwachsene"),
("K", "Kinder und Jugendliche")], validators=[validators.InputRequired(), validators.any_of(["E", "K"])])
therapy_setting = RadioField("Therapiesetting", choices=[
("E", "Einzeltherapie"),
("G", "Gruppentherapie")], validators=[validators.InputRequired(), validators.any_of(["E", "G"])])
# TODO: find a better name for label
amount_of_weeks = IntegerField(
"Telefonzeiten für die nächsten Wochen",
widget=RangeInput(),
default=1,
validators=[validators.InputRequired(), validators.NumberRange(min=1, max=4)]
)