2017-12-11 16:02:12 +00:00
|
|
|
|
import weakref
|
2016-08-07 09:17:49 +01:00
|
|
|
|
from datetime import datetime, timedelta
|
2017-12-11 16:22:37 +00:00
|
|
|
|
from itertools import chain
|
2022-03-01 13:51:04 +00:00
|
|
|
|
from numbers import Number
|
2017-08-07 11:30:25 +01:00
|
|
|
|
|
2018-02-20 11:22:17 +00:00
|
|
|
|
import pytz
|
2023-06-12 17:00:20 -04:00
|
|
|
|
from flask import Markup, render_template, request
|
2020-03-06 11:53:55 +00:00
|
|
|
|
from flask_login import current_user
|
2018-02-20 11:22:17 +00:00
|
|
|
|
from flask_wtf import FlaskForm as Form
|
|
|
|
|
|
from flask_wtf.file import FileAllowed
|
|
|
|
|
|
from flask_wtf.file import FileField as FileField_wtf
|
2021-12-07 12:39:37 +00:00
|
|
|
|
from flask_wtf.file import FileSize
|
2021-10-29 16:10:55 +01:00
|
|
|
|
from notifications_utils.formatters import strip_all_whitespace
|
2022-02-04 10:43:36 +00:00
|
|
|
|
from notifications_utils.insensitive_dict import InsensitiveDict
|
2023-08-25 08:57:24 -07:00
|
|
|
|
from notifications_utils.recipients import InvalidPhoneError, validate_phone_number
|
2020-04-23 12:15:24 +01:00
|
|
|
|
from werkzeug.utils import cached_property
|
2016-01-12 10:43:23 +00:00
|
|
|
|
from wtforms import (
|
2016-03-02 15:25:04 +00:00
|
|
|
|
BooleanField,
|
2018-02-20 11:22:17 +00:00
|
|
|
|
DateField,
|
2021-11-30 17:17:16 +00:00
|
|
|
|
EmailField,
|
2018-02-20 11:22:17 +00:00
|
|
|
|
FieldList,
|
|
|
|
|
|
FileField,
|
2016-05-11 09:43:55 +01:00
|
|
|
|
HiddenField,
|
2016-06-29 17:10:49 +01:00
|
|
|
|
IntegerField,
|
2018-02-20 11:22:17 +00:00
|
|
|
|
PasswordField,
|
2020-04-22 17:49:03 +01:00
|
|
|
|
)
|
|
|
|
|
|
from wtforms import RadioField as WTFormsRadioField
|
|
|
|
|
|
from wtforms import (
|
2021-11-30 17:17:16 +00:00
|
|
|
|
SearchField,
|
2018-11-08 11:56:29 +00:00
|
|
|
|
SelectMultipleField,
|
2018-02-20 11:22:17 +00:00
|
|
|
|
StringField,
|
2021-11-30 17:17:16 +00:00
|
|
|
|
TelField,
|
2018-02-20 11:22:17 +00:00
|
|
|
|
TextAreaField,
|
|
|
|
|
|
ValidationError,
|
|
|
|
|
|
validators,
|
2017-10-18 14:51:26 +01:00
|
|
|
|
)
|
2022-02-25 11:27:56 +00:00
|
|
|
|
from wtforms.validators import (
|
|
|
|
|
|
URL,
|
|
|
|
|
|
DataRequired,
|
|
|
|
|
|
InputRequired,
|
|
|
|
|
|
Length,
|
|
|
|
|
|
Optional,
|
|
|
|
|
|
Regexp,
|
|
|
|
|
|
)
|
2016-01-11 15:00:51 +00:00
|
|
|
|
|
2022-03-14 14:39:54 +00:00
|
|
|
|
from app.formatters import (
|
|
|
|
|
|
format_auth_type,
|
|
|
|
|
|
format_thousands,
|
|
|
|
|
|
guess_name_from_email_address,
|
|
|
|
|
|
)
|
2018-01-31 10:26:37 +00:00
|
|
|
|
from app.main.validators import (
|
2020-06-16 18:04:29 +01:00
|
|
|
|
CommonlyUsedPassword,
|
2018-01-31 10:26:37 +00:00
|
|
|
|
CsvFileValidator,
|
2018-02-28 11:50:41 +00:00
|
|
|
|
DoesNotStartWithDoubleZero,
|
2021-07-26 15:10:57 +01:00
|
|
|
|
LettersNumbersSingleQuotesFullStopsAndUnderscoresOnly,
|
2019-11-08 17:12:32 +00:00
|
|
|
|
MustContainAlphanumericCharacters,
|
2018-01-31 10:26:37 +00:00
|
|
|
|
NoCommasInPlaceHolders,
|
2020-03-04 14:25:14 +00:00
|
|
|
|
NoEmbeddedImagesInSVG,
|
2022-01-06 11:21:12 +00:00
|
|
|
|
NoTextInSVG,
|
2019-05-03 15:13:39 +01:00
|
|
|
|
OnlySMSCharacters,
|
2018-02-20 11:22:17 +00:00
|
|
|
|
ValidEmail,
|
|
|
|
|
|
ValidGovEmail,
|
2018-01-31 10:26:37 +00:00
|
|
|
|
)
|
2023-07-12 12:09:44 -04:00
|
|
|
|
from app.models.organization import Organization
|
2023-12-18 16:10:12 -05:00
|
|
|
|
from app.utils import merge_jsonlike
|
2023-11-16 12:24:27 -08:00
|
|
|
|
from app.utils.csv import get_user_preferred_timezone
|
2022-10-04 03:04:13 +00:00
|
|
|
|
from app.utils.user_permissions import all_ui_permissions, permission_options
|
2016-02-01 16:57:40 +00:00
|
|
|
|
|
2015-11-27 09:47:29 +00:00
|
|
|
|
|
2016-08-07 09:17:49 +01:00
|
|
|
|
def get_time_value_and_label(future_time):
|
2023-11-22 08:09:28 -08:00
|
|
|
|
preferred_tz = pytz.timezone(get_user_preferred_timezone())
|
2016-08-07 09:17:49 +01:00
|
|
|
|
return (
|
2023-11-22 08:09:28 -08:00
|
|
|
|
future_time.astimezone(preferred_tz).replace(tzinfo=None).isoformat(),
|
2023-11-16 12:24:27 -08:00
|
|
|
|
"{} at {} {}".format(
|
2023-11-22 08:09:28 -08:00
|
|
|
|
get_human_day(future_time.astimezone(preferred_tz)),
|
|
|
|
|
|
get_human_time(future_time.astimezone(preferred_tz)),
|
2023-11-16 12:24:27 -08:00
|
|
|
|
get_user_preferred_timezone(),
|
2023-08-25 09:12:23 -07:00
|
|
|
|
),
|
2016-08-07 09:17:49 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_human_time(time):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return {"0": "midnight", "12": "noon"}.get(
|
|
|
|
|
|
time.strftime("%-H"), time.strftime("%-I%p").lower()
|
2016-08-07 09:17:49 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def get_human_day(time, prefix_today_with="T"):
|
2016-10-11 14:11:10 +01:00
|
|
|
|
# Add 1 hour to get ‘midnight today’ instead of ‘midnight tomorrow’
|
2023-11-16 12:24:27 -08:00
|
|
|
|
|
2023-11-22 08:09:28 -08:00
|
|
|
|
preferred_tz = pytz.timezone(get_user_preferred_timezone())
|
2023-08-25 09:12:23 -07:00
|
|
|
|
time = (time - timedelta(hours=1)).strftime("%A")
|
2023-11-22 08:09:28 -08:00
|
|
|
|
if time == datetime.now(preferred_tz).strftime("%A"):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "{}oday".format(prefix_today_with)
|
2023-11-22 08:09:28 -08:00
|
|
|
|
if time == (datetime.now(preferred_tz) + timedelta(days=1)).strftime("%A"):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Tomorrow"
|
2016-10-11 14:11:10 +01:00
|
|
|
|
return time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_furthest_possible_scheduled_time():
|
2022-11-29 16:38:06 -05:00
|
|
|
|
# We want local time to find date boundaries
|
2023-11-22 08:09:28 -08:00
|
|
|
|
preferred_tz = pytz.timezone(get_user_preferred_timezone())
|
|
|
|
|
|
return (datetime.now(preferred_tz) + timedelta(days=4)).replace(hour=0)
|
2016-10-11 14:11:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_next_hours_until(until):
|
2023-11-22 08:09:28 -08:00
|
|
|
|
preferred_tz = pytz.timezone(get_user_preferred_timezone())
|
|
|
|
|
|
now = datetime.now(preferred_tz)
|
2016-10-11 14:11:10 +01:00
|
|
|
|
hours = int((until - now).total_seconds() / (60 * 60))
|
2016-08-07 09:17:49 +01:00
|
|
|
|
return [
|
2022-11-29 16:38:06 -05:00
|
|
|
|
(now + timedelta(hours=i)).replace(minute=0, second=0, microsecond=0)
|
2016-08-07 09:17:49 +01:00
|
|
|
|
for i in range(1, hours + 1)
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-11 14:11:10 +01:00
|
|
|
|
def get_next_days_until(until):
|
2023-11-22 08:09:28 -08:00
|
|
|
|
preferred_tz = pytz.timezone(get_user_preferred_timezone())
|
|
|
|
|
|
now = datetime.now(preferred_tz)
|
2016-10-11 14:11:10 +01:00
|
|
|
|
days = int((until - now).total_seconds() / (60 * 60 * 24))
|
|
|
|
|
|
return [
|
2023-08-25 09:12:23 -07:00
|
|
|
|
get_human_day((now + timedelta(days=i)), prefix_today_with="Later t")
|
2016-10-11 14:11:10 +01:00
|
|
|
|
for i in range(0, days + 1)
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-22 17:49:03 +01:00
|
|
|
|
class RadioField(WTFormsRadioField):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, *args, thing="an option", **kwargs):
|
2020-04-22 17:49:03 +01:00
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
self.thing = thing
|
|
|
|
|
|
self.validate_choice = False
|
|
|
|
|
|
|
|
|
|
|
|
def pre_validate(self, form):
|
|
|
|
|
|
super().pre_validate(form)
|
|
|
|
|
|
if self.data not in dict(self.choices).keys():
|
2023-08-25 09:12:23 -07:00
|
|
|
|
raise ValidationError(f"Select {self.thing}")
|
2020-04-22 17:49:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def email_address(label="Email address", gov_user=True, required=True):
|
2016-10-26 14:01:01 +01:00
|
|
|
|
validators = [
|
2018-12-07 12:23:12 +00:00
|
|
|
|
ValidEmail(),
|
2016-10-26 14:01:01 +01:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
if gov_user:
|
2016-10-28 10:45:05 +01:00
|
|
|
|
validators.append(ValidGovEmail())
|
2018-12-07 12:23:12 +00:00
|
|
|
|
|
|
|
|
|
|
if required:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
validators.append(DataRequired(message="Cannot be empty"))
|
2018-12-07 12:23:12 +00:00
|
|
|
|
|
2020-08-13 10:53:28 +01:00
|
|
|
|
return GovukEmailField(label, validators)
|
2016-01-08 12:00:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-02-15 13:13:57 +00:00
|
|
|
|
class UKMobileNumber(TelField):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, label="", validators=None, param_extensions=None, **kwargs):
|
2020-08-07 13:46:15 +01:00
|
|
|
|
super(UKMobileNumber, self).__init__(label, validators, **kwargs)
|
|
|
|
|
|
self.param_extensions = param_extensions
|
|
|
|
|
|
|
|
|
|
|
|
# self.__call__ renders the HTML for the field by:
|
|
|
|
|
|
# 1. delegating to self.meta.render_field which
|
|
|
|
|
|
# 2. calls field.widget
|
|
|
|
|
|
# this bypasses that by making self.widget a method with the same interface as widget.__call__
|
|
|
|
|
|
def widget(self, field, param_extensions=None, **kwargs):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return govuk_text_input_field_widget(
|
|
|
|
|
|
self, field, type="tel", param_extensions=param_extensions, **kwargs
|
|
|
|
|
|
)
|
2020-08-07 13:46:15 +01:00
|
|
|
|
|
2016-01-12 18:10:16 +00:00
|
|
|
|
def pre_validate(self, form):
|
2016-02-01 16:57:40 +00:00
|
|
|
|
try:
|
2016-03-08 07:17:39 +00:00
|
|
|
|
validate_phone_number(self.data)
|
2016-02-01 16:57:40 +00:00
|
|
|
|
except InvalidPhoneError as e:
|
2016-12-20 14:38:34 +00:00
|
|
|
|
raise ValidationError(str(e))
|
2016-01-12 18:10:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-05-25 09:40:37 +01:00
|
|
|
|
class InternationalPhoneNumber(TelField):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, label="", validators=None, param_extensions=None, **kwargs):
|
|
|
|
|
|
super(InternationalPhoneNumber, self).__init__(
|
|
|
|
|
|
label, validators=validators, **kwargs
|
|
|
|
|
|
)
|
2020-08-07 13:46:15 +01:00
|
|
|
|
self.param_extensions = param_extensions
|
|
|
|
|
|
|
|
|
|
|
|
# self.__call__ renders the HTML for the field by:
|
|
|
|
|
|
# 1. delegating to self.meta.render_field which
|
|
|
|
|
|
# 2. calls field.widget
|
|
|
|
|
|
# this bypasses that by making self.widget a method with the same interface as widget.__call__
|
|
|
|
|
|
def widget(self, field, param_extensions=None, **kwargs):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return govuk_text_input_field_widget(
|
|
|
|
|
|
self, field, type="tel", param_extensions=param_extensions, **kwargs
|
|
|
|
|
|
)
|
2020-08-07 13:46:15 +01:00
|
|
|
|
|
2017-05-25 09:40:37 +01:00
|
|
|
|
def pre_validate(self, form):
|
|
|
|
|
|
try:
|
2017-11-10 12:35:21 +00:00
|
|
|
|
if self.data:
|
|
|
|
|
|
validate_phone_number(self.data, international=True)
|
2017-05-25 09:40:37 +01:00
|
|
|
|
except InvalidPhoneError as e:
|
|
|
|
|
|
raise ValidationError(str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def uk_mobile_number(label="Mobile number"):
|
|
|
|
|
|
return UKMobileNumber(label, validators=[DataRequired(message="Cannot be empty")])
|
2016-01-08 12:00:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def international_phone_number(label="Mobile number"):
|
2017-05-25 09:40:37 +01:00
|
|
|
|
return InternationalPhoneNumber(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
label, validators=[DataRequired(message="Cannot be empty")]
|
2017-05-25 09:40:37 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def password(label="Password"):
|
2020-08-07 09:53:09 +01:00
|
|
|
|
return GovukPasswordField(
|
|
|
|
|
|
label,
|
|
|
|
|
|
validators=[
|
2023-08-25 09:12:23 -07:00
|
|
|
|
DataRequired(message="Cannot be empty"),
|
|
|
|
|
|
Length(8, 255, message="Must be at least 8 characters"),
|
|
|
|
|
|
CommonlyUsedPassword(message="Choose a password that’s harder to guess"),
|
|
|
|
|
|
],
|
2020-08-07 09:53:09 +01:00
|
|
|
|
)
|
2016-01-08 12:00:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def govuk_text_input_field_widget(
|
|
|
|
|
|
self, field, type=None, param_extensions=None, **kwargs
|
|
|
|
|
|
):
|
2022-03-07 12:40:18 +00:00
|
|
|
|
value = kwargs["value"] if "value" in kwargs else field.data
|
2022-03-01 13:51:04 +00:00
|
|
|
|
value = str(value) if isinstance(value, Number) else value
|
2020-08-07 11:36:45 +01:00
|
|
|
|
|
2020-07-22 16:29:11 +01:00
|
|
|
|
# error messages
|
|
|
|
|
|
error_message = None
|
|
|
|
|
|
if field.errors:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
error_message_format = (
|
|
|
|
|
|
"html" if kwargs.get("error_message_with_html") else "text"
|
|
|
|
|
|
)
|
2020-07-28 11:44:53 +01:00
|
|
|
|
error_message = {
|
|
|
|
|
|
"attributes": {
|
|
|
|
|
|
"data-module": "track-error",
|
|
|
|
|
|
"data-error-type": field.errors[0],
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"data-error-label": field.name,
|
2020-07-28 11:44:53 +01:00
|
|
|
|
},
|
2023-08-25 09:12:23 -07:00
|
|
|
|
error_message_format: field.errors[0],
|
2020-07-28 11:44:53 +01:00
|
|
|
|
}
|
2020-07-22 16:29:11 +01:00
|
|
|
|
|
2023-06-08 13:12:00 -04:00
|
|
|
|
# convert to parameters that usa understands
|
2020-07-22 16:29:11 +01:00
|
|
|
|
params = {
|
|
|
|
|
|
"errorMessage": error_message,
|
|
|
|
|
|
"id": field.id,
|
|
|
|
|
|
"label": {"text": field.label.text},
|
|
|
|
|
|
"name": field.name,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"value": value,
|
2020-07-22 16:29:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if type:
|
|
|
|
|
|
params["type"] = type
|
|
|
|
|
|
|
|
|
|
|
|
# extend default params with any sent in
|
2020-08-13 12:38:12 +01:00
|
|
|
|
merge_jsonlike(params, self.param_extensions)
|
|
|
|
|
|
# add any sent in through use in templates
|
|
|
|
|
|
merge_jsonlike(params, param_extensions)
|
2020-07-22 16:29:11 +01:00
|
|
|
|
|
|
|
|
|
|
return Markup(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
render_template("components/components/input/template.njk", params=params)
|
|
|
|
|
|
)
|
2020-07-22 16:29:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-07-14 16:24:40 +01:00
|
|
|
|
class GovukTextInputField(StringField):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, label="", validators=None, param_extensions=None, **kwargs):
|
2020-07-14 16:24:40 +01:00
|
|
|
|
super(GovukTextInputField, self).__init__(label, validators, **kwargs)
|
|
|
|
|
|
self.param_extensions = param_extensions
|
|
|
|
|
|
|
|
|
|
|
|
# self.__call__ renders the HTML for the field by:
|
|
|
|
|
|
# 1. delegating to self.meta.render_field which
|
|
|
|
|
|
# 2. calls field.widget
|
|
|
|
|
|
# this bypasses that by making self.widget a method with the same interface as widget.__call__
|
|
|
|
|
|
def widget(self, field, **kwargs):
|
2020-10-29 14:57:32 +00:00
|
|
|
|
return govuk_text_input_field_widget(self, field, **kwargs)
|
2020-07-14 16:24:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-08-07 09:49:07 +01:00
|
|
|
|
class GovukPasswordField(PasswordField):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, label="", validators=None, param_extensions=None, **kwargs):
|
2020-08-07 09:49:07 +01:00
|
|
|
|
super(GovukPasswordField, self).__init__(label, validators, **kwargs)
|
|
|
|
|
|
self.param_extensions = param_extensions
|
|
|
|
|
|
|
|
|
|
|
|
# self.__call__ renders the HTML for the field by:
|
|
|
|
|
|
# 1. delegating to self.meta.render_field which
|
|
|
|
|
|
# 2. calls field.widget
|
|
|
|
|
|
# this bypasses that by making self.widget a method with the same interface as widget.__call__
|
|
|
|
|
|
def widget(self, field, param_extensions=None, **kwargs):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return govuk_text_input_field_widget(
|
|
|
|
|
|
self, field, type="password", param_extensions=param_extensions, **kwargs
|
|
|
|
|
|
)
|
2020-08-07 09:49:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-08-07 10:07:52 +01:00
|
|
|
|
class GovukEmailField(EmailField):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, label="", validators=None, param_extensions=None, **kwargs):
|
2020-08-07 10:07:52 +01:00
|
|
|
|
super(GovukEmailField, self).__init__(label, validators=validators, **kwargs)
|
|
|
|
|
|
self.param_extensions = param_extensions
|
|
|
|
|
|
|
|
|
|
|
|
# self.__call__ renders the HTML for the field by:
|
|
|
|
|
|
# 1. delegating to self.meta.render_field which
|
|
|
|
|
|
# 2. calls field.widget
|
|
|
|
|
|
# this bypasses that by making self.widget a method with the same interface as widget.__call__
|
|
|
|
|
|
def widget(self, field, param_extensions=None, **kwargs):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
params = {
|
|
|
|
|
|
"attributes": {"spellcheck": "false"}
|
|
|
|
|
|
} # email addresses don't need to be spellchecked
|
2020-08-13 12:38:12 +01:00
|
|
|
|
merge_jsonlike(params, param_extensions)
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return govuk_text_input_field_widget(
|
|
|
|
|
|
self, field, type="email", param_extensions=params, **kwargs
|
|
|
|
|
|
)
|
2020-08-07 10:07:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-08-07 10:15:49 +01:00
|
|
|
|
class GovukSearchField(SearchField):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, label="", validators=None, param_extensions=None, **kwargs):
|
2020-08-07 10:15:49 +01:00
|
|
|
|
super(GovukSearchField, self).__init__(label, validators, **kwargs)
|
|
|
|
|
|
self.param_extensions = param_extensions
|
|
|
|
|
|
|
|
|
|
|
|
# self.__call__ renders the HTML for the field by:
|
|
|
|
|
|
# 1. delegating to self.meta.render_field which
|
|
|
|
|
|
# 2. calls field.widget
|
|
|
|
|
|
# this bypasses that by making self.widget a method with the same interface as widget.__call__
|
|
|
|
|
|
def widget(self, field, param_extensions=None, **kwargs):
|
2023-08-17 15:13:26 -04:00
|
|
|
|
params = {"classes": ""} # email addresses don't need to be spellchecked
|
2020-08-13 12:38:12 +01:00
|
|
|
|
merge_jsonlike(params, param_extensions)
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return govuk_text_input_field_widget(
|
|
|
|
|
|
self, field, type="search", param_extensions=params, **kwargs
|
|
|
|
|
|
)
|
2020-08-07 10:15:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-08-07 10:24:34 +01:00
|
|
|
|
class GovukDateField(DateField):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, label="", validators=None, param_extensions=None, **kwargs):
|
2020-08-07 10:24:34 +01:00
|
|
|
|
super(GovukDateField, self).__init__(label, validators, **kwargs)
|
|
|
|
|
|
self.param_extensions = param_extensions
|
|
|
|
|
|
|
|
|
|
|
|
# self.__call__ renders the HTML for the field by:
|
|
|
|
|
|
# 1. delegating to self.meta.render_field which
|
|
|
|
|
|
# 2. calls field.widget
|
|
|
|
|
|
# this bypasses that by making self.widget a method with the same interface as widget.__call__
|
|
|
|
|
|
def widget(self, field, param_extensions=None, **kwargs):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return govuk_text_input_field_widget(
|
|
|
|
|
|
self, field, param_extensions=param_extensions, **kwargs
|
|
|
|
|
|
)
|
2020-08-07 10:24:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-08-07 10:30:46 +01:00
|
|
|
|
class GovukIntegerField(IntegerField):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, label="", validators=None, param_extensions=None, **kwargs):
|
2020-08-07 10:30:46 +01:00
|
|
|
|
super(GovukIntegerField, self).__init__(label, validators, **kwargs)
|
|
|
|
|
|
self.param_extensions = param_extensions
|
|
|
|
|
|
|
|
|
|
|
|
# self.__call__ renders the HTML for the field by:
|
|
|
|
|
|
# 1. delegating to self.meta.render_field which
|
|
|
|
|
|
# 2. calls field.widget
|
|
|
|
|
|
# this bypasses that by making self.widget a method with the same interface as widget.__call__
|
|
|
|
|
|
def widget(self, field, param_extensions=None, **kwargs):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return govuk_text_input_field_widget(
|
|
|
|
|
|
self, field, param_extensions=param_extensions, **kwargs
|
|
|
|
|
|
)
|
2020-08-07 10:30:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-08-03 18:12:52 +01:00
|
|
|
|
class SMSCode(GovukTextInputField):
|
2018-05-07 22:57:18 +01:00
|
|
|
|
validators = [
|
2023-08-25 09:12:23 -07:00
|
|
|
|
DataRequired(message="Cannot be empty"),
|
|
|
|
|
|
Regexp(regex=r"^\d+$", message="Numbers only"),
|
|
|
|
|
|
Length(min=6, message="Not enough numbers"),
|
|
|
|
|
|
Length(max=6, message="Too many numbers"),
|
2018-05-07 22:57:18 +01:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def __call__(self, **kwargs):
|
2020-08-03 18:12:52 +01:00
|
|
|
|
params = {"attributes": {"pattern": "[0-9]*"}}
|
|
|
|
|
|
if "param_extensions" in kwargs:
|
2020-08-13 12:38:12 +01:00
|
|
|
|
merge_jsonlike(kwargs["param_extensions"], params)
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return super().__call__(type="tel", **kwargs)
|
2016-01-08 12:00:52 +00:00
|
|
|
|
|
2020-04-17 16:16:52 +01:00
|
|
|
|
def process_formdata(self, valuelist):
|
|
|
|
|
|
if valuelist:
|
2022-02-04 10:43:36 +00:00
|
|
|
|
self.data = InsensitiveDict.make_key(valuelist[0])
|
2020-04-17 16:16:52 +01:00
|
|
|
|
|
2016-01-08 12:00:52 +00:00
|
|
|
|
|
2020-08-07 11:36:45 +01:00
|
|
|
|
class ForgivingIntegerField(GovukTextInputField):
|
2019-02-27 13:02:55 +00:00
|
|
|
|
# Actual value is 2147483647 but this is a scary looking arbitrary number
|
|
|
|
|
|
POSTGRES_MAX_INT = 2000000000
|
2019-02-15 13:34:29 +00:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, label=None, things="items", format_error_suffix="", **kwargs):
|
2019-02-27 13:02:37 +00:00
|
|
|
|
self.things = things
|
2019-02-27 15:10:34 +00:00
|
|
|
|
self.format_error_suffix = format_error_suffix
|
2019-02-27 13:02:37 +00:00
|
|
|
|
super().__init__(label, **kwargs)
|
|
|
|
|
|
|
2019-02-15 13:34:29 +00:00
|
|
|
|
def process_formdata(self, valuelist):
|
|
|
|
|
|
if valuelist:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
value = valuelist[0].replace(",", "").replace(" ", "")
|
2019-02-15 13:34:29 +00:00
|
|
|
|
|
|
|
|
|
|
try:
|
2019-02-27 12:05:28 +00:00
|
|
|
|
value = int(value)
|
2019-02-15 13:34:29 +00:00
|
|
|
|
except ValueError:
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if value == "":
|
2019-02-27 12:05:28 +00:00
|
|
|
|
value = 0
|
2019-02-15 13:34:29 +00:00
|
|
|
|
|
2019-02-27 12:05:28 +00:00
|
|
|
|
return super().process_formdata([value])
|
2019-02-15 13:34:29 +00:00
|
|
|
|
|
|
|
|
|
|
def pre_validate(self, form):
|
|
|
|
|
|
if self.data:
|
|
|
|
|
|
error = None
|
|
|
|
|
|
try:
|
|
|
|
|
|
if int(self.data) > self.POSTGRES_MAX_INT:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
error = "Number of {} must be {} or less".format(
|
2019-02-27 13:02:37 +00:00
|
|
|
|
self.things,
|
2019-07-08 14:09:29 +01:00
|
|
|
|
format_thousands(self.POSTGRES_MAX_INT),
|
2019-02-27 13:02:37 +00:00
|
|
|
|
)
|
2019-02-15 13:34:29 +00:00
|
|
|
|
except ValueError:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
error = "Enter the number of {} {}".format(
|
2019-02-27 15:10:34 +00:00
|
|
|
|
self.things,
|
|
|
|
|
|
self.format_error_suffix,
|
|
|
|
|
|
)
|
2019-02-15 13:34:29 +00:00
|
|
|
|
|
|
|
|
|
|
if error:
|
|
|
|
|
|
raise ValidationError(error)
|
|
|
|
|
|
|
|
|
|
|
|
return super().pre_validate(form)
|
|
|
|
|
|
|
|
|
|
|
|
def __call__(self, **kwargs):
|
2019-02-27 12:05:28 +00:00
|
|
|
|
if self.get_form().is_submitted() and not self.get_form().validate():
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return super().__call__(value=(self.raw_data or [None])[0], **kwargs)
|
2019-02-27 12:05:28 +00:00
|
|
|
|
|
2019-02-15 13:34:29 +00:00
|
|
|
|
try:
|
|
|
|
|
|
value = int(self.data)
|
2019-07-08 14:09:29 +01:00
|
|
|
|
value = format_thousands(value)
|
2019-02-15 13:34:29 +00:00
|
|
|
|
except (ValueError, TypeError):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
value = self.data if self.data is not None else ""
|
2019-02-15 13:34:29 +00:00
|
|
|
|
|
|
|
|
|
|
return super().__call__(value=value, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
class FieldWithNoneOption:
|
2019-02-25 16:17:48 +00:00
|
|
|
|
# This is a special value that is specific to our forms. This is
|
|
|
|
|
|
# more expicit than casting `None` to a string `'None'` which can
|
|
|
|
|
|
# have unexpected edge cases
|
2023-08-25 09:12:23 -07:00
|
|
|
|
NONE_OPTION_VALUE = "__NONE__"
|
2019-02-25 16:17:48 +00:00
|
|
|
|
|
|
|
|
|
|
# When receiving Python data, eg when instantiating the form object
|
|
|
|
|
|
# we want to convert that data to our special value, so that it gets
|
|
|
|
|
|
# recognised as being one of the valid choices
|
|
|
|
|
|
def process_data(self, value):
|
|
|
|
|
|
self.data = self.NONE_OPTION_VALUE if value is None else value
|
|
|
|
|
|
|
|
|
|
|
|
# After validation we want to convert it back to a Python `None` for
|
|
|
|
|
|
# use elsewhere, eg posting to the API
|
|
|
|
|
|
def post_validate(self, form, validation_stopped):
|
|
|
|
|
|
if self.data == self.NONE_OPTION_VALUE and not validation_stopped:
|
|
|
|
|
|
self.data = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RadioFieldWithNoneOption(FieldWithNoneOption, RadioField):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NestedFieldMixin:
|
|
|
|
|
|
def children(self):
|
|
|
|
|
|
# start map with root option as a single child entry
|
2023-08-25 09:12:23 -07:00
|
|
|
|
child_map = {
|
|
|
|
|
|
None: [option for option in self if option.data == self.NONE_OPTION_VALUE]
|
|
|
|
|
|
}
|
2019-02-25 16:17:48 +00:00
|
|
|
|
|
|
|
|
|
|
# add entries for all other children
|
|
|
|
|
|
for option in self:
|
2020-04-23 12:15:24 +01:00
|
|
|
|
# assign all options with a NONE_OPTION_VALUE (not always None) to the None key
|
2019-02-25 16:17:48 +00:00
|
|
|
|
if option.data == self.NONE_OPTION_VALUE:
|
|
|
|
|
|
child_ids = [
|
2023-08-25 09:12:23 -07:00
|
|
|
|
folder["id"]
|
|
|
|
|
|
for folder in self.all_template_folders
|
|
|
|
|
|
if folder["parent_id"] is None
|
|
|
|
|
|
]
|
2019-02-25 16:17:48 +00:00
|
|
|
|
key = self.NONE_OPTION_VALUE
|
|
|
|
|
|
else:
|
|
|
|
|
|
child_ids = [
|
2023-08-25 09:12:23 -07:00
|
|
|
|
folder["id"]
|
|
|
|
|
|
for folder in self.all_template_folders
|
|
|
|
|
|
if folder["parent_id"] == option.data
|
|
|
|
|
|
]
|
2019-02-25 16:17:48 +00:00
|
|
|
|
key = option.data
|
|
|
|
|
|
|
|
|
|
|
|
child_map[key] = [option for option in self if option.data in child_ids]
|
|
|
|
|
|
|
|
|
|
|
|
return child_map
|
|
|
|
|
|
|
2020-04-23 12:15:24 +01:00
|
|
|
|
# to be used as the only version of .children once radios are converted
|
|
|
|
|
|
@cached_property
|
|
|
|
|
|
def _children(self):
|
|
|
|
|
|
return self.children()
|
|
|
|
|
|
|
|
|
|
|
|
def get_items_from_options(self, field):
|
|
|
|
|
|
items = []
|
|
|
|
|
|
|
|
|
|
|
|
for option in self._children[None]:
|
|
|
|
|
|
item = self.get_item_from_option(option)
|
|
|
|
|
|
if option.data in self._children:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
item["children"] = self.render_children(
|
|
|
|
|
|
field.name, option.label.text, self._children[option.data]
|
|
|
|
|
|
)
|
2020-04-23 12:15:24 +01:00
|
|
|
|
items.append(item)
|
|
|
|
|
|
|
|
|
|
|
|
return items
|
|
|
|
|
|
|
|
|
|
|
|
def render_children(self, name, label, options):
|
|
|
|
|
|
params = {
|
|
|
|
|
|
"name": name,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"fieldset": {"legend": {"text": label, "classes": "usa-sr-only"}},
|
|
|
|
|
|
"formGroup": {"classes": "usa-form-group--nested"},
|
2020-04-23 12:15:24 +01:00
|
|
|
|
"asList": True,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"items": [],
|
2020-04-23 12:15:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
for option in options:
|
|
|
|
|
|
item = self.get_item_from_option(option)
|
|
|
|
|
|
|
|
|
|
|
|
if len(self._children[option.data]):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
item["children"] = self.render_children(
|
|
|
|
|
|
name, option.label.text, self._children[option.data]
|
|
|
|
|
|
)
|
2020-04-23 12:15:24 +01:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
params["items"].append(item)
|
2020-04-23 12:15:24 +01:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return render_template("forms/fields/checkboxes/template.njk", params=params)
|
2020-04-23 12:15:24 +01:00
|
|
|
|
|
2019-02-25 16:17:48 +00:00
|
|
|
|
|
|
|
|
|
|
class NestedRadioField(RadioFieldWithNoneOption, NestedFieldMixin):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NestedCheckboxesField(SelectMultipleField, NestedFieldMixin):
|
|
|
|
|
|
NONE_OPTION_VALUE = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HiddenFieldWithNoneOption(FieldWithNoneOption, HiddenField):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RadioFieldWithRequiredMessage(RadioField):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, *args, required_message="Not a valid choice", **kwargs):
|
2019-02-25 16:17:48 +00:00
|
|
|
|
self.required_message = required_message
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def pre_validate(self, form):
|
|
|
|
|
|
try:
|
|
|
|
|
|
return super().pre_validate(form)
|
|
|
|
|
|
except ValueError:
|
2021-11-30 17:17:16 +00:00
|
|
|
|
raise ValidationError(self.required_message)
|
2019-02-25 16:17:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class StripWhitespaceForm(Form):
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
def bind_field(self, form, unbound_field, options):
|
|
|
|
|
|
# FieldList simply doesn't support filters.
|
|
|
|
|
|
# @see: https://github.com/wtforms/wtforms/issues/148
|
2020-08-07 09:49:07 +01:00
|
|
|
|
no_filter_fields = (FieldList, PasswordField, GovukPasswordField)
|
2023-08-25 09:12:23 -07:00
|
|
|
|
filters = (
|
|
|
|
|
|
[strip_all_whitespace]
|
|
|
|
|
|
if not issubclass(unbound_field.field_class, no_filter_fields)
|
|
|
|
|
|
else []
|
|
|
|
|
|
)
|
|
|
|
|
|
filters += unbound_field.kwargs.get("filters", [])
|
2017-12-11 16:02:12 +00:00
|
|
|
|
bound = unbound_field.bind(form=form, filters=filters, **options)
|
2023-08-25 09:12:23 -07:00
|
|
|
|
bound.get_form = weakref.ref(
|
|
|
|
|
|
form
|
|
|
|
|
|
) # GC won't collect the form if we don't use a weakref
|
2017-12-11 16:02:12 +00:00
|
|
|
|
return bound
|
|
|
|
|
|
|
2021-02-19 11:31:25 +00:00
|
|
|
|
def render_field(self, field, render_kw):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
render_kw.setdefault("required", False)
|
2021-02-19 11:31:25 +00:00
|
|
|
|
return super().render_field(field, render_kw)
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
|
2020-08-07 10:48:21 +01:00
|
|
|
|
class StripWhitespaceStringField(GovukTextInputField):
|
|
|
|
|
|
def __init__(self, label=None, param_extensions=None, **kwargs):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
kwargs["filters"] = tuple(
|
|
|
|
|
|
chain(
|
|
|
|
|
|
kwargs.get("filters", ()),
|
|
|
|
|
|
(strip_all_whitespace,),
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
2020-08-07 10:48:21 +01:00
|
|
|
|
super(GovukTextInputField, self).__init__(label, **kwargs)
|
|
|
|
|
|
self.param_extensions = param_extensions
|
2017-12-11 16:22:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class LoginForm(StripWhitespaceForm):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
email_address = GovukEmailField(
|
|
|
|
|
|
"Email address",
|
|
|
|
|
|
validators=[
|
|
|
|
|
|
Length(min=5, max=255),
|
|
|
|
|
|
DataRequired(message="Cannot be empty"),
|
|
|
|
|
|
ValidEmail(),
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
|
|
|
|
|
password = GovukPasswordField(
|
|
|
|
|
|
"Password", validators=[DataRequired(message="Enter your password")]
|
|
|
|
|
|
)
|
2015-12-01 13:23:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class RegisterUserForm(StripWhitespaceForm):
|
2020-08-07 15:16:04 +01:00
|
|
|
|
name = GovukTextInputField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Full name", validators=[DataRequired(message="Cannot be empty")]
|
2020-08-07 15:16:04 +01:00
|
|
|
|
)
|
2016-01-08 12:00:52 +00:00
|
|
|
|
email_address = email_address()
|
2017-08-29 14:52:24 +01:00
|
|
|
|
mobile_number = international_phone_number()
|
2016-01-08 12:00:52 +00:00
|
|
|
|
password = password()
|
2017-11-13 13:39:31 +00:00
|
|
|
|
# always register as sms type
|
2023-08-25 09:12:23 -07:00
|
|
|
|
auth_type = HiddenField("auth_type", default="sms_auth")
|
2015-12-04 16:21:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-07-02 09:08:21 +01:00
|
|
|
|
class RegisterUserFromInviteForm(RegisterUserForm):
|
2017-11-14 15:53:38 +00:00
|
|
|
|
def __init__(self, invited_user):
|
|
|
|
|
|
super().__init__(
|
2019-05-23 15:27:35 +01:00
|
|
|
|
service=invited_user.service,
|
|
|
|
|
|
email_address=invited_user.email_address,
|
|
|
|
|
|
auth_type=invited_user.auth_type,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
name=guess_name_from_email_address(invited_user.email_address),
|
2017-11-14 15:53:38 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
mobile_number = InternationalPhoneNumber("Mobile number", validators=[])
|
|
|
|
|
|
service = HiddenField("service")
|
|
|
|
|
|
email_address = HiddenField("email_address")
|
|
|
|
|
|
auth_type = HiddenField("auth_type", validators=[DataRequired()])
|
2016-03-02 15:25:04 +00:00
|
|
|
|
|
2017-11-10 12:35:21 +00:00
|
|
|
|
def validate_mobile_number(self, field):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if self.auth_type.data == "sms_auth" and not field.data:
|
|
|
|
|
|
raise ValidationError("Cannot be empty")
|
2017-11-10 12:35:21 +00:00
|
|
|
|
|
2016-03-02 15:25:04 +00:00
|
|
|
|
|
2018-02-19 16:53:29 +00:00
|
|
|
|
class RegisterUserFromOrgInviteForm(StripWhitespaceForm):
|
|
|
|
|
|
def __init__(self, invited_org_user):
|
|
|
|
|
|
super().__init__(
|
2023-07-12 12:09:44 -04:00
|
|
|
|
organization=invited_org_user.organization,
|
2019-05-23 15:27:35 +01:00
|
|
|
|
email_address=invited_org_user.email_address,
|
2018-02-19 16:53:29 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
2020-08-07 09:53:09 +01:00
|
|
|
|
name = GovukTextInputField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Full name", validators=[DataRequired(message="Cannot be empty")]
|
2018-02-19 16:53:29 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
mobile_number = InternationalPhoneNumber(
|
|
|
|
|
|
"Mobile number", validators=[DataRequired(message="Cannot be empty")]
|
|
|
|
|
|
)
|
2018-02-19 16:53:29 +00:00
|
|
|
|
password = password()
|
2023-08-25 09:12:23 -07:00
|
|
|
|
organization = HiddenField("organization")
|
|
|
|
|
|
email_address = HiddenField("email_address")
|
|
|
|
|
|
auth_type = HiddenField("auth_type", validators=[DataRequired()])
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
2020-10-29 16:25:02 +00:00
|
|
|
|
def govuk_checkbox_field_widget(self, field, param_extensions=None, **kwargs):
|
|
|
|
|
|
# error messages
|
|
|
|
|
|
error_message = None
|
|
|
|
|
|
if field.errors:
|
|
|
|
|
|
error_message = {
|
|
|
|
|
|
"attributes": {
|
|
|
|
|
|
"data-module": "track-error",
|
|
|
|
|
|
"data-error-type": field.errors[0],
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"data-error-label": field.name,
|
2020-10-29 16:25:02 +00:00
|
|
|
|
},
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"text": field.errors[0],
|
2020-10-29 16:25:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
params = {
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"name": field.name,
|
|
|
|
|
|
"errorMessage": error_message,
|
|
|
|
|
|
"items": [
|
2020-10-29 16:25:02 +00:00
|
|
|
|
{
|
|
|
|
|
|
"name": field.name,
|
|
|
|
|
|
"id": field.id,
|
|
|
|
|
|
"text": field.label.text,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"value": "y",
|
|
|
|
|
|
"checked": field.data,
|
2020-10-29 16:25:02 +00:00
|
|
|
|
}
|
2023-08-25 09:12:23 -07:00
|
|
|
|
],
|
2020-10-29 16:25:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# extend default params with any sent in during instantiation
|
|
|
|
|
|
if self.param_extensions:
|
2021-01-22 15:40:28 +00:00
|
|
|
|
merge_jsonlike(params, self.param_extensions)
|
2020-10-29 16:25:02 +00:00
|
|
|
|
|
|
|
|
|
|
# add any sent in though use in templates
|
|
|
|
|
|
if param_extensions:
|
2021-01-22 15:40:28 +00:00
|
|
|
|
merge_jsonlike(params, param_extensions)
|
2020-10-29 16:25:02 +00:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return Markup(render_template("forms/fields/checkboxes/macro.njk", params=params))
|
2020-10-29 16:25:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def govuk_checkboxes_field_widget(
|
|
|
|
|
|
self, field, wrap_in_collapsible=False, param_extensions=None, **kwargs
|
|
|
|
|
|
):
|
2020-10-29 16:25:02 +00:00
|
|
|
|
def _wrap_in_collapsible(field_label, checkboxes_string):
|
|
|
|
|
|
# wrap the checkboxes HTML in the HTML needed by the collapisble JS
|
|
|
|
|
|
result = Markup(
|
|
|
|
|
|
f'<div class="selection-wrapper"'
|
|
|
|
|
|
f' data-module="collapsible-checkboxes"'
|
|
|
|
|
|
f' data-field-label="{field_label}">'
|
2023-08-25 09:12:23 -07:00
|
|
|
|
f" {checkboxes_string}"
|
|
|
|
|
|
f"</div>"
|
2020-10-29 16:25:02 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
# error messages
|
|
|
|
|
|
error_message = None
|
|
|
|
|
|
if field.errors:
|
|
|
|
|
|
error_message = {
|
|
|
|
|
|
"attributes": {
|
|
|
|
|
|
"data-module": "track-error",
|
|
|
|
|
|
"data-error-type": field.errors[0],
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"data-error-label": field.name,
|
2020-10-29 16:25:02 +00:00
|
|
|
|
},
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"text": field.errors[0],
|
2020-10-29 16:25:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# returns either a list or a hierarchy of lists
|
|
|
|
|
|
# depending on how get_items_from_options is implemented
|
|
|
|
|
|
items = self.get_items_from_options(field)
|
|
|
|
|
|
|
|
|
|
|
|
params = {
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"name": field.name,
|
2020-10-29 16:25:02 +00:00
|
|
|
|
"fieldset": {
|
|
|
|
|
|
"attributes": {"id": field.name},
|
|
|
|
|
|
"legend": {
|
|
|
|
|
|
"text": field.label.text,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
},
|
2020-10-29 16:25:02 +00:00
|
|
|
|
},
|
|
|
|
|
|
"asList": self.render_as_list,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"errorMessage": error_message,
|
|
|
|
|
|
"items": items,
|
2020-10-29 16:25:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# extend default params with any sent in during instantiation
|
|
|
|
|
|
if self.param_extensions:
|
2021-01-22 15:40:28 +00:00
|
|
|
|
merge_jsonlike(params, self.param_extensions)
|
2020-10-29 16:25:02 +00:00
|
|
|
|
|
|
|
|
|
|
# add any sent in though use in templates
|
|
|
|
|
|
if param_extensions:
|
2021-01-22 15:40:28 +00:00
|
|
|
|
merge_jsonlike(params, param_extensions)
|
2020-10-29 16:25:02 +00:00
|
|
|
|
|
|
|
|
|
|
if wrap_in_collapsible:
|
|
|
|
|
|
# add a blank hint to act as an ARIA live-region
|
|
|
|
|
|
params.update(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
{
|
|
|
|
|
|
"hint": {
|
|
|
|
|
|
"html": '<div class="selection-summary" role="region" aria-live="polite"></div>'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
2020-10-29 16:25:02 +00:00
|
|
|
|
|
|
|
|
|
|
return _wrap_in_collapsible(
|
|
|
|
|
|
self.field_label,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
Markup(render_template("forms/fields/checkboxes/macro.njk", params=params)),
|
2022-08-05 00:25:03 -07:00
|
|
|
|
)
|
2020-10-29 16:25:02 +00:00
|
|
|
|
else:
|
|
|
|
|
|
return Markup(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
render_template("forms/fields/checkboxes/macro.njk", params=params)
|
|
|
|
|
|
)
|
2020-10-29 16:25:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
2020-06-23 14:53:51 +01:00
|
|
|
|
def govuk_radios_field_widget(self, field, param_extensions=None, **kwargs):
|
|
|
|
|
|
# error messages
|
|
|
|
|
|
error_message = None
|
|
|
|
|
|
if field.errors:
|
|
|
|
|
|
error_message = {
|
|
|
|
|
|
"attributes": {
|
|
|
|
|
|
"data-module": "track-error",
|
|
|
|
|
|
"data-error-type": field.errors[0],
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"data-error-label": field.name,
|
2020-06-23 14:53:51 +01:00
|
|
|
|
},
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"text": field.errors[0],
|
2020-06-23 14:53:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# returns either a list or a hierarchy of lists
|
|
|
|
|
|
# depending on how get_items_from_options is implemented
|
|
|
|
|
|
items = self.get_items_from_options(field)
|
|
|
|
|
|
|
|
|
|
|
|
params = {
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"name": field.name,
|
2020-06-23 14:53:51 +01:00
|
|
|
|
"fieldset": {
|
|
|
|
|
|
"attributes": {"id": field.name},
|
|
|
|
|
|
"legend": {
|
|
|
|
|
|
"text": field.label.text,
|
2023-08-28 12:29:25 -04:00
|
|
|
|
"classes": "usa-legend text-bold",
|
2023-08-25 09:12:23 -07:00
|
|
|
|
},
|
2020-06-23 14:53:51 +01:00
|
|
|
|
},
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"errorMessage": error_message,
|
|
|
|
|
|
"items": items,
|
2020-06-23 14:53:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# extend default params with any sent in during instantiation
|
|
|
|
|
|
if self.param_extensions:
|
2021-01-22 15:40:28 +00:00
|
|
|
|
merge_jsonlike(params, self.param_extensions)
|
2020-06-23 14:53:51 +01:00
|
|
|
|
|
|
|
|
|
|
# add any sent in though use in templates
|
|
|
|
|
|
if param_extensions:
|
2021-01-22 15:40:28 +00:00
|
|
|
|
merge_jsonlike(params, param_extensions)
|
2020-06-23 14:53:51 +01:00
|
|
|
|
|
|
|
|
|
|
return Markup(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
render_template("components/components/radios/template.njk", params=params)
|
|
|
|
|
|
)
|
2020-06-23 14:53:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-10-29 16:25:02 +00:00
|
|
|
|
class GovukCheckboxField(BooleanField):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, label="", validators=None, param_extensions=None, **kwargs):
|
|
|
|
|
|
super(GovukCheckboxField, self).__init__(
|
|
|
|
|
|
label, validators, false_values=None, **kwargs
|
|
|
|
|
|
)
|
2020-05-07 15:44:13 +01:00
|
|
|
|
self.param_extensions = param_extensions
|
|
|
|
|
|
|
2020-05-07 15:30:20 +01:00
|
|
|
|
# self.__call__ renders the HTML for the field by:
|
|
|
|
|
|
# 1. delegating to self.meta.render_field which
|
|
|
|
|
|
# 2. calls field.widget
|
|
|
|
|
|
# this bypasses that by making self.widget a method with the same interface as widget.__call__
|
|
|
|
|
|
def widget(self, field, param_extensions=None, **kwargs):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return govuk_checkbox_field_widget(
|
|
|
|
|
|
self, field, param_extensions=param_extensions, **kwargs
|
|
|
|
|
|
)
|
2020-05-07 15:30:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-04-02 18:06:01 +01:00
|
|
|
|
# based on work done by @richardjpope: https://github.com/richardjpope/recourse/blob/master/recourse/forms.py#L6
|
2020-10-29 16:25:02 +00:00
|
|
|
|
class GovukCheckboxesField(SelectMultipleField):
|
2020-04-23 12:15:24 +01:00
|
|
|
|
render_as_list = False
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, label="", validators=None, param_extensions=None, **kwargs):
|
2020-10-29 14:58:39 +00:00
|
|
|
|
super(GovukCheckboxesField, self).__init__(label, validators, **kwargs)
|
2020-04-02 18:06:01 +01:00
|
|
|
|
self.param_extensions = param_extensions
|
|
|
|
|
|
|
2020-04-23 12:15:24 +01:00
|
|
|
|
def get_item_from_option(self, option):
|
|
|
|
|
|
return {
|
|
|
|
|
|
"name": option.name,
|
|
|
|
|
|
"id": option.id,
|
|
|
|
|
|
"text": option.label.text,
|
|
|
|
|
|
"value": str(option.data), # to protect against non-string types like uuids
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"checked": option.checked,
|
2020-04-23 12:15:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_items_from_options(self, field):
|
|
|
|
|
|
return [self.get_item_from_option(option) for option in field]
|
|
|
|
|
|
|
2020-04-02 18:06:01 +01:00
|
|
|
|
# self.__call__ renders the HTML for the field by:
|
|
|
|
|
|
# 1. delegating to self.meta.render_field which
|
|
|
|
|
|
# 2. calls field.widget
|
|
|
|
|
|
# this bypasses that by making self.widget a method with the same interface as widget.__call__
|
2020-04-09 11:51:11 +01:00
|
|
|
|
def widget(self, field, param_extensions=None, **kwargs):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return govuk_checkboxes_field_widget(
|
|
|
|
|
|
self, field, param_extensions=param_extensions, **kwargs
|
|
|
|
|
|
)
|
2020-04-02 18:06:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-10-29 16:25:02 +00:00
|
|
|
|
# Wraps checkboxes rendering in HTML needed by the collapsible JS
|
|
|
|
|
|
class GovukCollapsibleCheckboxesField(GovukCheckboxesField):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(
|
|
|
|
|
|
self, label="", validators=None, field_label="", param_extensions=None, **kwargs
|
|
|
|
|
|
):
|
|
|
|
|
|
super(GovukCollapsibleCheckboxesField, self).__init__(
|
|
|
|
|
|
label, validators, param_extensions, **kwargs
|
|
|
|
|
|
)
|
2020-04-23 12:12:55 +01:00
|
|
|
|
self.field_label = field_label
|
|
|
|
|
|
|
|
|
|
|
|
def widget(self, field, **kwargs):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return govuk_checkboxes_field_widget(
|
|
|
|
|
|
self, field, wrap_in_collapsible=True, param_extensions=None, **kwargs
|
|
|
|
|
|
)
|
2020-04-23 12:12:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-10-29 16:25:02 +00:00
|
|
|
|
# GovukCollapsibleCheckboxesField adds an ARIA live-region to the hint and wraps the render in HTML needed by the
|
2020-04-23 12:15:24 +01:00
|
|
|
|
# collapsible JS
|
|
|
|
|
|
# NestedFieldMixin puts the items into a tree hierarchy, pre-rendering the sub-trees of the top-level items
|
2023-08-25 09:12:23 -07:00
|
|
|
|
class GovukCollapsibleNestedCheckboxesField(
|
|
|
|
|
|
NestedFieldMixin, GovukCollapsibleCheckboxesField
|
|
|
|
|
|
):
|
2020-04-23 12:15:24 +01:00
|
|
|
|
NONE_OPTION_VALUE = None
|
|
|
|
|
|
render_as_list = True
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-23 14:53:51 +01:00
|
|
|
|
class GovukRadiosField(RadioField):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, label="", validators=None, param_extensions=None, **kwargs):
|
2020-06-23 14:53:51 +01:00
|
|
|
|
super(GovukRadiosField, self).__init__(label, validators, **kwargs)
|
|
|
|
|
|
self.param_extensions = param_extensions
|
|
|
|
|
|
|
|
|
|
|
|
def get_item_from_option(self, option):
|
|
|
|
|
|
return {
|
|
|
|
|
|
"name": option.name,
|
|
|
|
|
|
"id": option.id,
|
|
|
|
|
|
"text": option.label.text,
|
|
|
|
|
|
"value": str(option.data), # to protect against non-string types like uuids
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"checked": option.checked,
|
2020-06-23 14:53:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_items_from_options(self, field):
|
|
|
|
|
|
return [self.get_item_from_option(option) for option in field]
|
|
|
|
|
|
|
|
|
|
|
|
# self.__call__ renders the HTML for the field by:
|
|
|
|
|
|
# 1. delegating to self.meta.render_field which
|
|
|
|
|
|
# 2. calls field.widget
|
|
|
|
|
|
# this bypasses that by making self.widget a method with the same interface as widget.__call__
|
|
|
|
|
|
def widget(self, field, param_extensions=None, **kwargs):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return govuk_radios_field_widget(
|
|
|
|
|
|
self, field, param_extensions=param_extensions, **kwargs
|
|
|
|
|
|
)
|
2020-06-23 14:53:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
2021-06-07 17:06:15 +01:00
|
|
|
|
class OptionalGovukRadiosField(GovukRadiosField):
|
|
|
|
|
|
def pre_validate(self, form):
|
|
|
|
|
|
if self.data is None:
|
|
|
|
|
|
return
|
|
|
|
|
|
super().pre_validate(form)
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-26 22:18:24 +01:00
|
|
|
|
class OnOffField(GovukRadiosField):
|
|
|
|
|
|
def __init__(self, label, choices=None, *args, **kwargs):
|
|
|
|
|
|
choices = choices or [
|
2023-08-25 09:12:23 -07:00
|
|
|
|
(True, "On"),
|
|
|
|
|
|
(False, "Off"),
|
2020-06-26 22:18:24 +01:00
|
|
|
|
]
|
|
|
|
|
|
super().__init__(
|
|
|
|
|
|
label,
|
2023-11-28 17:33:17 -05:00
|
|
|
|
*args,
|
2020-06-26 22:18:24 +01:00
|
|
|
|
choices=choices,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
thing=f"{choices[0][1].lower()} or {choices[1][1].lower()}",
|
2020-06-26 22:18:24 +01:00
|
|
|
|
**kwargs,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def process_formdata(self, valuelist):
|
|
|
|
|
|
if valuelist:
|
|
|
|
|
|
value = valuelist[0]
|
2023-08-25 09:12:23 -07:00
|
|
|
|
self.data = (value == "True") if value in ["True", "False"] else value
|
2020-06-26 22:18:24 +01:00
|
|
|
|
|
|
|
|
|
|
def iter_choices(self):
|
|
|
|
|
|
for value, label in self.choices:
|
|
|
|
|
|
# This overrides WTForms default behaviour which is to check
|
|
|
|
|
|
# self.coerce(value) == self.data
|
|
|
|
|
|
# where self.coerce returns a string for a boolean input
|
2023-08-25 09:12:23 -07:00
|
|
|
|
yield (value, label, (self.data in {value, self.coerce(value)}))
|
2020-06-26 22:18:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
|
class OrganizationTypeField(GovukRadiosField):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, *args, include_only=None, validators=None, **kwargs):
|
2020-06-26 20:39:20 +01:00
|
|
|
|
super().__init__(
|
|
|
|
|
|
*args,
|
|
|
|
|
|
choices=[
|
2023-08-25 09:12:23 -07:00
|
|
|
|
(value, label)
|
|
|
|
|
|
for value, label in Organization.TYPE_LABELS.items()
|
2020-06-26 20:39:20 +01:00
|
|
|
|
if not include_only or value in include_only
|
|
|
|
|
|
],
|
2023-08-25 09:12:23 -07:00
|
|
|
|
thing="the type of organization",
|
2020-06-26 20:39:20 +01:00
|
|
|
|
validators=validators or [],
|
2023-08-25 09:12:23 -07:00
|
|
|
|
**kwargs,
|
2020-06-26 20:39:20 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-26 22:32:42 +01:00
|
|
|
|
class GovukRadiosFieldWithNoneOption(FieldWithNoneOption, GovukRadiosField):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-07-22 14:51:52 +01:00
|
|
|
|
# guard against data entries that aren't a known permission
|
2020-04-02 18:08:50 +01:00
|
|
|
|
def filter_by_permissions(valuelist):
|
|
|
|
|
|
if valuelist is None:
|
|
|
|
|
|
return None
|
|
|
|
|
|
else:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return [
|
|
|
|
|
|
entry
|
|
|
|
|
|
for entry in valuelist
|
|
|
|
|
|
if any(entry in option for option in permission_options)
|
|
|
|
|
|
]
|
2020-04-02 18:08:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
2022-02-23 19:57:27 +00:00
|
|
|
|
class AuthTypeForm(StripWhitespaceForm):
|
|
|
|
|
|
auth_type = GovukRadiosField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Sign in using",
|
2022-02-23 19:57:27 +00:00
|
|
|
|
choices=[
|
2023-08-25 09:12:23 -07:00
|
|
|
|
("sms_auth", format_auth_type("sms_auth")),
|
|
|
|
|
|
("email_auth", format_auth_type("email_auth")),
|
|
|
|
|
|
],
|
2022-02-23 19:57:27 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-13 11:10:34 +01:00
|
|
|
|
class BasePermissionsForm(StripWhitespaceForm):
|
2019-02-25 16:23:28 +00:00
|
|
|
|
def __init__(self, all_template_folders=None, *args, **kwargs):
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
2019-05-17 11:03:41 +01:00
|
|
|
|
self.folder_permissions.choices = []
|
2019-02-27 15:45:18 +00:00
|
|
|
|
if all_template_folders is not None:
|
2019-02-25 16:23:28 +00:00
|
|
|
|
self.folder_permissions.all_template_folders = all_template_folders
|
|
|
|
|
|
self.folder_permissions.choices = [
|
2023-08-25 09:12:23 -07:00
|
|
|
|
(item["id"], item["name"])
|
|
|
|
|
|
for item in ([{"name": "Templates", "id": None}] + all_template_folders)
|
2019-02-25 16:23:28 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
2020-10-29 14:58:39 +00:00
|
|
|
|
folder_permissions = GovukCollapsibleNestedCheckboxesField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Folders this team member can see", field_label="folder"
|
|
|
|
|
|
)
|
2018-06-13 12:07:08 +01:00
|
|
|
|
|
2020-06-25 16:24:34 +01:00
|
|
|
|
login_authentication = GovukRadiosField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Sign in using",
|
2017-11-01 15:36:27 +00:00
|
|
|
|
choices=[
|
2023-08-25 09:12:23 -07:00
|
|
|
|
("sms_auth", "Text message code"),
|
|
|
|
|
|
("email_auth", "Email link"),
|
2017-11-01 15:36:27 +00:00
|
|
|
|
],
|
2023-08-25 09:12:23 -07:00
|
|
|
|
thing="how this team member should sign in",
|
|
|
|
|
|
validators=[DataRequired()],
|
2017-11-01 15:36:27 +00:00
|
|
|
|
)
|
2016-03-09 17:42:47 +00:00
|
|
|
|
|
2020-10-29 14:58:39 +00:00
|
|
|
|
permissions_field = GovukCheckboxesField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Permissions",
|
2020-04-02 18:08:50 +01:00
|
|
|
|
filters=[filter_by_permissions],
|
2023-08-25 09:12:23 -07:00
|
|
|
|
choices=[(value, label) for value, label in permission_options],
|
|
|
|
|
|
param_extensions={"hint": {"text": "All team members can see sent messages."}},
|
2020-04-02 18:08:50 +01:00
|
|
|
|
)
|
2020-05-14 16:59:34 +01:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def permissions(self):
|
2020-07-31 15:16:56 +01:00
|
|
|
|
return set(self.permissions_field.data)
|
2018-08-08 08:45:58 +01:00
|
|
|
|
|
2018-08-06 11:10:37 +01:00
|
|
|
|
@classmethod
|
2019-02-25 16:23:28 +00:00
|
|
|
|
def from_user(cls, user, service_id, **kwargs):
|
2021-06-10 23:09:36 +01:00
|
|
|
|
form = cls(
|
2019-02-25 16:23:28 +00:00
|
|
|
|
**kwargs,
|
2018-08-06 11:10:37 +01:00
|
|
|
|
**{
|
2021-07-22 14:51:52 +01:00
|
|
|
|
"permissions_field": (
|
|
|
|
|
|
user.permissions_for_service(service_id) & all_ui_permissions
|
|
|
|
|
|
)
|
2018-08-06 11:10:37 +01:00
|
|
|
|
},
|
2023-08-25 09:12:23 -07:00
|
|
|
|
login_authentication=user.auth_type,
|
2018-08-06 11:10:37 +01:00
|
|
|
|
)
|
2021-06-30 15:15:00 +01:00
|
|
|
|
|
2021-06-10 23:09:36 +01:00
|
|
|
|
return form
|
2018-06-12 11:51:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-07-31 15:16:56 +01:00
|
|
|
|
class PermissionsForm(BasePermissionsForm):
|
2020-07-13 11:10:34 +01:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
class BaseInviteUserForm:
|
2016-10-26 14:01:01 +01:00
|
|
|
|
email_address = email_address(gov_user=False)
|
2016-03-09 13:00:52 +00:00
|
|
|
|
|
2021-07-15 14:21:58 +01:00
|
|
|
|
def __init__(self, inviter_email_address, *args, **kwargs):
|
2018-06-12 11:51:37 +01:00
|
|
|
|
super().__init__(*args, **kwargs)
|
2021-07-15 14:21:58 +01:00
|
|
|
|
self.inviter_email_address = inviter_email_address
|
2016-03-09 13:00:52 +00:00
|
|
|
|
|
|
|
|
|
|
def validate_email_address(self, field):
|
2021-07-13 14:43:26 +01:00
|
|
|
|
if current_user.platform_admin:
|
|
|
|
|
|
return
|
2021-07-15 14:21:58 +01:00
|
|
|
|
if field.data.lower() == self.inviter_email_address.lower():
|
2019-09-12 16:42:33 +01:00
|
|
|
|
raise ValidationError("You cannot send an invitation to yourself")
|
2016-03-09 13:00:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
2020-07-13 11:10:34 +01:00
|
|
|
|
class InviteUserForm(BaseInviteUserForm, PermissionsForm):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-07-13 14:38:13 +01:00
|
|
|
|
class InviteOrgUserForm(BaseInviteUserForm, StripWhitespaceForm):
|
|
|
|
|
|
pass
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class TwoFactorForm(StripWhitespaceForm):
|
2016-01-27 12:22:32 +00:00
|
|
|
|
def __init__(self, validate_code_func, *args, **kwargs):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"""
|
2016-01-07 12:43:10 +00:00
|
|
|
|
Keyword arguments:
|
2016-01-27 12:22:32 +00:00
|
|
|
|
validate_code_func -- Validates the code with the API.
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"""
|
2016-01-27 12:22:32 +00:00
|
|
|
|
self.validate_code_func = validate_code_func
|
2016-01-07 12:43:10 +00:00
|
|
|
|
super(TwoFactorForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
sms_code = SMSCode("Text message code")
|
2015-12-08 12:36:54 +00:00
|
|
|
|
|
2023-04-26 11:24:01 -04:00
|
|
|
|
def validate(self, extra_validators=None):
|
2018-05-07 21:24:23 +01:00
|
|
|
|
if not self.sms_code.validate(self):
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
is_valid, reason = self.validate_code_func(self.sms_code.data)
|
|
|
|
|
|
|
2016-01-27 12:22:32 +00:00
|
|
|
|
if not is_valid:
|
2018-05-07 21:24:23 +01:00
|
|
|
|
self.sms_code.errors.append(reason)
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
return True
|
2016-01-27 12:22:32 +00:00
|
|
|
|
|
2015-12-07 16:56:11 +00:00
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class TextNotReceivedForm(StripWhitespaceForm):
|
2017-08-29 14:52:24 +01:00
|
|
|
|
mobile_number = international_phone_number()
|
2015-12-15 15:35:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class RenameServiceForm(StripWhitespaceForm):
|
2020-07-14 16:51:11 +01:00
|
|
|
|
name = GovukTextInputField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Service name",
|
2016-03-10 14:29:31 +00:00
|
|
|
|
validators=[
|
2023-08-25 09:12:23 -07:00
|
|
|
|
DataRequired(message="Cannot be empty"),
|
2020-11-30 17:08:40 +00:00
|
|
|
|
MustContainAlphanumericCharacters(),
|
2023-08-25 09:12:23 -07:00
|
|
|
|
Length(max=255, message="Service name must be 255 characters or fewer"),
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
2016-03-10 14:29:31 +00:00
|
|
|
|
|
2016-01-11 13:15:10 +00:00
|
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
|
class RenameOrganizationForm(StripWhitespaceForm):
|
2020-08-04 16:32:40 +01:00
|
|
|
|
name = GovukTextInputField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Organization name",
|
2018-03-06 17:12:31 +00:00
|
|
|
|
validators=[
|
2023-08-25 09:12:23 -07:00
|
|
|
|
DataRequired(message="Cannot be empty"),
|
2020-11-30 17:08:40 +00:00
|
|
|
|
MustContainAlphanumericCharacters(),
|
2023-08-25 09:12:23 -07:00
|
|
|
|
Length(
|
|
|
|
|
|
max=255, message="Organization name must be 255 characters or fewer"
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
2018-03-06 17:12:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
|
class OrganizationOrganizationTypeForm(StripWhitespaceForm):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
organization_type = OrganizationTypeField("What type of organization is this?")
|
2019-02-19 17:26:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
|
class AdminOrganizationDomainsForm(StripWhitespaceForm):
|
2019-02-19 17:26:16 +00:00
|
|
|
|
def populate(self, domains_list):
|
|
|
|
|
|
for index, value in enumerate(domains_list):
|
|
|
|
|
|
self.domains[index].data = value
|
|
|
|
|
|
|
|
|
|
|
|
domains = FieldList(
|
|
|
|
|
|
StripWhitespaceStringField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"",
|
2019-02-19 17:26:16 +00:00
|
|
|
|
validators=[
|
|
|
|
|
|
Optional(),
|
|
|
|
|
|
],
|
2023-08-25 09:12:23 -07:00
|
|
|
|
default="",
|
2019-02-19 17:26:16 +00:00
|
|
|
|
),
|
2019-03-22 16:27:30 +00:00
|
|
|
|
min_entries=20,
|
|
|
|
|
|
max_entries=20,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
label="Domain names",
|
2019-02-19 17:26:16 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class CreateServiceForm(StripWhitespaceForm):
|
2020-08-07 12:18:29 +01:00
|
|
|
|
name = GovukTextInputField(
|
2019-07-16 15:37:27 +01:00
|
|
|
|
"What’s your service called?",
|
2017-10-04 11:49:32 +01:00
|
|
|
|
validators=[
|
2023-08-25 09:12:23 -07:00
|
|
|
|
DataRequired(message="Cannot be empty"),
|
2020-11-30 17:08:40 +00:00
|
|
|
|
MustContainAlphanumericCharacters(),
|
2023-08-25 09:12:23 -07:00
|
|
|
|
Length(max=255, message="Service name must be 255 characters or fewer"),
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
|
|
|
|
|
organization_type = OrganizationTypeField("Where is this service run?")
|
2019-07-16 15:37:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
|
class AdminNewOrganizationForm(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
RenameOrganizationForm, OrganizationOrganizationTypeForm
|
2019-07-01 10:19:33 +01:00
|
|
|
|
):
|
2019-07-01 17:01:11 +01:00
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
2019-07-01 10:19:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
2022-03-15 10:50:18 +00:00
|
|
|
|
class AdminServiceSMSAllowanceForm(StripWhitespaceForm):
|
2020-08-07 10:31:09 +01:00
|
|
|
|
free_sms_allowance = GovukIntegerField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Numbers of text message fragments per year",
|
|
|
|
|
|
validators=[InputRequired(message="Cannot be empty")],
|
2017-10-05 13:49:43 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-03-15 10:50:18 +00:00
|
|
|
|
class AdminServiceMessageLimitForm(StripWhitespaceForm):
|
2020-10-19 17:19:32 +01:00
|
|
|
|
message_limit = GovukIntegerField(
|
2023-12-06 12:26:33 -07:00
|
|
|
|
"Max number of messages the service has per send",
|
2023-08-25 09:12:23 -07:00
|
|
|
|
validators=[DataRequired(message="Cannot be empty")],
|
2020-10-19 17:19:32 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-03-15 10:50:18 +00:00
|
|
|
|
class AdminServiceRateLimitForm(StripWhitespaceForm):
|
2020-10-21 14:30:35 +01:00
|
|
|
|
rate_limit = GovukIntegerField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Number of messages the service can send in a rolling 60 second window",
|
|
|
|
|
|
validators=[DataRequired(message="Cannot be empty")],
|
2020-10-21 14:30:35 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class ConfirmPasswordForm(StripWhitespaceForm):
|
2016-01-22 16:34:36 +00:00
|
|
|
|
def __init__(self, validate_password_func, *args, **kwargs):
|
|
|
|
|
|
self.validate_password_func = validate_password_func
|
|
|
|
|
|
super(ConfirmPasswordForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
password = GovukPasswordField("Enter password")
|
2016-01-11 13:15:10 +00:00
|
|
|
|
|
2016-01-22 16:34:36 +00:00
|
|
|
|
def validate_password(self, field):
|
|
|
|
|
|
if not self.validate_password_func(field.data):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
raise ValidationError("Invalid password")
|
2016-01-22 16:34:36 +00:00
|
|
|
|
|
2016-01-11 13:15:10 +00:00
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class BaseTemplateForm(StripWhitespaceForm):
|
2020-08-07 12:07:16 +01:00
|
|
|
|
name = GovukTextInputField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Template name", validators=[DataRequired(message="Cannot be empty")]
|
|
|
|
|
|
)
|
2016-01-22 12:19:15 +00:00
|
|
|
|
|
2016-01-22 12:15:47 +00:00
|
|
|
|
template_content = TextAreaField(
|
2020-08-07 12:07:16 +01:00
|
|
|
|
"Message",
|
2023-08-25 09:12:23 -07:00
|
|
|
|
validators=[DataRequired(message="Cannot be empty"), NoCommasInPlaceHolders()],
|
2016-04-07 16:02:06 +01:00
|
|
|
|
)
|
2023-08-25 09:12:23 -07:00
|
|
|
|
process_type = HiddenField("normal")
|
2016-01-11 13:15:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-02-15 15:06:47 +00:00
|
|
|
|
class SMSTemplateForm(BaseTemplateForm):
|
|
|
|
|
|
def validate_template_content(self, field):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
OnlySMSCharacters(template_type="sms")(None, field)
|
2017-02-15 15:06:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EmailTemplateForm(BaseTemplateForm):
|
2016-04-14 14:04:41 +01:00
|
|
|
|
subject = TextAreaField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Subject", validators=[DataRequired(message="Cannot be empty")]
|
|
|
|
|
|
)
|
2016-02-22 14:45:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class ForgotPasswordForm(StripWhitespaceForm):
|
2016-10-26 14:01:01 +01:00
|
|
|
|
email_address = email_address(gov_user=False)
|
2016-01-04 14:00:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class NewPasswordForm(StripWhitespaceForm):
|
2016-01-08 12:00:52 +00:00
|
|
|
|
new_password = password()
|
2016-01-11 15:00:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class ChangePasswordForm(StripWhitespaceForm):
|
2016-01-27 12:22:32 +00:00
|
|
|
|
def __init__(self, validate_password_func, *args, **kwargs):
|
|
|
|
|
|
self.validate_password_func = validate_password_func
|
|
|
|
|
|
super(ChangePasswordForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
old_password = password("Current password")
|
|
|
|
|
|
new_password = password("New password")
|
2016-01-12 11:25:46 +00:00
|
|
|
|
|
2016-01-27 12:22:32 +00:00
|
|
|
|
def validate_old_password(self, field):
|
|
|
|
|
|
if not self.validate_password_func(field.data):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
raise ValidationError("Invalid password")
|
2016-01-27 12:22:32 +00:00
|
|
|
|
|
2016-01-12 11:25:46 +00:00
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class CsvUploadForm(StripWhitespaceForm):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
file = FileField(
|
|
|
|
|
|
"Add recipients",
|
|
|
|
|
|
validators=[
|
|
|
|
|
|
DataRequired(message="Please pick a file"),
|
|
|
|
|
|
CsvFileValidator(),
|
|
|
|
|
|
FileSize(max_size=10e6, message="File must be smaller than 10Mb"), # 10Mb
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
2016-01-12 10:28:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class ChangeNameForm(StripWhitespaceForm):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
new_name = GovukTextInputField("Your name")
|
2016-01-12 10:28:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
2024-01-08 10:13:26 -08:00
|
|
|
|
# https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
2023-12-04 14:52:48 -08:00
|
|
|
|
class ChangePreferredTimezoneForm(StripWhitespaceForm):
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
super(ChangePreferredTimezoneForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
self.new_preferred_timezone.choices = [
|
2024-01-08 10:13:26 -08:00
|
|
|
|
("America/Puerto_Rico", "America/Puerto_Rico"),
|
2023-12-04 14:52:48 -08:00
|
|
|
|
("US/Eastern", "US/Eastern"),
|
|
|
|
|
|
("US/Central", "US/Central"),
|
|
|
|
|
|
("US/Mountain", "US/Mountain"),
|
|
|
|
|
|
("US/Pacific", "US/Pacific"),
|
2024-01-08 10:13:26 -08:00
|
|
|
|
("US/Alaska", "US/Alaska"),
|
|
|
|
|
|
("US/Hawaii", "US/Hawaii"),
|
|
|
|
|
|
("US/Aleutian", "US/Aleutian"),
|
|
|
|
|
|
("US/Samoa", "US/Samoa"),
|
2023-12-04 14:52:48 -08:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
new_preferred_timezone = GovukRadiosField(
|
|
|
|
|
|
"What timezone would you like to use?",
|
|
|
|
|
|
default="US/Eastern",
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class ChangeEmailForm(StripWhitespaceForm):
|
2016-01-27 12:22:32 +00:00
|
|
|
|
def __init__(self, validate_email_func, *args, **kwargs):
|
|
|
|
|
|
self.validate_email_func = validate_email_func
|
|
|
|
|
|
super(ChangeEmailForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
2016-01-12 10:28:14 +00:00
|
|
|
|
email_address = email_address()
|
|
|
|
|
|
|
2016-01-27 12:22:32 +00:00
|
|
|
|
def validate_email_address(self, field):
|
2021-12-10 16:56:08 +00:00
|
|
|
|
# The validate_email_func can be used to call API to check if the email address is already in
|
|
|
|
|
|
# use. We don't want to run that check for invalid email addresses, since that will cause an error.
|
|
|
|
|
|
# If there are any other validation errors on the email_address, we should skip this check.
|
|
|
|
|
|
if self.email_address.errors:
|
|
|
|
|
|
return
|
|
|
|
|
|
|
2016-01-27 12:22:32 +00:00
|
|
|
|
is_valid = self.validate_email_func(field.data)
|
2018-02-19 16:53:29 +00:00
|
|
|
|
if is_valid:
|
2016-01-27 12:22:32 +00:00
|
|
|
|
raise ValidationError("The email address is already in use")
|
|
|
|
|
|
|
2016-01-12 10:28:14 +00:00
|
|
|
|
|
2019-04-18 16:03:13 +01:00
|
|
|
|
class ChangeNonGovEmailForm(ChangeEmailForm):
|
|
|
|
|
|
email_address = email_address(gov_user=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class ChangeMobileNumberForm(StripWhitespaceForm):
|
2017-08-29 14:52:24 +01:00
|
|
|
|
mobile_number = international_phone_number()
|
2016-01-12 10:28:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class ChooseTimeForm(StripWhitespaceForm):
|
2016-08-07 09:17:49 +01:00
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
super(ChooseTimeForm, self).__init__(*args, **kwargs)
|
2023-08-25 09:12:23 -07:00
|
|
|
|
self.scheduled_for.choices = [("", "Now")] + [
|
|
|
|
|
|
get_time_value_and_label(hour)
|
|
|
|
|
|
for hour in get_next_hours_until(get_furthest_possible_scheduled_time())
|
2016-08-07 09:17:49 +01:00
|
|
|
|
]
|
2023-08-25 09:12:23 -07:00
|
|
|
|
self.scheduled_for.categories = get_next_days_until(
|
|
|
|
|
|
get_furthest_possible_scheduled_time()
|
|
|
|
|
|
)
|
2016-08-07 09:17:49 +01:00
|
|
|
|
|
2021-01-26 15:43:59 +00:00
|
|
|
|
scheduled_for = GovukRadiosField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"When should Notify send these messages?",
|
|
|
|
|
|
default="",
|
2016-08-07 09:17:49 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class CreateKeyForm(StripWhitespaceForm):
|
2018-11-13 14:27:01 +00:00
|
|
|
|
def __init__(self, existing_keys, *args, **kwargs):
|
2018-11-13 09:57:17 +00:00
|
|
|
|
self.existing_key_names = [
|
2023-08-25 09:12:23 -07:00
|
|
|
|
key["name"].lower() for key in existing_keys if not key["expiry_date"]
|
2018-11-13 09:57:17 +00:00
|
|
|
|
]
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
2016-01-21 14:15:36 +00:00
|
|
|
|
|
2021-01-12 15:04:54 +00:00
|
|
|
|
key_type = GovukRadiosField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Type of key",
|
|
|
|
|
|
thing="the type of key",
|
2016-06-29 17:10:49 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
key_name = GovukTextInputField(
|
|
|
|
|
|
"Name for this key",
|
|
|
|
|
|
validators=[DataRequired(message="You need to give the key a name")],
|
|
|
|
|
|
)
|
2016-01-21 14:15:36 +00:00
|
|
|
|
|
|
|
|
|
|
def validate_key_name(self, key_name):
|
2016-01-21 16:52:01 +00:00
|
|
|
|
if key_name.data.lower() in self.existing_key_names:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
raise ValidationError("A key with this name already exists")
|
2016-04-19 13:51:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
2019-02-15 11:03:19 +00:00
|
|
|
|
class EstimateUsageForm(StripWhitespaceForm):
|
2019-02-15 13:34:29 +00:00
|
|
|
|
volume_email = ForgivingIntegerField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"How many emails do you expect to send in the next year?",
|
|
|
|
|
|
things="emails",
|
|
|
|
|
|
format_error_suffix="you expect to send",
|
2016-10-24 15:46:22 +01:00
|
|
|
|
)
|
2019-02-15 13:34:29 +00:00
|
|
|
|
volume_sms = ForgivingIntegerField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"How many text messages do you expect to send in the next year?",
|
|
|
|
|
|
things="text messages",
|
|
|
|
|
|
format_error_suffix="you expect to send",
|
2016-10-24 13:38:55 +01:00
|
|
|
|
)
|
2020-06-26 21:56:08 +01:00
|
|
|
|
consent_to_research = GovukRadiosField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Can we contact you when we’re doing user research?",
|
2018-08-30 11:51:34 +01:00
|
|
|
|
choices=[
|
2023-08-25 09:12:23 -07:00
|
|
|
|
("yes", "Yes"),
|
|
|
|
|
|
("no", "No"),
|
2018-08-30 11:51:34 +01:00
|
|
|
|
],
|
2023-08-25 09:12:23 -07:00
|
|
|
|
thing="yes or no",
|
2020-06-26 21:56:08 +01:00
|
|
|
|
param_extensions={
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"hint": {
|
|
|
|
|
|
"text": "You do not have to take part and you can unsubscribe at any time"
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2018-08-30 11:51:34 +01:00
|
|
|
|
)
|
2016-05-11 09:43:55 +01:00
|
|
|
|
|
2019-02-15 13:34:29 +00:00
|
|
|
|
at_least_one_volume_filled = True
|
|
|
|
|
|
|
|
|
|
|
|
def validate(self, *args, **kwargs):
|
2022-12-05 15:33:44 -05:00
|
|
|
|
if self.volume_email.data == self.volume_sms.data == 0:
|
2019-02-15 13:34:29 +00:00
|
|
|
|
self.at_least_one_volume_filled = False
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
return super().validate(*args, **kwargs)
|
|
|
|
|
|
|
2016-05-11 09:43:55 +01:00
|
|
|
|
|
2018-08-09 11:59:05 +01:00
|
|
|
|
class ServiceContactDetailsForm(StripWhitespaceForm):
|
|
|
|
|
|
contact_details_type = RadioField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Type of contact details",
|
2018-08-09 11:59:05 +01:00
|
|
|
|
choices=[
|
2023-08-25 09:12:23 -07:00
|
|
|
|
("url", "Link"),
|
|
|
|
|
|
("email_address", "Email address"),
|
|
|
|
|
|
("phone_number", "Phone number"),
|
2018-08-09 11:59:05 +01:00
|
|
|
|
],
|
2018-06-05 10:37:41 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
2020-08-07 12:21:05 +01:00
|
|
|
|
url = GovukTextInputField("URL")
|
|
|
|
|
|
email_address = GovukEmailField("Email address")
|
2020-08-13 11:40:09 +01:00
|
|
|
|
# This is a text field because the number provided by the user can also be a short code
|
2020-08-07 12:21:05 +01:00
|
|
|
|
phone_number = GovukTextInputField("Phone number")
|
2018-08-09 11:59:05 +01:00
|
|
|
|
|
2023-04-26 11:24:01 -04:00
|
|
|
|
def validate(self, extra_validators=None):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if self.contact_details_type.data == "url":
|
|
|
|
|
|
self.url.validators = [DataRequired(), URL(message="Must be a valid URL")]
|
2018-08-09 11:59:05 +01:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
elif self.contact_details_type.data == "email_address":
|
|
|
|
|
|
self.email_address.validators = [
|
|
|
|
|
|
DataRequired(),
|
|
|
|
|
|
Length(min=5, max=255),
|
|
|
|
|
|
ValidEmail(),
|
|
|
|
|
|
]
|
2018-08-09 11:59:05 +01:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
elif self.contact_details_type.data == "phone_number":
|
2018-08-09 11:59:05 +01:00
|
|
|
|
|
|
|
|
|
|
def valid_phone_number(self, num):
|
|
|
|
|
|
try:
|
2022-12-22 22:11:07 -05:00
|
|
|
|
validate_phone_number(num.data, international=True)
|
2018-08-09 11:59:05 +01:00
|
|
|
|
return True
|
|
|
|
|
|
except InvalidPhoneError:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
raise ValidationError("Must be a valid phone number")
|
|
|
|
|
|
|
|
|
|
|
|
self.phone_number.validators = [
|
|
|
|
|
|
DataRequired(),
|
|
|
|
|
|
Length(min=5, max=20),
|
|
|
|
|
|
valid_phone_number,
|
|
|
|
|
|
]
|
2018-08-09 11:59:05 +01:00
|
|
|
|
|
2023-04-26 11:24:01 -04:00
|
|
|
|
return super().validate(extra_validators)
|
2018-08-09 11:59:05 +01:00
|
|
|
|
|
2018-06-05 10:37:41 +01:00
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class ServiceReplyToEmailForm(StripWhitespaceForm):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
email_address = email_address(label="Reply-to email address", gov_user=False)
|
2020-10-29 14:58:39 +00:00
|
|
|
|
is_default = GovukCheckboxField("Make this email address the default")
|
2016-07-01 13:47:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class ServiceSmsSenderForm(StripWhitespaceForm):
|
2020-08-04 16:58:04 +01:00
|
|
|
|
sms_sender = GovukTextInputField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Text message sender",
|
2016-08-22 16:10:57 +01:00
|
|
|
|
validators=[
|
2019-09-12 16:42:33 +01:00
|
|
|
|
DataRequired(message="Cannot be empty"),
|
2018-01-31 10:26:37 +00:00
|
|
|
|
Length(max=11, message="Enter 11 characters or fewer"),
|
2020-04-21 12:51:54 +01:00
|
|
|
|
Length(min=3, message="Enter 3 characters or more"),
|
2021-07-26 15:10:57 +01:00
|
|
|
|
LettersNumbersSingleQuotesFullStopsAndUnderscoresOnly(),
|
2018-02-28 11:50:41 +00:00
|
|
|
|
DoesNotStartWithDoubleZero(),
|
2023-08-25 09:12:23 -07:00
|
|
|
|
],
|
2016-08-22 16:10:57 +01:00
|
|
|
|
)
|
2020-10-29 14:58:39 +00:00
|
|
|
|
is_default = GovukCheckboxField("Make this text message sender the default")
|
2016-07-01 13:47:22 +01:00
|
|
|
|
|
2016-08-08 10:28:40 +01:00
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class ServiceEditInboundNumberForm(StripWhitespaceForm):
|
2020-10-29 14:58:39 +00:00
|
|
|
|
is_default = GovukCheckboxField("Make this text message sender the default")
|
2017-10-30 14:30:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
2022-03-15 10:50:18 +00:00
|
|
|
|
class AdminNotesForm(StripWhitespaceForm):
|
2021-01-14 18:00:44 +00:00
|
|
|
|
notes = TextAreaField(validators=[])
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-03-15 10:50:18 +00:00
|
|
|
|
class AdminBillingDetailsForm(StripWhitespaceForm):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
billing_contact_email_addresses = GovukTextInputField("Contact email addresses")
|
|
|
|
|
|
billing_contact_names = GovukTextInputField("Contact names")
|
|
|
|
|
|
billing_reference = GovukTextInputField("Reference")
|
|
|
|
|
|
purchase_order_number = GovukTextInputField("Purchase order number")
|
2021-01-27 18:20:25 +00:00
|
|
|
|
notes = TextAreaField(validators=[])
|
2021-01-25 15:40:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
2019-02-18 16:27:53 +00:00
|
|
|
|
class ServiceOnOffSettingForm(StripWhitespaceForm):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
def __init__(self, name, *args, truthy="On", falsey="Off", **kwargs):
|
2019-01-29 14:51:31 +00:00
|
|
|
|
super().__init__(*args, **kwargs)
|
2019-02-18 16:27:53 +00:00
|
|
|
|
self.enabled.label.text = name
|
2019-03-25 14:46:42 +00:00
|
|
|
|
self.enabled.choices = [
|
|
|
|
|
|
(True, truthy),
|
|
|
|
|
|
(False, falsey),
|
|
|
|
|
|
]
|
2019-02-18 16:27:53 +00:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
enabled = OnOffField("Choices")
|
2019-02-18 16:27:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceSwitchChannelForm(ServiceOnOffSettingForm):
|
|
|
|
|
|
def __init__(self, channel, *args, **kwargs):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
name = "Send {}".format(
|
|
|
|
|
|
{
|
|
|
|
|
|
"email": "emails",
|
|
|
|
|
|
"sms": "text messages",
|
|
|
|
|
|
}.get(channel)
|
|
|
|
|
|
)
|
2018-01-19 13:45:54 +00:00
|
|
|
|
|
2019-02-18 16:27:53 +00:00
|
|
|
|
super().__init__(name, *args, **kwargs)
|
2018-01-19 13:45:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
2019-01-31 16:56:35 +00:00
|
|
|
|
class SVGFileUpload(StripWhitespaceForm):
|
|
|
|
|
|
file = FileField_wtf(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Upload an SVG logo",
|
2019-01-31 16:56:35 +00:00
|
|
|
|
validators=[
|
2023-08-25 09:12:23 -07:00
|
|
|
|
FileAllowed(["svg"], "SVG Images only!"),
|
2020-03-04 14:25:14 +00:00
|
|
|
|
DataRequired(message="You need to upload a file to submit"),
|
2022-01-06 11:21:12 +00:00
|
|
|
|
NoEmbeddedImagesInSVG(),
|
|
|
|
|
|
NoTextInSVG(),
|
2023-08-25 09:12:23 -07:00
|
|
|
|
],
|
2019-01-31 16:56:35 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-07 10:48:21 +01:00
|
|
|
|
class EmailFieldInGuestList(GovukEmailField, StripWhitespaceStringField):
|
2017-12-11 16:22:37 +00:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
class InternationalPhoneNumberInGuestList(
|
|
|
|
|
|
InternationalPhoneNumber, StripWhitespaceStringField
|
|
|
|
|
|
):
|
2017-12-11 16:22:37 +00:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-12 09:02:40 +01:00
|
|
|
|
class GuestList(StripWhitespaceForm):
|
2016-09-20 12:30:00 +01:00
|
|
|
|
def populate(self, email_addresses, phone_numbers):
|
2020-06-12 09:02:40 +01:00
|
|
|
|
for form_field, existing_guest_list in (
|
2016-09-20 12:30:00 +01:00
|
|
|
|
(self.email_addresses, email_addresses),
|
2023-08-25 09:12:23 -07:00
|
|
|
|
(self.phone_numbers, phone_numbers),
|
2016-09-20 12:30:00 +01:00
|
|
|
|
):
|
2020-06-12 09:02:40 +01:00
|
|
|
|
for index, value in enumerate(existing_guest_list):
|
2016-09-20 12:30:00 +01:00
|
|
|
|
form_field[index].data = value
|
|
|
|
|
|
|
|
|
|
|
|
email_addresses = FieldList(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
EmailFieldInGuestList("", validators=[Optional(), ValidEmail()], default=""),
|
2016-09-20 12:30:00 +01:00
|
|
|
|
min_entries=5,
|
|
|
|
|
|
max_entries=5,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
label="Email addresses",
|
2016-09-20 12:30:00 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
phone_numbers = FieldList(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
InternationalPhoneNumberInGuestList("", validators=[Optional()], default=""),
|
2016-09-20 12:30:00 +01:00
|
|
|
|
min_entries=5,
|
|
|
|
|
|
max_entries=5,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
label="Mobile numbers",
|
2016-09-20 12:30:00 +01:00
|
|
|
|
)
|
2017-01-03 10:45:06 +00:00
|
|
|
|
|
2017-01-03 16:14:25 +00:00
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class DateFilterForm(StripWhitespaceForm):
|
2020-08-07 10:24:56 +01:00
|
|
|
|
start_date = GovukDateField("Start Date", [validators.optional()])
|
|
|
|
|
|
end_date = GovukDateField("End Date", [validators.optional()])
|
2020-10-29 14:58:39 +00:00
|
|
|
|
include_from_test_key = GovukCheckboxField("Include test keys")
|
2017-02-28 12:16:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
2019-07-22 13:09:03 +01:00
|
|
|
|
class RequiredDateFilterForm(StripWhitespaceForm):
|
2020-08-07 10:24:56 +01:00
|
|
|
|
start_date = GovukDateField("Start Date")
|
|
|
|
|
|
end_date = GovukDateField("End Date")
|
2019-07-22 13:09:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-28 15:43:58 +00:00
|
|
|
|
class BillingReportDateFilterForm(StripWhitespaceForm):
|
|
|
|
|
|
start_date = GovukDateField("First day covered by report")
|
|
|
|
|
|
end_date = GovukDateField("Last day covered by report")
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-02-06 17:32:13 +00:00
|
|
|
|
class SearchByNameForm(StripWhitespaceForm):
|
2020-08-07 10:16:24 +01:00
|
|
|
|
search = GovukSearchField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Search by name",
|
|
|
|
|
|
validators=[
|
|
|
|
|
|
DataRequired("You need to enter full or partial name to search by.")
|
|
|
|
|
|
],
|
2019-08-15 12:41:46 +01:00
|
|
|
|
)
|
2017-05-04 09:30:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
2022-03-15 10:50:18 +00:00
|
|
|
|
class AdminSearchUsersByEmailForm(StripWhitespaceForm):
|
2020-08-07 10:16:24 +01:00
|
|
|
|
search = GovukSearchField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Search by name or email address",
|
2018-07-10 16:33:13 +01:00
|
|
|
|
validators=[
|
2023-08-25 09:12:23 -07:00
|
|
|
|
DataRequired(
|
|
|
|
|
|
"You need to enter full or partial email address to search by."
|
|
|
|
|
|
)
|
2018-07-10 17:24:20 +01:00
|
|
|
|
],
|
2018-07-10 16:33:13 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-01-26 17:21:06 +00:00
|
|
|
|
class SearchUsersForm(StripWhitespaceForm):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
search = GovukSearchField("Search by name or email address")
|
2018-01-26 17:21:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class SearchNotificationsForm(StripWhitespaceForm):
|
2020-08-07 10:16:24 +01:00
|
|
|
|
to = GovukSearchField()
|
2018-08-07 14:44:09 +01:00
|
|
|
|
|
|
|
|
|
|
labels = {
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"email": "Search by email address",
|
|
|
|
|
|
"sms": "Search by phone number",
|
2018-08-07 14:44:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, message_type, *args, **kwargs):
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
self.to.label.text = self.labels.get(
|
|
|
|
|
|
message_type,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Search by phone number or email address",
|
2018-08-07 14:44:09 +01:00
|
|
|
|
)
|
2017-05-30 13:51:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
2021-04-13 13:59:51 +01:00
|
|
|
|
class SearchTemplatesForm(StripWhitespaceForm):
|
|
|
|
|
|
search = GovukSearchField()
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, api_keys, *args, **kwargs):
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
self.search.label.text = (
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Search by name or ID" if api_keys else "Search by name"
|
2021-04-13 13:59:51 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class PlaceholderForm(StripWhitespaceForm):
|
2017-05-04 09:30:55 +01:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-03-15 10:50:18 +00:00
|
|
|
|
class AdminServiceInboundNumberForm(StripWhitespaceForm):
|
2017-10-24 15:37:44 +01:00
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
2023-08-25 09:12:23 -07:00
|
|
|
|
self.inbound_number.choices = kwargs["inbound_number_choices"]
|
2017-10-24 15:37:44 +01:00
|
|
|
|
|
2020-06-30 15:13:42 +01:00
|
|
|
|
inbound_number = GovukRadiosField(
|
2021-01-14 15:16:02 +00:00
|
|
|
|
"Set inbound number",
|
2023-08-25 09:12:23 -07:00
|
|
|
|
thing="an inbound number",
|
2017-10-24 15:37:44 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-26 17:23:44 +01:00
|
|
|
|
class CallbackForm(StripWhitespaceForm):
|
2020-08-03 15:10:02 +01:00
|
|
|
|
url = GovukTextInputField(
|
|
|
|
|
|
"URL",
|
2023-08-25 09:12:23 -07:00
|
|
|
|
validators=[
|
|
|
|
|
|
DataRequired(message="Cannot be empty"),
|
|
|
|
|
|
Regexp(regex="^https.*", message="Must be a valid https URL"),
|
|
|
|
|
|
],
|
2020-08-03 15:10:02 +01:00
|
|
|
|
)
|
|
|
|
|
|
bearer_token = GovukPasswordField(
|
|
|
|
|
|
"Bearer token",
|
2023-08-25 09:12:23 -07:00
|
|
|
|
validators=[
|
|
|
|
|
|
DataRequired(message="Cannot be empty"),
|
|
|
|
|
|
Length(min=10, message="Must be at least 10 characters"),
|
|
|
|
|
|
],
|
2020-08-03 15:10:02 +01:00
|
|
|
|
)
|
2018-04-26 17:23:44 +01:00
|
|
|
|
|
2023-04-26 11:24:01 -04:00
|
|
|
|
def validate(self, extra_validators=None):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return super().validate(extra_validators) or self.url.data == ""
|
2018-04-26 17:23:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class SMSPrefixForm(StripWhitespaceForm):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
enabled = OnOffField("") # label is assigned on instantiation
|
2017-11-03 15:01:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-05-04 09:30:55 +01:00
|
|
|
|
def get_placeholder_form_instance(
|
|
|
|
|
|
placeholder_name,
|
|
|
|
|
|
dict_to_populate_from,
|
2018-03-16 14:17:43 +00:00
|
|
|
|
template_type,
|
2017-05-25 09:40:37 +01:00
|
|
|
|
allow_international_phone_numbers=False,
|
2017-05-04 09:30:55 +01:00
|
|
|
|
):
|
2018-03-16 14:17:43 +00:00
|
|
|
|
if (
|
2023-08-25 09:12:23 -07:00
|
|
|
|
InsensitiveDict.make_key(placeholder_name) == "emailaddress"
|
|
|
|
|
|
and template_type == "email"
|
2018-03-16 14:17:43 +00:00
|
|
|
|
):
|
2017-05-25 09:40:37 +01:00
|
|
|
|
field = email_address(label=placeholder_name, gov_user=False)
|
2018-03-16 14:17:43 +00:00
|
|
|
|
elif (
|
2023-08-25 09:12:23 -07:00
|
|
|
|
InsensitiveDict.make_key(placeholder_name) == "phonenumber"
|
|
|
|
|
|
and template_type == "sms"
|
2018-03-16 14:17:43 +00:00
|
|
|
|
):
|
2017-05-25 09:40:37 +01:00
|
|
|
|
if allow_international_phone_numbers:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
field = international_phone_number(
|
|
|
|
|
|
label=placeholder_name
|
|
|
|
|
|
) # TODO: modify as necessary for non-us numbers
|
2017-05-25 09:40:37 +01:00
|
|
|
|
else:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
field = uk_mobile_number(
|
|
|
|
|
|
label=placeholder_name
|
|
|
|
|
|
) # TODO: replace with us_mobile_number
|
2017-05-25 09:40:37 +01:00
|
|
|
|
else:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
field = GovukTextInputField(
|
|
|
|
|
|
placeholder_name, validators=[DataRequired(message="Cannot be empty")]
|
|
|
|
|
|
)
|
2017-05-25 09:40:37 +01:00
|
|
|
|
|
|
|
|
|
|
PlaceholderForm.placeholder_value = field
|
2017-05-04 09:30:55 +01:00
|
|
|
|
|
|
|
|
|
|
return PlaceholderForm(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
placeholder_value=dict_to_populate_from.get(placeholder_name, "")
|
2017-05-04 09:30:55 +01:00
|
|
|
|
)
|
2017-10-16 16:35:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:02:12 +00:00
|
|
|
|
class SetSenderForm(StripWhitespaceForm):
|
2017-10-16 16:35:35 +01:00
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
2023-08-25 09:12:23 -07:00
|
|
|
|
self.sender.choices = kwargs["sender_choices"]
|
|
|
|
|
|
self.sender.label.text = kwargs["sender_label"]
|
2017-10-16 16:35:35 +01:00
|
|
|
|
|
2021-01-13 10:31:07 +00:00
|
|
|
|
sender = GovukRadiosField()
|
2018-01-03 10:44:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SetTemplateSenderForm(StripWhitespaceForm):
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
2023-08-25 09:12:23 -07:00
|
|
|
|
self.sender.choices = kwargs["sender_choices"]
|
|
|
|
|
|
self.sender.label.text = "Select your sender"
|
2018-01-03 10:44:36 +00:00
|
|
|
|
|
2021-01-13 14:58:39 +00:00
|
|
|
|
sender = GovukRadiosField()
|
2018-02-12 09:26:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
|
class AdminSetOrganizationForm(StripWhitespaceForm):
|
2018-02-12 09:26:13 +00:00
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
2023-08-25 09:12:23 -07:00
|
|
|
|
self.organizations.choices = kwargs["choices"]
|
2018-02-12 09:26:13 +00:00
|
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
|
organizations = GovukRadiosField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Select an organization", validators=[DataRequired()]
|
2018-02-12 09:26:13 +00:00
|
|
|
|
)
|
2018-05-18 14:05:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
2022-03-15 10:50:18 +00:00
|
|
|
|
class AdminServiceAddDataRetentionForm(StripWhitespaceForm):
|
2020-06-26 21:17:38 +01:00
|
|
|
|
notification_type = GovukRadiosField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"What notification type?",
|
2018-07-17 14:39:04 +01:00
|
|
|
|
choices=[
|
2023-08-25 09:12:23 -07:00
|
|
|
|
("email", "Email"),
|
|
|
|
|
|
("sms", "SMS"),
|
2018-07-17 14:39:04 +01:00
|
|
|
|
],
|
2023-08-25 09:12:23 -07:00
|
|
|
|
thing="notification type",
|
2018-07-17 14:39:04 +01:00
|
|
|
|
)
|
2020-08-07 10:31:09 +01:00
|
|
|
|
days_of_retention = GovukIntegerField(
|
|
|
|
|
|
label="Days of retention",
|
2023-08-25 09:12:23 -07:00
|
|
|
|
validators=[
|
|
|
|
|
|
validators.NumberRange(min=3, max=90, message="Must be between 3 and 90")
|
|
|
|
|
|
],
|
2020-08-07 10:31:09 +01:00
|
|
|
|
)
|
2018-07-17 14:39:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
2022-03-15 10:50:18 +00:00
|
|
|
|
class AdminServiceEditDataRetentionForm(StripWhitespaceForm):
|
2020-08-07 10:31:09 +01:00
|
|
|
|
days_of_retention = GovukIntegerField(
|
|
|
|
|
|
label="Days of retention",
|
2023-08-25 09:12:23 -07:00
|
|
|
|
validators=[
|
|
|
|
|
|
validators.NumberRange(min=3, max=90, message="Must be between 3 and 90")
|
|
|
|
|
|
],
|
2020-08-07 10:31:09 +01:00
|
|
|
|
)
|
2018-09-06 16:34:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
2018-11-01 16:02:43 +00:00
|
|
|
|
class TemplateFolderForm(StripWhitespaceForm):
|
2019-03-18 16:12:12 +00:00
|
|
|
|
def __init__(self, all_service_users=None, *args, **kwargs):
|
2019-03-15 14:13:27 +00:00
|
|
|
|
super().__init__(*args, **kwargs)
|
2019-03-18 16:12:12 +00:00
|
|
|
|
if all_service_users is not None:
|
|
|
|
|
|
self.users_with_permission.all_service_users = all_service_users
|
|
|
|
|
|
self.users_with_permission.choices = [
|
|
|
|
|
|
(item.id, item.name) for item in all_service_users
|
2019-03-15 14:13:27 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
2020-10-29 14:58:39 +00:00
|
|
|
|
users_with_permission = GovukCollapsibleCheckboxesField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Team members who can see this folder", field_label="team member"
|
|
|
|
|
|
)
|
|
|
|
|
|
name = GovukTextInputField(
|
|
|
|
|
|
"Folder name", validators=[DataRequired(message="Cannot be empty")]
|
|
|
|
|
|
)
|
2018-11-08 11:56:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-11-20 18:03:57 +00:00
|
|
|
|
def required_for_ops(*operations):
|
|
|
|
|
|
operations = set(operations)
|
|
|
|
|
|
|
|
|
|
|
|
def validate(form, field):
|
2018-12-07 16:38:48 +00:00
|
|
|
|
if form.op not in operations and any(field.raw_data):
|
2018-11-20 18:03:57 +00:00
|
|
|
|
# super weird
|
2023-08-25 09:12:23 -07:00
|
|
|
|
raise validators.StopValidation("Must be empty")
|
2018-12-07 16:38:48 +00:00
|
|
|
|
if form.op in operations and not any(field.raw_data):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
raise validators.StopValidation("Cannot be empty")
|
|
|
|
|
|
|
2018-11-20 18:03:57 +00:00
|
|
|
|
return validate
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-08 11:56:29 +00:00
|
|
|
|
class TemplateAndFoldersSelectionForm(Form):
|
2018-11-27 14:33:50 +00:00
|
|
|
|
"""
|
2018-12-07 16:38:48 +00:00
|
|
|
|
This form expects the form data to include an operation, based on which submit button is clicked.
|
2018-11-27 14:33:50 +00:00
|
|
|
|
If enter is pressed, unknown will be sent by a hidden submit button at the top of the form.
|
|
|
|
|
|
The value of this operation affects which fields are required, expected to be empty, or optional.
|
|
|
|
|
|
|
|
|
|
|
|
* unknown
|
|
|
|
|
|
currently not implemented, but in the future will try and work out if there are any obvious commands that can be
|
|
|
|
|
|
assumed based on which fields are empty vs populated.
|
2018-12-04 14:47:05 +00:00
|
|
|
|
* move-to-existing-folder
|
2018-11-27 14:33:50 +00:00
|
|
|
|
must have data for templates_and_folders checkboxes, and move_to radios
|
2018-12-04 14:47:05 +00:00
|
|
|
|
* move-to-new-folder
|
2018-11-27 14:33:50 +00:00
|
|
|
|
must have data for move_to_new_folder_name, cannot have data for move_to_existing_folder_name
|
2018-12-04 14:47:05 +00:00
|
|
|
|
* add-new-folder
|
2018-11-27 14:33:50 +00:00
|
|
|
|
must have data for move_to_existing_folder_name, cannot have data for move_to_new_folder_name
|
|
|
|
|
|
"""
|
2018-11-08 11:56:29 +00:00
|
|
|
|
|
|
|
|
|
|
ALL_TEMPLATES_FOLDER = {
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"name": "Templates",
|
|
|
|
|
|
"id": RadioFieldWithNoneOption.NONE_OPTION_VALUE,
|
2018-11-08 11:56:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
|
self,
|
2018-11-23 16:29:21 +00:00
|
|
|
|
all_template_folders,
|
|
|
|
|
|
template_list,
|
2020-07-01 16:43:08 +01:00
|
|
|
|
available_template_types,
|
2018-11-28 13:48:13 +00:00
|
|
|
|
allow_adding_copy_of_template,
|
2018-11-08 11:56:29 +00:00
|
|
|
|
*args,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
**kwargs,
|
2018-11-08 11:56:29 +00:00
|
|
|
|
):
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
2020-07-01 16:43:08 +01:00
|
|
|
|
self.available_template_types = available_template_types
|
|
|
|
|
|
|
2018-11-23 16:29:21 +00:00
|
|
|
|
self.templates_and_folders.choices = template_list.as_id_and_name
|
2018-11-08 11:56:29 +00:00
|
|
|
|
|
2018-11-20 18:03:57 +00:00
|
|
|
|
self.op = None
|
2018-11-28 13:48:13 +00:00
|
|
|
|
self.is_move_op = self.is_add_folder_op = self.is_add_template_op = False
|
2018-11-15 17:31:07 +00:00
|
|
|
|
|
2018-12-18 14:40:53 +00:00
|
|
|
|
self.move_to.all_template_folders = all_template_folders
|
2018-11-23 16:29:21 +00:00
|
|
|
|
self.move_to.choices = [
|
2023-08-25 09:12:23 -07:00
|
|
|
|
(item["id"], item["name"])
|
2018-11-23 16:29:21 +00:00
|
|
|
|
for item in ([self.ALL_TEMPLATES_FOLDER] + all_template_folders)
|
2018-11-08 11:56:29 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
self.add_template_by_template_type.choices = list(
|
|
|
|
|
|
filter(
|
|
|
|
|
|
None,
|
|
|
|
|
|
[
|
|
|
|
|
|
# ('email', 'Email') if 'email' in available_template_types else None,
|
2023-10-03 11:14:21 -04:00
|
|
|
|
("sms", "Start with a blank template")
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if "sms" in available_template_types
|
|
|
|
|
|
else None,
|
|
|
|
|
|
("copy-existing", "Copy an existing template")
|
|
|
|
|
|
if allow_adding_copy_of_template
|
|
|
|
|
|
else None,
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
2018-11-28 13:48:13 +00:00
|
|
|
|
|
2020-07-01 16:43:08 +01:00
|
|
|
|
@property
|
|
|
|
|
|
def trying_to_add_unavailable_template_type(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return all(
|
|
|
|
|
|
(
|
|
|
|
|
|
self.is_add_template_op,
|
|
|
|
|
|
self.add_template_by_template_type.data,
|
|
|
|
|
|
self.add_template_by_template_type.data
|
|
|
|
|
|
not in self.available_template_types,
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
2020-07-01 16:43:08 +01:00
|
|
|
|
|
2018-11-30 16:31:42 +00:00
|
|
|
|
def is_selected(self, template_folder_id):
|
|
|
|
|
|
return template_folder_id in (self.templates_and_folders.data or [])
|
|
|
|
|
|
|
2023-04-26 11:24:01 -04:00
|
|
|
|
def validate(self, extra_validators=None):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
self.op = request.form.get("operation")
|
2018-11-15 17:31:07 +00:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
self.is_move_op = self.op in {"move-to-existing-folder", "move-to-new-folder"}
|
|
|
|
|
|
self.is_add_folder_op = self.op in {"add-new-folder", "move-to-new-folder"}
|
|
|
|
|
|
self.is_add_template_op = self.op in {"add-new-template"}
|
2018-11-15 17:31:07 +00:00
|
|
|
|
|
2018-11-28 13:48:13 +00:00
|
|
|
|
if not (self.is_add_folder_op or self.is_move_op or self.is_add_template_op):
|
2018-11-15 17:31:07 +00:00
|
|
|
|
return False
|
|
|
|
|
|
|
2023-04-26 11:24:01 -04:00
|
|
|
|
return super().validate(extra_validators)
|
2018-11-15 17:31:07 +00:00
|
|
|
|
|
2018-11-20 18:03:57 +00:00
|
|
|
|
def get_folder_name(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if self.op == "add-new-folder":
|
2018-11-20 18:03:57 +00:00
|
|
|
|
return self.add_new_folder_name.data
|
2023-08-25 09:12:23 -07:00
|
|
|
|
elif self.op == "move-to-new-folder":
|
2018-11-20 18:03:57 +00:00
|
|
|
|
return self.move_to_new_folder_name.data
|
|
|
|
|
|
return None
|
2018-11-15 17:31:07 +00:00
|
|
|
|
|
2020-10-29 14:58:39 +00:00
|
|
|
|
templates_and_folders = GovukCheckboxesField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Choose templates or folders",
|
|
|
|
|
|
validators=[required_for_ops("move-to-new-folder", "move-to-existing-folder")],
|
2020-04-09 11:51:11 +01:00
|
|
|
|
choices=[], # added to keep order of arguments, added properly in __init__
|
2023-08-25 09:12:23 -07:00
|
|
|
|
param_extensions={"fieldset": {"legend": {"classes": "usa-sr-only"}}},
|
2020-04-09 11:51:11 +01:00
|
|
|
|
)
|
2019-01-03 14:58:20 +00:00
|
|
|
|
# if no default set, it is set to None, which process_data transforms to '__NONE__'
|
|
|
|
|
|
# this means '__NONE__' (self.ALL_TEMPLATES option) is selected when no form data has been submitted
|
|
|
|
|
|
# set default to empty string so process_data method doesn't perform any transformation
|
2018-12-18 14:40:53 +00:00
|
|
|
|
move_to = NestedRadioField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Choose a folder",
|
|
|
|
|
|
default="",
|
|
|
|
|
|
validators=[required_for_ops("move-to-existing-folder"), Optional()],
|
|
|
|
|
|
)
|
|
|
|
|
|
add_new_folder_name = GovukTextInputField(
|
|
|
|
|
|
"Folder name", validators=[required_for_ops("add-new-folder")]
|
|
|
|
|
|
)
|
|
|
|
|
|
move_to_new_folder_name = GovukTextInputField(
|
|
|
|
|
|
"Folder name", validators=[required_for_ops("move-to-new-folder")]
|
|
|
|
|
|
)
|
2018-11-28 13:48:13 +00:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
add_template_by_template_type = RadioFieldWithRequiredMessage(
|
|
|
|
|
|
"New template",
|
|
|
|
|
|
validators=[
|
|
|
|
|
|
required_for_ops("add-new-template"),
|
|
|
|
|
|
Optional(),
|
|
|
|
|
|
],
|
|
|
|
|
|
required_message="Select the type of template you want to add",
|
|
|
|
|
|
)
|
2019-02-15 10:27:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
2022-03-15 10:50:18 +00:00
|
|
|
|
class AdminClearCacheForm(StripWhitespaceForm):
|
2022-02-21 11:28:15 +00:00
|
|
|
|
model_type = GovukCheckboxesField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"What do you want to clear today",
|
2019-02-15 10:27:38 +00:00
|
|
|
|
)
|
2019-05-13 14:50:40 +01:00
|
|
|
|
|
2022-02-21 11:28:15 +00:00
|
|
|
|
def validate_model_type(self, field):
|
|
|
|
|
|
if not field.data:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
raise ValidationError("Select at least one option")
|
2022-02-21 11:28:15 +00:00
|
|
|
|
|
2019-05-13 14:50:40 +01:00
|
|
|
|
|
2021-05-21 17:10:34 +01:00
|
|
|
|
class ChangeSecurityKeyNameForm(StripWhitespaceForm):
|
|
|
|
|
|
security_key_name = GovukTextInputField(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Name of key",
|
2021-05-14 17:52:13 +01:00
|
|
|
|
validators=[
|
2023-08-25 09:12:23 -07:00
|
|
|
|
DataRequired(message="Cannot be empty"),
|
2021-05-14 17:52:13 +01:00
|
|
|
|
MustContainAlphanumericCharacters(),
|
2023-08-25 09:12:23 -07:00
|
|
|
|
Length(max=255, message="Name of key must be 255 characters or fewer"),
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|