theraPy/forms/SearchForm.py

88 lines
3.7 KiB
Python
Raw Permalink 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
2024-10-12 21:39:20 +02:00
from wtforms.fields.simple import BooleanField
2024-08-27 23:09:19 +02:00
from wtforms.widgets.core import CheckboxInput, ListWidget, Input
2024-08-26 21:05:34 +02:00
def validate_therapy_types(form, field):
2024-09-07 21:12:55 +02:00
"""
Validate form input for given therapy types (mapping in input field)
:param form: required by calling method
:param field: data field for validation (therapy types)
:return:
"""
# Given choices by the data source
2024-08-26 21:05:34 +02:00
valid_choices = {"A", "S", "T", "V"}
2024-09-07 21:12:55 +02:00
# All of them in arbitrary sets are allowed
2024-08-26 21:05:34 +02:00
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):
2024-09-07 21:12:55 +02:00
"""
Validate number of weeks for therapy phone times search.
:param form: required by calling method
:param field: data field for validation (therapy phone times in weeks)
:return:
"""
# Weeks from 1 to 4
valid_choices = {1, 2, 3, 4}
2024-09-07 21:12:55 +02:00
# Check for given data at all -> at least 1 week necessary
if not field.data:
raise validators.ValidationError("Please select at least one option.")
2024-09-07 21:12:55 +02:00
# All of them in arbitrary sets are allowed
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):
2024-09-07 21:12:55 +02:00
"""
Define a range as input type to use HTML element form-range
"""
2024-08-27 23:09:19 +02:00
input_type = "range"
2024-08-26 21:05:34 +02:00
class SearchForm(Form):
2024-09-07 21:12:55 +02:00
"""
Search Form to search with several parameters and constraints for doctors/therapists with phone times
"""
# Match location as string or plz with arbitrary length
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-09-07 21:12:55 +02:00
# Define a distance as range input to limit the search area
2024-08-27 23:09:19 +02:00
therapy_distance = IntegerField(
2024-09-09 17:32:55 +02:00
"Distanz",
2024-08-27 23:09:19 +02:00
widget=RangeInput(),
default=25,
validators=[validators.InputRequired(), validators.NumberRange(min=0, max=50)]
)
2024-09-07 21:12:55 +02:00
# Define several therapy types (all of them possible to search for)
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])
2024-09-07 21:12:55 +02:00
# Distinction between adults/children & youth as radio button
2024-08-26 21:05:34 +02:00
therapy_age_range = RadioField("Altersgruppe", choices=[
("E", "Erwachsene"),
("K", "Kinder und Jugendliche")], validators=[validators.InputRequired(), validators.any_of(["E", "K"])])
2024-09-07 21:12:55 +02:00
# Distinction between available therapy types
2024-08-26 21:05:34 +02:00
therapy_setting = RadioField("Therapiesetting", choices=[
("E", "Einzeltherapie"),
("G", "Gruppentherapie")], validators=[validators.InputRequired(), validators.any_of(["E", "G"])])
# TODO: find a better name for label
2024-09-07 21:12:55 +02:00
# Choose for how many weeks to show phone times
amount_of_weeks = IntegerField(
2024-09-09 17:32:55 +02:00
# Rest of the label rendered by the frontend (even though not perfect solution)
"Telefonzeiten für die ",
widget=RangeInput(),
default=1,
validators=[validators.InputRequired(), validators.NumberRange(min=1, max=4)]
2024-10-12 20:13:27 +02:00
)
2024-10-12 21:39:20 +02:00
privacy_agreement = BooleanField("Datenschutz", validators=[validators.InputRequired()])