Files
notifications-admin/app/main/forms.py

2184 lines
67 KiB
Python
Raw Normal View History

import weakref
from datetime import datetime, timedelta
from itertools import chain
import pytz
from flask import Markup, render_template, request
from flask_login import current_user
from flask_wtf import FlaskForm as Form
from flask_wtf.file import FileAllowed
from flask_wtf.file import FileField as FileField_wtf
from notifications_utils.columns import Columns
from notifications_utils.countries.data import Postage
from notifications_utils.formatters import strip_whitespace
from notifications_utils.postal_address import PostalAddress
from notifications_utils.recipients import (
InvalidPhoneError,
normalise_phone_number,
validate_phone_number,
)
from werkzeug.utils import cached_property
2016-01-12 10:43:23 +00:00
from wtforms import (
BooleanField,
DateField,
FieldList,
FileField,
HiddenField,
IntegerField,
PasswordField,
)
from wtforms import RadioField as WTFormsRadioField
from wtforms import (
SelectMultipleField,
StringField,
TextAreaField,
ValidationError,
validators,
widgets,
Add flake8 linting to project The GDS Way™[1] recommends using Flake8 to lint Python projects. This commit takes the Flake8 config from Digital Marketplace API[2] and removes the bits we don’t need. It changes the `max_complexity` setting to 14, which is the most complex code we have in this repo currently (we shouldn’t be writing code _more_ complex than what we already have). This commit also fixes the errors found by Flake8, which includes 6(!) tests which were never getting run because they had the same names as existing tests. Here is a full list of the errors that were found and fixed: ``` ./app/__init__.py:2:1: F401 're' imported but unused ./app/__init__.py:4:1: F401 'json' imported but unused ./app/__init__.py:8:1: F401 'dateutil' imported but unused ./app/__init__.py:11:1: F401 'flask.escape' imported but unused ./app/__init__.py:41:1: F401 'app.proxy_fix' imported but unused ./app/__init__.py:129:5: F821 undefined name 'proxy_fix' ./app/__init__.py:221:19: F821 undefined name 'highlight' ./app/__init__.py:221:35: F821 undefined name 'JavascriptLexer' ./app/__init__.py:221:54: F821 undefined name 'HtmlFormatter' ./app/config.py:2:1: F401 'datetime.timedelta' imported but unused ./app/event_handlers.py:2:1: F401 'flask_login.current_user' imported but unused ./app/utils.py:11:1: F401 'dateutil.parser' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.two_factor' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.notifications' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.add_service' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.forgot_password' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.inbound_number' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.styleguide' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.organisations' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.letter_jobs' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.verify' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.conversation' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.api_keys' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.send' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.dashboard' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.jobs' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.manage_users' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.sign_in' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.sign_out' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.code_not_received' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.invites' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.platform_admin' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.providers' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.service_settings' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.index' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.new_password' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.user_profile' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.feedback' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.choose_service' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.templates' imported but unused ./app/main/__init__.py:5:1: F401 'app.main.views.register' imported but unused ./app/main/forms.py:12:1: F401 'wtforms.SelectField' imported but unused ./app/main/views/api_keys.py:37:29: E241 multiple spaces after ':' ./app/main/views/feedback.py:3:1: F401 'flask.flash' imported but unused ./app/main/views/feedback.py:122:17: E123 closing bracket does not match indentation of opening bracket's line ./app/main/views/inbound_number.py:1:1: F401 'flask.url_for' imported but unused ./app/main/views/inbound_number.py:1:1: F401 'flask.session' imported but unused ./app/main/views/inbound_number.py:1:1: F401 'flask.redirect' imported but unused ./app/main/views/inbound_number.py:1:1: F401 'flask.request' imported but unused ./app/main/views/inbound_number.py:13:1: F401 'flask.jsonify' imported but unused ./app/main/views/jobs.py:31:1: F401 'app.utils.get_template' imported but unused ./app/main/views/letter_jobs.py:1:1: F401 'datetime' imported but unused ./app/main/views/letter_jobs.py:6:1: F401 'app.format_datetime_24h' imported but unused ./app/main/views/manage_users.py:111:9: E123 closing bracket does not match indentation of opening bracket's line ./app/main/views/notifications.py:121:5: F841 local variable 'status_args' is assigned to but never used ./app/main/views/organisations.py:1:1: F401 'flask.request' imported but unused ./app/main/views/service_settings.py:77:9: E123 closing bracket does not match indentation of opening bracket's line ./app/main/views/service_settings.py:82:9: E123 closing bracket does not match indentation of opening bracket's line ./app/main/views/service_settings.py:420:13: E123 closing bracket does not match indentation of opening bracket's line ./app/main/views/sign_in.py:12:1: F401 'flask_login.confirm_login' imported but unused ./app/main/views/sign_in.py:17:1: F401 'app.service_api_client' imported but unused ./app/main/views/sign_in.py:62:13: E123 closing bracket does not match indentation of opening bracket's line ./app/main/views/templates.py:4:1: F401 'flask.json' imported but unused ./app/main/views/templates.py:17:1: F401 'notifications_utils.formatters.escape_html' imported but unused ./app/main/views/templates.py:23:1: F401 'app.utils.get_help_argument' imported but unused ./app/main/views/templates.py:64:13: E123 closing bracket does not match indentation of opening bracket's line ./app/notify_client/service_api_client.py:6:1: F401 '.notification_api_client' imported but unused ./app/notify_client/user_api_client.py:1:1: F401 'uuid' imported but unused ./app/notify_client/user_api_client.py:3:1: F401 'flask.session' imported but unused ./tests/__init__.py:1:1: F401 'csv' imported but unused ./tests/app/main/test_asset_fingerprinter.py:2:1: F401 'os' imported but unused ./tests/app/main/test_asset_fingerprinter.py:4:1: F401 'unittest.mock' imported but unused ./tests/app/main/test_asset_fingerprinter.py:98:9: F841 local variable 'string_with_unicode_character' is assigned to but never used ./tests/app/main/test_errorhandlers.py:2:1: F401 'flask.url_for' imported but unused ./tests/app/main/test_permissions.py:26:13: F841 local variable 'response' is assigned to but never used ./tests/app/main/test_placeholder_form.py:3:1: F401 'wtforms.Label' imported but unused ./tests/app/main/test_placeholder_form.py:11:10: F841 local variable 'req' is assigned to but never used ./tests/app/main/test_two_factor_form.py:10:67: F841 local variable 'req' is assigned to but never used ./tests/app/main/test_two_factor_form.py:23:65: F841 local variable 'req' is assigned to but never used ./tests/app/main/test_two_factor_form.py:37:48: F841 local variable 'req' is assigned to but never used ./tests/app/main/test_two_factor_form.py:51:67: F841 local variable 'req' is assigned to but never used ./tests/app/main/test_two_factor_form.py:65:67: F841 local variable 'req' is assigned to but never used ./tests/app/main/views/test_accept_invite.py:356:5: F841 local variable 'element' is assigned to but never used ./tests/app/main/views/test_activity.py:11:1: F811 redefinition of unused 'mock_get_notifications' from line 11 ./tests/app/main/views/test_activity.py:18:1: F401 'datetime.datetime' imported but unused ./tests/app/main/views/test_activity.py:102:5: F841 local variable 'content' is assigned to but never used ./tests/app/main/views/test_activity.py:104:5: F841 local variable 'notification' is assigned to but never used ./tests/app/main/views/test_activity.py:337:5: F841 local variable '_notifications_mock' is assigned to but never used ./tests/app/main/views/test_activity.py:373:13: E126 continuation line over-indented for hanging indent ./tests/app/main/views/test_activity.py:378:9: E121 continuation line under-indented for hanging indent ./tests/app/main/views/test_activity.py:404:13: E126 continuation line over-indented for hanging indent ./tests/app/main/views/test_activity.py:407:9: E121 continuation line under-indented for hanging indent ./tests/app/main/views/test_api_keys.py:354:5: F841 local variable 'response' is assigned to but never used ./tests/app/main/views/test_conversation.py:5:1: F401 'bs4.BeautifulSoup' imported but unused ./tests/app/main/views/test_conversation.py:198:5: F841 local variable 'mock_get_inbound_sms' is assigned to but never used ./tests/app/main/views/test_dashboard.py:53:5: F841 local variable 'mock_template_stats' is assigned to but never used ./tests/app/main/views/test_dashboard.py:72:5: F841 local variable 'mock_template_stats' is assigned to but never used ./tests/app/main/views/test_jobs.py:2:1: F401 'uuid' imported but unused ./tests/app/main/views/test_jobs.py:3:1: F401 'urllib.parse.urlparse' imported but unused ./tests/app/main/views/test_jobs.py:3:1: F401 'urllib.parse.quote' imported but unused ./tests/app/main/views/test_jobs.py:3:1: F401 'urllib.parse.parse_qs' imported but unused ./tests/app/main/views/test_jobs.py:9:1: F401 'app.main.views.jobs.get_status_filters' imported but unused ./tests/app/main/views/test_jobs.py:10:1: F401 'tests.notification_json' imported but unused ./tests/app/main/views/test_letters.py:6:1: F401 'tests.service_json' imported but unused ./tests/app/main/views/test_notifications.py:5:1: F401 'app.utils.REQUESTED_STATUSES' imported but unused ./tests/app/main/views/test_notifications.py:5:1: F401 'app.utils.DELIVERED_STATUSES' imported but unused ./tests/app/main/views/test_notifications.py:5:1: F401 'app.utils.SENDING_STATUSES' imported but unused ./tests/app/main/views/test_notifications.py:5:1: F401 'app.utils.FAILURE_STATUSES' imported but unused ./tests/app/main/views/test_platform_admin.py:242:13: E126 continuation line over-indented for hanging indent ./tests/app/main/views/test_platform_admin.py:247:13: E126 continuation line over-indented for hanging indent ./tests/app/main/views/test_send.py:3:1: F401 'unittest.mock.Mock' imported but unused ./tests/app/main/views/test_send.py:18:1: F811 redefinition of unused 'mock_get_service' from line 18 ./tests/app/main/views/test_send.py:18:1: F401 'tests.conftest.multiple_letter_contact_blocks' imported but unused ./tests/app/main/views/test_send.py:18:1: F401 'tests.conftest.no_sms_senders' imported but unused ./tests/app/main/views/test_send.py:18:1: F401 'tests.conftest.multiple_sms_senders' imported but unused ./tests/app/main/views/test_send.py:18:1: F401 'tests.conftest.no_letter_contact_blocks' imported but unused ./tests/app/main/views/test_send.py:102:5: F841 local variable 'response' is assigned to but never used ./tests/app/main/views/test_send.py:870:5: F841 local variable 'response' is assigned to but never used ./tests/app/main/views/test_send.py:1367:5: F841 local variable 'service_id' is assigned to but never used ./tests/app/main/views/test_send.py:1451:13: E126 continuation line over-indented for hanging indent ./tests/app/main/views/test_send.py:1620:80: E226 missing whitespace around arithmetic operator ./tests/app/main/views/test_send.py:1909:13: E126 continuation line over-indented for hanging indent ./tests/app/main/views/test_send.py:1912:9: E121 continuation line under-indented for hanging indent ./tests/app/main/views/test_service_settings.py:13:1: F811 redefinition of unused 'no_reply_to_email_addresses' from line 13 ./tests/app/main/views/test_service_settings.py:13:1: F401 'tests.conftest.single_reply_to_email_address' imported but unused ./tests/app/main/views/test_service_settings.py:28:5: E123 closing bracket does not match indentation of opening bracket's line ./tests/app/main/views/test_service_settings.py:104:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:166:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:186:5: F841 local variable 'mocked_get_fn' is assigned to but never used ./tests/app/main/views/test_service_settings.py:217:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:237:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:257:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:307:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:340:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:466:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:555:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:615:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:719:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:874:5: F841 local variable 'page' is assigned to but never used ./tests/app/main/views/test_service_settings.py:902:5: F841 local variable 'page' is assigned to but never used ./tests/app/main/views/test_service_settings.py:954:5: F841 local variable 'page' is assigned to but never used ./tests/app/main/views/test_service_settings.py:986:5: F841 local variable 'page' is assigned to but never used ./tests/app/main/views/test_service_settings.py:1101:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:1121:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:1271:1: F811 redefinition of unused 'test_set_letter_contact_block_saves' from line 1189 ./tests/app/main/views/test_service_settings.py:1433:5: F841 local variable 'page' is assigned to but never used ./tests/app/main/views/test_service_settings.py:1495:5: F841 local variable 'mocked_get_fn' is assigned to but never used ./tests/app/main/views/test_service_settings.py:1540:5: F841 local variable 'mocked_get_fn' is assigned to but never used ./tests/app/main/views/test_service_settings.py:1570:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:1589:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:1621:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:1641:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:1658:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:1676:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:1697:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:1759:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_service_settings.py:1775:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13 ./tests/app/main/views/test_templates.py:3:1: F401 'uuid' imported but unused ./tests/app/main/views/test_templates.py:11:1: F401 'tests.conftest.mock_get_user' imported but unused ./tests/app/main/views/test_templates.py:514:1: F811 redefinition of unused 'mock_get_user' from line 11 ./tests/app/main/views/test_templates.py:672:1: F811 redefinition of unused 'mock_get_user' from line 11 ./tests/app/main/views/test_templates.py:795:1: F811 redefinition of unused 'mock_get_user' from line 11 ./tests/app/main/views/test_templates.py:835:1: F811 redefinition of unused 'mock_get_user' from line 11 ./tests/app/main/views/test_two_factor.py:67:13: E126 continuation line over-indented for hanging indent ./tests/app/notify_client/test_notification_client.py:79:5: F841 local variable 'mock_post' is assigned to but never used ``` 1. https://gds-way.cloudapps.digital/manuals/programming-languages/python/linting.html#how-to-use-flake8 2. https://github.com/alphagov/digitalmarketplace-api/blob/d5ab8afef4a0472f9d266d94ab4ffe1333a9aaad/.flake8
2017-10-18 14:51:26 +01:00
)
from wtforms.fields.html5 import EmailField, SearchField, TelField
from wtforms.validators import URL, DataRequired, Length, Optional, Regexp
from app import format_thousands
from app.main.validators import (
CommonlyUsedPassword,
CsvFileValidator,
DoesNotStartWithDoubleZero,
2020-04-02 15:57:46 +01:00
LettersNumbersFullStopsAndUnderscoresOnly,
2019-11-08 17:12:32 +00:00
MustContainAlphanumericCharacters,
NoCommasInPlaceHolders,
NoEmbeddedImagesInSVG,
OnlySMSCharacters,
ValidEmail,
ValidGovEmail,
)
from app.models.feedback import PROBLEM_TICKET_TYPE, QUESTION_TICKET_TYPE
from app.models.organisation import Organisation
from app.models.roles_and_permissions import (
broadcast_permissions,
permissions,
roles,
)
from app.utils import guess_name_from_email_address
def get_time_value_and_label(future_time):
return (
future_time.replace(tzinfo=None).isoformat(),
'{} at {}'.format(
get_human_day(future_time.astimezone(pytz.timezone('Europe/London'))),
get_human_time(future_time.astimezone(pytz.timezone('Europe/London')))
)
)
def get_human_time(time):
return {
'0': 'midnight',
'12': 'midday'
}.get(
time.strftime('%-H'),
time.strftime('%-I%p').lower()
)
def get_human_day(time, prefix_today_with='T'):
# Add 1 hour to get midnight today instead of midnight tomorrow
time = (time - timedelta(hours=1)).strftime('%A')
if time == datetime.utcnow().strftime('%A'):
return '{}oday'.format(prefix_today_with)
if time == (datetime.utcnow() + timedelta(days=1)).strftime('%A'):
return 'Tomorrow'
return time
def get_furthest_possible_scheduled_time():
return (datetime.utcnow() + timedelta(days=4)).replace(hour=0)
def get_next_hours_until(until):
now = datetime.utcnow()
hours = int((until - now).total_seconds() / (60 * 60))
return [
(now + timedelta(hours=i)).replace(minute=0, second=0, microsecond=0).replace(tzinfo=pytz.utc)
for i in range(1, hours + 1)
]
def get_next_days_until(until):
now = datetime.utcnow()
days = int((until - now).total_seconds() / (60 * 60 * 24))
return [
get_human_day(
(now + timedelta(days=i)).replace(tzinfo=pytz.utc),
prefix_today_with='Later t'
)
for i in range(0, days + 1)
]
class RadioField(WTFormsRadioField):
def __init__(
self,
*args,
thing='an option',
**kwargs
):
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():
raise ValidationError(f'Select {self.thing}')
def email_address(label='Email address', gov_user=True, required=True):
validators = [
ValidEmail(),
]
if gov_user:
validators.append(ValidGovEmail())
if required:
2019-09-12 16:42:33 +01:00
validators.append(DataRequired(message='Cannot be empty'))
return EmailField(label, validators, render_kw={'spellcheck': 'false'})
class UKMobileNumber(TelField):
def pre_validate(self, form):
try:
validate_phone_number(self.data)
except InvalidPhoneError as e:
raise ValidationError(str(e))
class InternationalPhoneNumber(TelField):
def pre_validate(self, form):
try:
2017-11-10 12:35:21 +00:00
if self.data:
validate_phone_number(self.data, international=True)
except InvalidPhoneError as e:
raise ValidationError(str(e))
def uk_mobile_number(label='Mobile number'):
return UKMobileNumber(label,
2019-09-12 16:42:33 +01:00
validators=[DataRequired(message='Cannot be empty')])
def international_phone_number(label='Mobile number'):
return InternationalPhoneNumber(
label,
2019-09-12 16:42:33 +01:00
validators=[DataRequired(message='Cannot be empty')]
)
def password(label='Password'):
return PasswordField(label,
2019-09-12 16:42:33 +01:00
validators=[DataRequired(message='Cannot be empty'),
Length(8, 255, message='Must be at least 8 characters'),
CommonlyUsedPassword(message='Choose a password thats harder to guess')])
class SMSCode(StringField):
validators = [
2019-09-12 16:42:33 +01:00
DataRequired(message='Cannot be empty'),
2018-11-13 10:49:35 +00:00
Regexp(regex=r'^\d+$', message='Numbers only'),
Length(min=5, message='Not enough numbers'),
Length(max=5, message='Too many numbers'),
]
def __call__(self, **kwargs):
return super().__call__(type='tel', pattern='[0-9]*', **kwargs)
def process_formdata(self, valuelist):
if valuelist:
self.data = Columns.make_key(valuelist[0])
class ForgivingIntegerField(StringField):
# Actual value is 2147483647 but this is a scary looking arbitrary number
POSTGRES_MAX_INT = 2000000000
def __init__(
self,
label=None,
things='items',
format_error_suffix='',
**kwargs
):
self.things = things
self.format_error_suffix = format_error_suffix
super().__init__(label, **kwargs)
def process_formdata(self, valuelist):
if valuelist:
value = valuelist[0].replace(',', '').replace(' ', '')
try:
value = int(value)
except ValueError:
pass
if value == '':
value = 0
return super().process_formdata([value])
def pre_validate(self, form):
if self.data:
error = None
try:
if int(self.data) > self.POSTGRES_MAX_INT:
error = 'Number of {} must be {} or less'.format(
self.things,
format_thousands(self.POSTGRES_MAX_INT),
)
except ValueError:
error = 'Enter the number of {} {}'.format(
self.things,
self.format_error_suffix,
)
if error:
raise ValidationError(error)
return super().pre_validate(form)
def __call__(self, **kwargs):
if self.get_form().is_submitted() and not self.get_form().validate():
return super().__call__(
value=(self.raw_data or [None])[0],
**kwargs
)
try:
value = int(self.data)
value = format_thousands(value)
except (ValueError, TypeError):
value = self.data if self.data is not None else ''
return super().__call__(value=value, **kwargs)
class OrganisationTypeField(RadioField):
def __init__(
self,
*args,
include_only=None,
validators=None,
**kwargs
):
super().__init__(
*args,
choices=[
(value, label) for value, label in Organisation.TYPES
if not include_only or value in include_only
],
thing='the type of organisation',
validators=validators or [],
**kwargs
)
class FieldWithNoneOption():
# 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
NONE_OPTION_VALUE = '__NONE__'
# 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
child_map = {None: [option for option in self
if option.data == self.NONE_OPTION_VALUE]}
# add entries for all other children
for option in self:
# assign all options with a NONE_OPTION_VALUE (not always None) to the None key
if option.data == self.NONE_OPTION_VALUE:
child_ids = [
folder['id'] for folder in self.all_template_folders
if folder['parent_id'] is None]
key = self.NONE_OPTION_VALUE
else:
child_ids = [
folder['id'] for folder in self.all_template_folders
if folder['parent_id'] == option.data]
key = option.data
child_map[key] = [option for option in self if option.data in child_ids]
return child_map
# 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:
item['children'] = self.render_children(field.name, option.label.text, self._children[option.data])
items.append(item)
return items
def render_children(self, name, label, options):
params = {
"name": name,
"fieldset": {
"legend": {
"text": label,
"classes": "govuk-visually-hidden"
}
},
"formGroup": {
"classes": "govuk-form-group--nested"
},
"asList": True,
"items": []
}
for option in options:
item = self.get_item_from_option(option)
if len(self._children[option.data]):
item['children'] = self.render_children(name, option.label.text, self._children[option.data])
params['items'].append(item)
return render_template('forms/fields/checkboxes/template.njk', params=params)
class NestedRadioField(RadioFieldWithNoneOption, NestedFieldMixin):
pass
class NestedCheckboxesField(SelectMultipleField, NestedFieldMixin):
NONE_OPTION_VALUE = None
class HiddenFieldWithNoneOption(FieldWithNoneOption, HiddenField):
pass
class RadioFieldWithRequiredMessage(RadioField):
def __init__(self, *args, required_message='Not a valid choice', **kwargs):
self.required_message = required_message
super().__init__(*args, **kwargs)
def pre_validate(self, form):
try:
return super().pre_validate(form)
except ValueError:
raise ValueError(self.required_message)
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
no_filter_fields = (FieldList, PasswordField)
filters = [strip_whitespace] if not issubclass(unbound_field.field_class, no_filter_fields) else []
filters += unbound_field.kwargs.get('filters', [])
bound = unbound_field.bind(form=form, filters=filters, **options)
bound.get_form = weakref.ref(form) # GC won't collect the form if we don't use a weakref
return bound
class StripWhitespaceStringField(StringField):
def __init__(self, label=None, **kwargs):
kwargs['filters'] = tuple(chain(
kwargs.get('filters', ()),
(
strip_whitespace,
),
))
super(StringField, self).__init__(label, **kwargs)
class PostalAddressField(TextAreaField):
def process_formdata(self, valuelist):
if valuelist:
self.data = PostalAddress(valuelist[0]).normalised
class OnOffField(RadioField):
def __init__(self, label, choices=None, *args, **kwargs):
choices = choices or [
(True, 'On'),
(False, 'Off'),
]
super().__init__(
label,
choices=choices,
thing=f'{choices[0][1].lower()} or {choices[1][1].lower()}',
*args,
**kwargs,
)
def process_formdata(self, valuelist):
if valuelist:
value = valuelist[0]
self.data = (value == 'True') if value in ['True', 'False'] else value
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
yield (
value,
label,
(self.data in {value, self.coerce(value)})
)
class LoginForm(StripWhitespaceForm):
email_address = EmailField('Email address', validators=[
Length(min=5, max=255),
2019-09-12 16:42:33 +01:00
DataRequired(message='Cannot be empty'),
ValidEmail()
])
2015-12-02 15:23:03 +00:00
password = PasswordField('Password', validators=[
DataRequired(message='Enter your password')
])
class RegisterUserForm(StripWhitespaceForm):
2015-12-02 15:23:03 +00:00
name = StringField('Full name',
2019-09-12 16:42:33 +01:00
validators=[DataRequired(message='Cannot be empty')])
email_address = email_address()
mobile_number = international_phone_number()
password = password()
# always register as sms type
auth_type = HiddenField('auth_type', default='sms_auth')
class RegisterUserFromInviteForm(RegisterUserForm):
def __init__(self, invited_user):
super().__init__(
service=invited_user.service,
email_address=invited_user.email_address,
auth_type=invited_user.auth_type,
name=guess_name_from_email_address(
invited_user.email_address
),
)
2017-11-10 12:35:21 +00:00
mobile_number = InternationalPhoneNumber('Mobile number', validators=[])
service = HiddenField('service')
email_address = HiddenField('email_address')
auth_type = HiddenField('auth_type', validators=[DataRequired()])
2017-11-10 12:35:21 +00:00
def validate_mobile_number(self, field):
if self.auth_type.data == 'sms_auth' and not field.data:
2019-09-12 16:42:33 +01:00
raise ValidationError('Cannot be empty')
2017-11-10 12:35:21 +00:00
2018-02-19 16:53:29 +00:00
class RegisterUserFromOrgInviteForm(StripWhitespaceForm):
def __init__(self, invited_org_user):
super().__init__(
organisation=invited_org_user.organisation,
email_address=invited_org_user.email_address,
2018-02-19 16:53:29 +00:00
)
name = StringField(
'Full name',
2019-09-12 16:42:33 +01:00
validators=[DataRequired(message='Cannot be empty')]
2018-02-19 16:53:29 +00:00
)
2019-09-12 16:42:33 +01:00
mobile_number = InternationalPhoneNumber('Mobile number', validators=[DataRequired(message='Cannot be empty')])
2018-02-19 16:53:29 +00:00
password = password()
organisation = HiddenField('organisation')
email_address = HiddenField('email_address')
auth_type = HiddenField('auth_type', validators=[DataRequired()])
class govukCheckboxesMixin:
def extend_params(self, params, extensions):
items = None
param_items = len(params['items']) if 'items' in params else 0
# split items off from params to make it a pure dict
if 'items' in extensions:
items = extensions['items']
del extensions['items']
# merge dicts
params.update(extensions)
# merge items
if items:
if 'items' not in params:
params['items'] = items
else:
for idx, _item in enumerate(items):
if idx >= param_items:
params['items'].append(items[idx])
else:
params['items'][idx].update(items[idx])
class govukCheckboxField(govukCheckboxesMixin, BooleanField):
def __init__(self, label='', validators=None, param_extensions=None, **kwargs):
super(govukCheckboxField, self).__init__(label, validators, false_values=None, **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):
# error messages
error_message = None
if field.errors:
error_message = {
"attributes": {
"data-module": "track-error",
"data-error-type": field.errors[0],
"data-error-label": field.name
},
"text": " ".join(field.errors).strip()
}
params = {
'name': field.name,
'errorMessage': error_message,
'items': [
{
"name": field.name,
"id": field.id,
"text": field.label.text,
"value": 'y',
"checked": field.data
}
]
}
# extend default params with any sent in during instantiation
if self.param_extensions:
self.extend_params(params, self.param_extensions)
# add any sent in though use in templates
if param_extensions:
self.extend_params(params, param_extensions)
return Markup(
render_template('forms/fields/checkboxes/macro.njk', params=params))
# based on work done by @richardjpope: https://github.com/richardjpope/recourse/blob/master/recourse/forms.py#L6
class govukCheckboxesField(govukCheckboxesMixin, SelectMultipleField):
render_as_list = False
def __init__(self, label='', validators=None, param_extensions=None, **kwargs):
super(govukCheckboxesField, 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
"checked": option.checked
}
def get_items_from_options(self, field):
return [self.get_item_from_option(option) for option in field]
def extend_params(self, params, extensions):
items = None
param_items = len(params['items']) if 'items' in params else 0
# split items off from params to make it a pure dict
if 'items' in extensions:
items = extensions['items']
del extensions['items']
# merge dicts
params.update(extensions)
# merge items
if items:
if 'items' not in params:
params['items'] = items
else:
for idx, _item in enumerate(items):
if idx >= param_items:
params['items'].append(items[idx])
else:
params['items'][idx].update(items[idx])
# 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):
# error messages
error_message = None
if field.errors:
error_message = {
"attributes": {
"data-module": "track-error",
"data-error-type": field.errors[0],
"data-error-label": field.name
},
"text": " ".join(field.errors).strip()
}
# 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 = {
'name': field.name,
"fieldset": {
"attributes": {"id": field.name},
"legend": {
"text": field.label.text,
"classes": "govuk-fieldset__legend--s"
}
},
"asList": self.render_as_list,
'errorMessage': error_message,
'items': items
}
# extend default params with any sent in during instantiation
if self.param_extensions:
self.extend_params(params, self.param_extensions)
# add any sent in though use in templates
if param_extensions:
self.extend_params(params, param_extensions)
return Markup(
render_template('forms/fields/checkboxes/macro.njk', params=params))
# Extends fields using the govukCheckboxesField interface to wrap their render in HTML needed by the collapsible JS
class govukCollapsibleCheckboxesMixin:
def __init__(self, label='', validators=None, field_label='', param_extensions=None, **kwargs):
super(govukCollapsibleCheckboxesMixin, self).__init__(label, validators, param_extensions, **kwargs)
self.field_label = field_label
def widget(self, field, **kwargs):
# add a blank hint to act as an ARIA live-region
if self.param_extensions is not None:
self.param_extensions.update(
{"hint": {"html": "<div class=\"selection-summary\" role=\"region\" aria-live=\"polite\"></div>"}})
else:
self.param_extensions = \
{"hint": {"html": "<div class=\"selection-summary\" role=\"region\" aria-live=\"polite\"></div>"}}
# wrap the checkboxes HTML in the HTML needed by the collapisble JS
return Markup(
f'<div class="selection-wrapper"'
f' data-module="collapsible-checkboxes"'
f' data-field-label="{self.field_label}">'
f' {super(govukCollapsibleCheckboxesMixin, self).widget(field, **kwargs)}'
f'</div>'
)
class govukCollapsibleCheckboxesField(govukCollapsibleCheckboxesMixin, govukCheckboxesField):
pass
# govukCollapsibleCheckboxesMixin adds an ARIA live-region to the hint and wraps the render in HTML needed by the
# collapsible JS
# NestedFieldMixin puts the items into a tree hierarchy, pre-rendering the sub-trees of the top-level items
class govukCollapsibleNestedCheckboxesField(govukCollapsibleCheckboxesMixin, NestedFieldMixin, govukCheckboxesField):
NONE_OPTION_VALUE = None
render_as_list = True
# guard against data entries that aren't a role in permissions
def filter_by_permissions(valuelist):
if valuelist is None:
return None
else:
return [entry for entry in valuelist if any(entry in role for role in permissions)]
# guard against data entries that aren't a role in broadcast_permissions
def filter_by_broadcast_permissions(valuelist):
if valuelist is None:
return None
else:
return [entry for entry in valuelist if any(entry in role for role in broadcast_permissions)]
class BasePermissionsForm(StripWhitespaceForm):
def __init__(self, all_template_folders=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.folder_permissions.choices = []
if all_template_folders is not None:
self.folder_permissions.all_template_folders = all_template_folders
self.folder_permissions.choices = [
(item['id'], item['name']) for item in ([{'name': 'Templates', 'id': None}] + all_template_folders)
]
folder_permissions = govukCollapsibleNestedCheckboxesField(
'Folders this team member can see',
field_label='folder')
login_authentication = RadioField(
'Sign in using',
choices=[
('sms_auth', 'Text message code'),
('email_auth', 'Email link'),
],
thing='how this team member should sign in',
validators=[DataRequired()]
)
2016-03-03 13:00:12 +00:00
permissions_field = govukCheckboxesField(
'Permissions',
filters=[filter_by_permissions],
choices=[
(value, label) for value, label in permissions
],
param_extensions={
"hint": {"text": "All team members can see sent messages."}
}
)
@property
def permissions(self):
return set(self.permissions_field.data)
@classmethod
def from_user(cls, user, service_id, **kwargs):
return cls(
**kwargs,
**{
"permissions_field": [
role for role in roles.keys() if user.has_permission_for_service(service_id, role)]
},
login_authentication=user.auth_type
)
class PermissionsForm(BasePermissionsForm):
pass
class BroadcastPermissionsForm(BasePermissionsForm):
permissions_field = govukCheckboxesField(
'Permissions',
choices=[
(value, label) for value, label in broadcast_permissions
],
filters=[filter_by_broadcast_permissions],
param_extensions={
"hint": {"text": "All team members can see sent messages."}
}
)
@property
def permissions(self):
return {'view_activity'} | super().permissions
class BaseInviteUserForm():
email_address = email_address(gov_user=False)
def __init__(self, invalid_email_address, *args, **kwargs):
super().__init__(*args, **kwargs)
self.invalid_email_address = invalid_email_address.lower()
def validate_email_address(self, field):
if field.data.lower() == self.invalid_email_address and not current_user.platform_admin:
2019-09-12 16:42:33 +01:00
raise ValidationError("You cannot send an invitation to yourself")
class InviteUserForm(BaseInviteUserForm, PermissionsForm):
pass
class BroadcastInviteUserForm(BaseInviteUserForm, BroadcastPermissionsForm):
pass
2018-02-19 16:53:29 +00:00
class InviteOrgUserForm(StripWhitespaceForm):
email_address = email_address(gov_user=False)
def __init__(self, invalid_email_address, *args, **kwargs):
super(InviteOrgUserForm, self).__init__(*args, **kwargs)
self.invalid_email_address = invalid_email_address.lower()
def validate_email_address(self, field):
if field.data.lower() == self.invalid_email_address and not current_user.platform_admin:
2019-09-12 16:42:33 +01:00
raise ValidationError("You cannot send an invitation to yourself")
2018-02-19 16:53:29 +00:00
class TwoFactorForm(StripWhitespaceForm):
def __init__(self, validate_code_func, *args, **kwargs):
'''
Keyword arguments:
validate_code_func -- Validates the code with the API.
'''
self.validate_code_func = validate_code_func
super(TwoFactorForm, self).__init__(*args, **kwargs)
sms_code = SMSCode('Text message code')
def validate(self):
if not self.sms_code.validate(self):
return False
is_valid, reason = self.validate_code_func(self.sms_code.data)
if not is_valid:
self.sms_code.errors.append(reason)
return False
return True
class TextNotReceivedForm(StripWhitespaceForm):
mobile_number = international_phone_number()
class RenameServiceForm(StripWhitespaceForm):
name = StringField(
u'Service name',
validators=[
2019-11-08 17:12:32 +00:00
DataRequired(message='Cannot be empty'),
MustContainAlphanumericCharacters()
])
2018-03-06 17:12:31 +00:00
class RenameOrganisationForm(StripWhitespaceForm):
name = StringField(
u'Organisation name',
validators=[
2019-11-08 17:12:32 +00:00
DataRequired(message='Cannot be empty'),
MustContainAlphanumericCharacters()
2018-03-06 17:12:31 +00:00
])
class AddGPOrganisationForm(StripWhitespaceForm):
def __init__(self, *args, service_name='unknown', **kwargs):
super().__init__(*args, **kwargs)
self.same_as_service_name.label.text = 'Is your GP practice called {}?'.format(service_name)
self.service_name = service_name
def get_organisation_name(self):
if self.same_as_service_name.data:
return self.service_name
return self.name.data
same_as_service_name = OnOffField(
'Is your GP practice called the same name as your service?',
choices=(
(True, 'Yes'),
(False, 'No'),
),
)
name = StringField(
'Whats your practice called?',
)
def validate_name(self, field):
if self.same_as_service_name.data is False:
if not field.data:
2019-09-12 16:42:33 +01:00
raise ValidationError('Cannot be empty')
else:
field.data = ''
class AddNHSLocalOrganisationForm(StripWhitespaceForm):
def __init__(self, *args, organisation_choices=None, **kwargs):
super().__init__(*args, **kwargs)
self.organisations.choices = organisation_choices
organisations = RadioField(
'Which NHS Trust or Clinical Commissioning Group do you work for?',
thing='an NHS Trust or Clinical Commissioning Group'
)
class OrganisationOrganisationTypeForm(StripWhitespaceForm):
organisation_type = OrganisationTypeField('What type of organisation is this?')
class OrganisationCrownStatusForm(StripWhitespaceForm):
crown_status = RadioField(
(
'Is this organisation a crown body?'
),
choices=[
('crown', 'Yes'),
('non-crown', 'No'),
('unknown', 'Not sure'),
],
thing='whether this organisation is a crown body',
)
class OrganisationAgreementSignedForm(StripWhitespaceForm):
agreement_signed = RadioField(
(
'Has this organisation signed the agreement?'
),
choices=[
('yes', 'Yes'),
('no', 'No'),
('unknown', 'No (but we have some service-specific agreements in place)'),
],
thing='whether this organisation has signed the agreement',
)
class OrganisationDomainsForm(StripWhitespaceForm):
def populate(self, domains_list):
for index, value in enumerate(domains_list):
self.domains[index].data = value
domains = FieldList(
StripWhitespaceStringField(
'',
validators=[
Optional(),
],
default=''
),
min_entries=20,
max_entries=20,
label="Domain names"
)
class CreateServiceForm(StripWhitespaceForm):
name = StringField(
"Whats your service called?",
validators=[
2019-11-08 17:12:32 +00:00
DataRequired(message='Cannot be empty'),
MustContainAlphanumericCharacters()
])
organisation_type = OrganisationTypeField('Who runs this service?')
class CreateNhsServiceForm(CreateServiceForm):
organisation_type = OrganisationTypeField(
'Who runs this service?',
include_only={'nhs_central', 'nhs_local', 'nhs_gp'},
)
class NewOrganisationForm(
RenameOrganisationForm,
OrganisationOrganisationTypeForm,
OrganisationCrownStatusForm,
):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Dont offer the not sure choice
self.crown_status.choices = self.crown_status.choices[:-1]
class FreeSMSAllowance(StripWhitespaceForm):
free_sms_allowance = IntegerField(
'Numbers of text message fragments per year',
validators=[
2019-09-12 16:42:33 +01:00
DataRequired(message='Cannot be empty')
]
)
class ConfirmPasswordForm(StripWhitespaceForm):
def __init__(self, validate_password_func, *args, **kwargs):
self.validate_password_func = validate_password_func
super(ConfirmPasswordForm, self).__init__(*args, **kwargs)
password = PasswordField(u'Enter password')
def validate_password(self, field):
if not self.validate_password_func(field.data):
raise ValidationError('Invalid password')
class BaseTemplateForm(StripWhitespaceForm):
name = StringField(
u'Template name',
2019-09-12 16:42:33 +01:00
validators=[DataRequired(message="Cannot be empty")])
template_content = TextAreaField(
u'Message',
validators=[
2019-09-12 16:42:33 +01:00
DataRequired(message="Cannot be empty"),
2017-02-15 15:06:47 +00:00
NoCommasInPlaceHolders()
]
)
process_type = RadioField(
'Use priority queue?',
choices=[
('priority', 'Yes'),
('normal', 'No'),
],
thing='yes or no',
validators=[DataRequired()],
default='normal'
)
2017-02-15 15:06:47 +00:00
class SMSTemplateForm(BaseTemplateForm):
def validate_template_content(self, field):
OnlySMSCharacters(template_type='sms')(None, field)
2017-02-15 15:06:47 +00:00
class BroadcastTemplateForm(SMSTemplateForm):
def validate_template_content(self, field):
OnlySMSCharacters(template_type='broadcast')(None, field)
class LetterAddressForm(StripWhitespaceForm):
def __init__(self, *args, allow_international_letters=False, **kwargs):
self.allow_international_letters = allow_international_letters
super().__init__(*args, **kwargs)
address = PostalAddressField(
'Address',
validators=[DataRequired(message="Cannot be empty")]
)
def validate_address(self, field):
address = PostalAddress(
field.data,
allow_international_letters=self.allow_international_letters,
)
if not address.has_enough_lines:
raise ValidationError(
f'Address must be at least {PostalAddress.MIN_LINES} lines long'
)
if address.has_too_many_lines:
raise ValidationError(
f'Address must be no more than {PostalAddress.MAX_LINES} lines long'
)
if not address.has_valid_last_line:
if self.allow_international_letters:
raise ValidationError(
f'Last line of the address must be a UK postcode or another country'
)
raise ValidationError(
f'Last line of the address must be a real UK postcode'
)
if address.has_invalid_characters:
raise ValidationError(
'Address lines must not start with any of the following characters: @ ( ) = [ ] ” \\ / ,'
)
2017-02-15 15:06:47 +00:00
class EmailTemplateForm(BaseTemplateForm):
subject = TextAreaField(
u'Subject',
2019-09-12 16:42:33 +01:00
validators=[DataRequired(message="Cannot be empty")])
class LetterTemplateForm(EmailTemplateForm):
subject = TextAreaField(
u'Main heading',
2019-09-12 16:42:33 +01:00
validators=[DataRequired(message="Cannot be empty")])
template_content = TextAreaField(
u'Body',
validators=[
2019-09-12 16:42:33 +01:00
DataRequired(message="Cannot be empty"),
NoCommasInPlaceHolders()
]
)
class LetterTemplatePostageForm(StripWhitespaceForm):
postage = RadioField(
'Choose the postage for this letter template',
choices=[
('first', 'First class'),
('second', 'Second class'),
],
thing='first class or second class',
validators=[DataRequired()]
)
class LetterUploadPostageForm(StripWhitespaceForm):
def __init__(self, *args, postage_zone, **kwargs):
super().__init__(*args, **kwargs)
if postage_zone != Postage.UK:
self.postage.choices = [(postage_zone, '')]
self.postage.data = postage_zone
@property
def show_postage(self):
return len(self.postage.choices) > 1
postage = RadioField(
'Choose the postage for this letter',
choices=[
('first', 'First class post'),
('second', 'Second class post'),
],
default='second',
validators=[DataRequired()]
)
class ForgotPasswordForm(StripWhitespaceForm):
email_address = email_address(gov_user=False)
2016-01-04 14:00:39 +00:00
class NewPasswordForm(StripWhitespaceForm):
new_password = password()
class ChangePasswordForm(StripWhitespaceForm):
def __init__(self, validate_password_func, *args, **kwargs):
self.validate_password_func = validate_password_func
super(ChangePasswordForm, self).__init__(*args, **kwargs)
old_password = password('Current password')
new_password = password('New password')
def validate_old_password(self, field):
if not self.validate_password_func(field.data):
raise ValidationError('Invalid password')
class CsvUploadForm(StripWhitespaceForm):
file = FileField('Add recipients', validators=[DataRequired(
message='Please pick a file'), CsvFileValidator()])
class ChangeNameForm(StripWhitespaceForm):
new_name = StringField(u'Your name')
class ChangeEmailForm(StripWhitespaceForm):
def __init__(self, validate_email_func, *args, **kwargs):
self.validate_email_func = validate_email_func
super(ChangeEmailForm, self).__init__(*args, **kwargs)
email_address = email_address()
def validate_email_address(self, field):
is_valid = self.validate_email_func(field.data)
2018-02-19 16:53:29 +00:00
if is_valid:
raise ValidationError("The email address is already in use")
class ChangeNonGovEmailForm(ChangeEmailForm):
email_address = email_address(gov_user=False)
class ChangeMobileNumberForm(StripWhitespaceForm):
mobile_number = international_phone_number()
class ChooseTimeForm(StripWhitespaceForm):
def __init__(self, *args, **kwargs):
super(ChooseTimeForm, self).__init__(*args, **kwargs)
self.scheduled_for.choices = [('', 'Now')] + [
get_time_value_and_label(hour) for hour in get_next_hours_until(
get_furthest_possible_scheduled_time()
)
]
self.scheduled_for.categories = get_next_days_until(get_furthest_possible_scheduled_time())
scheduled_for = RadioField(
'When should Notify send these messages?',
default='',
)
class ChooseBroadcastDurationForm(StripWhitespaceForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.finishes_at.choices = [
get_time_value_and_label(hour) for hour in get_next_hours_until(
get_furthest_possible_scheduled_time()
)
]
self.finishes_at.categories = get_next_days_until(
get_furthest_possible_scheduled_time()
)
finishes_at = RadioField(
Make start time explicit when previewing a broadcast We recently introduced a form control that lets user choose when a broadcast ends. Based on the most recent research participant, we think: - there is a specific misunderstanding of what this control does - there is a general low level of understanding of what a ‘broadcast’ means People will try to understand what a ‘broadcast’ is by using mental models they have for other kinds of messaging, for example text messages. Other kinds of messaging are one-to-one, i.e. they go from a sender to a recipient. They are not ongoing in any way. Emails and texts are sent at a time (and for all practicable purposes are received at that same time). So, when we present the user with a form that controls time, they might well assume it controls the time when the message will be sent. This is a feature we offer for sending messages using a spreadsheet, and that’s where we’ve borrowed this pattern from. We reinforce this assumption with the labelling of the form control. By front-loading it with the word ‘When’ we are playing to the users confirmation bias, i.e. they are interpreting the meaning of the control in a way that confirms their prior beliefs about how messaging works. So this commit does two things: - re-labels the form to front-load the word ‘End’ not ‘When’ - adds text to the page explaining when the broadcast will start, so there’s a chance of overriding that confirmation bias If we can get users to go through this before sending a broadcast for real, it could help them learn what a broadcast is, and how it differs from sending text messages.
2020-07-27 17:25:13 +01:00
'End time',
)
class CreateKeyForm(StripWhitespaceForm):
def __init__(self, existing_keys, *args, **kwargs):
self.existing_key_names = [
key['name'].lower() for key in existing_keys
if not key['expiry_date']
]
super().__init__(*args, **kwargs)
key_type = RadioField(
'Type of key',
thing='the type of key',
)
key_name = StringField(u'Description of key', validators=[
DataRequired(message='You need to give the key a name')
])
def validate_key_name(self, key_name):
if key_name.data.lower() in self.existing_key_names:
raise ValidationError('A key with this name already exists')
class SupportType(StripWhitespaceForm):
support_type = RadioField(
'How can we help you?',
choices=[
(PROBLEM_TICKET_TYPE, 'Report a problem'),
(QUESTION_TICKET_TYPE, 'Ask a question or give feedback'),
],
)
class SupportRedirect(StripWhitespaceForm):
who = RadioField(
'What do you need help with?',
choices=[
('public-sector', 'I work in the public sector and need to send emails, text messages or letters'),
('public', 'Im a member of the public with a question for the government'),
],
)
class FeedbackOrProblem(StripWhitespaceForm):
name = StringField('Name (optional)')
email_address = email_address(label='Email address', gov_user=False, required=True)
2019-09-12 16:42:33 +01:00
feedback = TextAreaField('Your message', validators=[DataRequired(message="Cannot be empty")])
class Triage(StripWhitespaceForm):
severe = RadioField(
'Is it an emergency?',
choices=[
('yes', 'Yes'),
('no', 'No'),
],
thing='yes or no',
)
class EstimateUsageForm(StripWhitespaceForm):
volume_email = ForgivingIntegerField(
'How many emails do you expect to send in the next year?',
things='emails',
format_error_suffix='you expect to send',
)
volume_sms = ForgivingIntegerField(
'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
)
volume_letter = ForgivingIntegerField(
'How many letters do you expect to send in the next year?',
things='letters',
format_error_suffix='you expect to send',
2016-10-24 13:38:55 +01:00
)
consent_to_research = RadioField(
'Can we contact you when were doing user research?',
choices=[
('yes', 'Yes'),
('no', 'No'),
],
thing='yes or no',
)
at_least_one_volume_filled = True
def validate(self, *args, **kwargs):
if self.volume_email.data == self.volume_sms.data == self.volume_letter.data == 0:
self.at_least_one_volume_filled = False
return False
return super().validate(*args, **kwargs)
class ProviderForm(StripWhitespaceForm):
priority = IntegerField('Priority', [validators.NumberRange(min=1, max=100, message="Must be between 1 and 100")])
class ProviderRatioForm(StripWhitespaceForm):
ratio = RadioField(choices=[
(str(value), '{}% / {}%'.format(value, 100 - value))
for value in range(100, -10, -10)
])
@property
def percentage_left(self):
return int(self.ratio.data)
@property
def percentage_right(self):
return 100 - self.percentage_left
class ServiceContactDetailsForm(StripWhitespaceForm):
contact_details_type = RadioField(
'Type of contact details',
choices=[
('url', 'Link'),
('email_address', 'Email address'),
('phone_number', 'Phone number'),
],
)
url = StringField("URL")
email_address = EmailField("Email address")
phone_number = StringField("Phone number")
def validate(self):
if self.contact_details_type.data == 'url':
self.url.validators = [DataRequired(), URL(message='Must be a valid URL')]
elif self.contact_details_type.data == 'email_address':
self.email_address.validators = [DataRequired(), Length(min=5, max=255), ValidEmail()]
elif self.contact_details_type.data == 'phone_number':
# we can't use the existing phone number validation functions here since we want to allow landlines
def valid_phone_number(self, num):
try:
normalise_phone_number(num.data)
return True
except InvalidPhoneError:
raise ValidationError('Must be a valid phone number')
self.phone_number.validators = [DataRequired(), Length(min=5, max=20), valid_phone_number]
return super().validate()
class ServiceReplyToEmailForm(StripWhitespaceForm):
2019-07-09 16:47:11 +01:00
email_address = email_address(label='Reply-to email address', gov_user=False)
is_default = govukCheckboxField("Make this email address the default")
class ServiceSmsSenderForm(StripWhitespaceForm):
sms_sender = StringField(
'Text message sender',
validators=[
2019-09-12 16:42:33 +01:00
DataRequired(message="Cannot be empty"),
Length(max=11, message="Enter 11 characters or fewer"),
Length(min=3, message="Enter 3 characters or more"),
2020-04-02 15:57:46 +01:00
LettersNumbersFullStopsAndUnderscoresOnly(),
DoesNotStartWithDoubleZero(),
]
)
is_default = govukCheckboxField("Make this text message sender the default")
class ServiceEditInboundNumberForm(StripWhitespaceForm):
is_default = govukCheckboxField("Make this text message sender the default")
2017-10-30 14:30:43 +00:00
class ServiceLetterContactBlockForm(StripWhitespaceForm):
letter_contact_block = TextAreaField(
validators=[
2019-09-12 16:42:33 +01:00
DataRequired(message="Cannot be empty"),
NoCommasInPlaceHolders()
]
)
is_default = govukCheckboxField("Set as your default address")
2017-10-27 10:56:03 +01:00
def validate_letter_contact_block(self, field):
line_count = field.data.strip().count('\n')
if line_count >= 10:
raise ValidationError(
'Contains {} lines, maximum is 10'.format(line_count + 1)
)
class ServiceOnOffSettingForm(StripWhitespaceForm):
def __init__(self, name, *args, truthy='On', falsey='Off', **kwargs):
super().__init__(*args, **kwargs)
self.enabled.label.text = name
self.enabled.choices = [
(True, truthy),
(False, falsey),
]
enabled = OnOffField('Choices')
class ServiceSwitchChannelForm(ServiceOnOffSettingForm):
def __init__(self, channel, *args, **kwargs):
name = 'Send {}'.format({
'email': 'emails',
'sms': 'text messages',
'letter': 'letters',
}.get(channel))
super().__init__(name, *args, **kwargs)
class SetEmailBranding(StripWhitespaceForm):
branding_style = RadioFieldWithNoneOption(
2018-02-07 10:30:49 +00:00
'Branding style',
thing='a branding style',
)
Add a page to manage a service’s whitelist Services who are in alpha or building prototypes need a way of sending to any email address or phone number without having to sign the MOU. This commit adds a page where they can whitelist up to 5 email addresses and 5 phone numbers. It uses the ‘list entry’ UI pattern from the Digital Marketplace frontend toolkit [1] [2] [3]. I had to do some modification: - of the Javascript, to make it work with the GOV.UK Module pattern - of the template to make it work with WTForms - of the content security policy, because the list entry pattern uses Hogan[1], which needs to use `eval()` (this should be fine if we’re only allowing it for scripts that we serve) - of our SASS lint config, to allow browser-targeting mixins to come after normal rules (so that they can override them) This commit also adds a new form class to validate and populate the two whitelists. The validation is fairly rudimentary at the moment, and doesn’t highlight which item in the list has the error, but it’s probably good enough. The list can only be updated all-at-once, this is how it’s possible to remove items from the list without having to make multiple `POST` requests. 1. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/templates/forms/list-entry.html 2. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/scss/forms/_list-entry.scss 3. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/javascripts/list-entry.js 4. http://twitter.github.io/hogan.js/
2016-09-20 12:30:00 +01:00
DEFAULT = (FieldWithNoneOption.NONE_OPTION_VALUE, 'GOV.UK')
def __init__(self, all_branding_options, current_branding):
super().__init__(branding_style=current_branding)
self.branding_style.choices = sorted(
all_branding_options + [self.DEFAULT],
key=lambda branding: (
branding[0] != current_branding,
branding[0] is not self.DEFAULT[0],
branding[1].lower(),
),
)
Add a page to manage a service’s whitelist Services who are in alpha or building prototypes need a way of sending to any email address or phone number without having to sign the MOU. This commit adds a page where they can whitelist up to 5 email addresses and 5 phone numbers. It uses the ‘list entry’ UI pattern from the Digital Marketplace frontend toolkit [1] [2] [3]. I had to do some modification: - of the Javascript, to make it work with the GOV.UK Module pattern - of the template to make it work with WTForms - of the content security policy, because the list entry pattern uses Hogan[1], which needs to use `eval()` (this should be fine if we’re only allowing it for scripts that we serve) - of our SASS lint config, to allow browser-targeting mixins to come after normal rules (so that they can override them) This commit also adds a new form class to validate and populate the two whitelists. The validation is fairly rudimentary at the moment, and doesn’t highlight which item in the list has the error, but it’s probably good enough. The list can only be updated all-at-once, this is how it’s possible to remove items from the list without having to make multiple `POST` requests. 1. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/templates/forms/list-entry.html 2. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/scss/forms/_list-entry.scss 3. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/javascripts/list-entry.js 4. http://twitter.github.io/hogan.js/
2016-09-20 12:30:00 +01:00
class SetLetterBranding(SetEmailBranding):
# form is the same, but instead of GOV.UK we have None as a valid option
DEFAULT = (FieldWithNoneOption.NONE_OPTION_VALUE, 'None')
class PreviewBranding(StripWhitespaceForm):
branding_style = HiddenFieldWithNoneOption('branding_style')
2018-02-07 10:30:49 +00:00
class ServiceUpdateEmailBranding(StripWhitespaceForm):
name = StringField('Name of brand')
text = StringField('Text')
2018-02-07 10:30:49 +00:00
colour = StringField(
'Colour',
validators=[
Regexp(regex="^$|^#(?:[0-9a-fA-F]{3}){1,2}$", message='Must be a valid color hex code (starting with #)')
2018-02-07 10:30:49 +00:00
]
)
2017-07-28 15:17:50 +01:00
file = FileField_wtf('Upload a PNG logo', validators=[FileAllowed(['png'], 'PNG Images only!')])
brand_type = RadioField(
2018-08-23 17:44:34 +01:00
"Brand type",
choices=[
('both', 'GOV.UK and branding'),
('org', 'Branding only'),
('org_banner', 'Branding banner'),
]
)
2017-07-28 15:17:50 +01:00
def validate_name(self, name):
op = request.form.get('operation')
if op == 'email-branding-details' and not self.name.data:
raise ValidationError('This field is required')
2017-07-28 15:17:50 +01:00
class SVGFileUpload(StripWhitespaceForm):
file = FileField_wtf(
'Upload an SVG logo',
validators=[
FileAllowed(['svg'], 'SVG Images only!'),
DataRequired(message="You need to upload a file to submit"),
NoEmbeddedImagesInSVG()
]
)
class ServiceLetterBrandingDetails(StripWhitespaceForm):
name = StringField('Name of brand', validators=[DataRequired()])
class PDFUploadForm(StripWhitespaceForm):
file = FileField_wtf(
'Upload a letter in PDF format',
validators=[
FileAllowed(['pdf'], 'Save your letter as a PDF and try again.'),
DataRequired(message="You need to choose a file to upload")
]
)
class EmailFieldInGuestList(EmailField, StripWhitespaceStringField):
pass
class InternationalPhoneNumberInGuestList(InternationalPhoneNumber, StripWhitespaceStringField):
pass
class GuestList(StripWhitespaceForm):
Add a page to manage a service’s whitelist Services who are in alpha or building prototypes need a way of sending to any email address or phone number without having to sign the MOU. This commit adds a page where they can whitelist up to 5 email addresses and 5 phone numbers. It uses the ‘list entry’ UI pattern from the Digital Marketplace frontend toolkit [1] [2] [3]. I had to do some modification: - of the Javascript, to make it work with the GOV.UK Module pattern - of the template to make it work with WTForms - of the content security policy, because the list entry pattern uses Hogan[1], which needs to use `eval()` (this should be fine if we’re only allowing it for scripts that we serve) - of our SASS lint config, to allow browser-targeting mixins to come after normal rules (so that they can override them) This commit also adds a new form class to validate and populate the two whitelists. The validation is fairly rudimentary at the moment, and doesn’t highlight which item in the list has the error, but it’s probably good enough. The list can only be updated all-at-once, this is how it’s possible to remove items from the list without having to make multiple `POST` requests. 1. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/templates/forms/list-entry.html 2. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/scss/forms/_list-entry.scss 3. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/javascripts/list-entry.js 4. http://twitter.github.io/hogan.js/
2016-09-20 12:30:00 +01:00
def populate(self, email_addresses, phone_numbers):
for form_field, existing_guest_list in (
Add a page to manage a service’s whitelist Services who are in alpha or building prototypes need a way of sending to any email address or phone number without having to sign the MOU. This commit adds a page where they can whitelist up to 5 email addresses and 5 phone numbers. It uses the ‘list entry’ UI pattern from the Digital Marketplace frontend toolkit [1] [2] [3]. I had to do some modification: - of the Javascript, to make it work with the GOV.UK Module pattern - of the template to make it work with WTForms - of the content security policy, because the list entry pattern uses Hogan[1], which needs to use `eval()` (this should be fine if we’re only allowing it for scripts that we serve) - of our SASS lint config, to allow browser-targeting mixins to come after normal rules (so that they can override them) This commit also adds a new form class to validate and populate the two whitelists. The validation is fairly rudimentary at the moment, and doesn’t highlight which item in the list has the error, but it’s probably good enough. The list can only be updated all-at-once, this is how it’s possible to remove items from the list without having to make multiple `POST` requests. 1. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/templates/forms/list-entry.html 2. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/scss/forms/_list-entry.scss 3. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/javascripts/list-entry.js 4. http://twitter.github.io/hogan.js/
2016-09-20 12:30:00 +01:00
(self.email_addresses, email_addresses),
(self.phone_numbers, phone_numbers)
):
for index, value in enumerate(existing_guest_list):
Add a page to manage a service’s whitelist Services who are in alpha or building prototypes need a way of sending to any email address or phone number without having to sign the MOU. This commit adds a page where they can whitelist up to 5 email addresses and 5 phone numbers. It uses the ‘list entry’ UI pattern from the Digital Marketplace frontend toolkit [1] [2] [3]. I had to do some modification: - of the Javascript, to make it work with the GOV.UK Module pattern - of the template to make it work with WTForms - of the content security policy, because the list entry pattern uses Hogan[1], which needs to use `eval()` (this should be fine if we’re only allowing it for scripts that we serve) - of our SASS lint config, to allow browser-targeting mixins to come after normal rules (so that they can override them) This commit also adds a new form class to validate and populate the two whitelists. The validation is fairly rudimentary at the moment, and doesn’t highlight which item in the list has the error, but it’s probably good enough. The list can only be updated all-at-once, this is how it’s possible to remove items from the list without having to make multiple `POST` requests. 1. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/templates/forms/list-entry.html 2. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/scss/forms/_list-entry.scss 3. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/javascripts/list-entry.js 4. http://twitter.github.io/hogan.js/
2016-09-20 12:30:00 +01:00
form_field[index].data = value
email_addresses = FieldList(
EmailFieldInGuestList(
Add a page to manage a service’s whitelist Services who are in alpha or building prototypes need a way of sending to any email address or phone number without having to sign the MOU. This commit adds a page where they can whitelist up to 5 email addresses and 5 phone numbers. It uses the ‘list entry’ UI pattern from the Digital Marketplace frontend toolkit [1] [2] [3]. I had to do some modification: - of the Javascript, to make it work with the GOV.UK Module pattern - of the template to make it work with WTForms - of the content security policy, because the list entry pattern uses Hogan[1], which needs to use `eval()` (this should be fine if we’re only allowing it for scripts that we serve) - of our SASS lint config, to allow browser-targeting mixins to come after normal rules (so that they can override them) This commit also adds a new form class to validate and populate the two whitelists. The validation is fairly rudimentary at the moment, and doesn’t highlight which item in the list has the error, but it’s probably good enough. The list can only be updated all-at-once, this is how it’s possible to remove items from the list without having to make multiple `POST` requests. 1. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/templates/forms/list-entry.html 2. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/scss/forms/_list-entry.scss 3. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/javascripts/list-entry.js 4. http://twitter.github.io/hogan.js/
2016-09-20 12:30:00 +01:00
'',
validators=[
Optional(),
ValidEmail()
Add a page to manage a service’s whitelist Services who are in alpha or building prototypes need a way of sending to any email address or phone number without having to sign the MOU. This commit adds a page where they can whitelist up to 5 email addresses and 5 phone numbers. It uses the ‘list entry’ UI pattern from the Digital Marketplace frontend toolkit [1] [2] [3]. I had to do some modification: - of the Javascript, to make it work with the GOV.UK Module pattern - of the template to make it work with WTForms - of the content security policy, because the list entry pattern uses Hogan[1], which needs to use `eval()` (this should be fine if we’re only allowing it for scripts that we serve) - of our SASS lint config, to allow browser-targeting mixins to come after normal rules (so that they can override them) This commit also adds a new form class to validate and populate the two whitelists. The validation is fairly rudimentary at the moment, and doesn’t highlight which item in the list has the error, but it’s probably good enough. The list can only be updated all-at-once, this is how it’s possible to remove items from the list without having to make multiple `POST` requests. 1. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/templates/forms/list-entry.html 2. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/scss/forms/_list-entry.scss 3. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/javascripts/list-entry.js 4. http://twitter.github.io/hogan.js/
2016-09-20 12:30:00 +01:00
],
default=''
),
min_entries=5,
max_entries=5,
label="Email addresses"
)
phone_numbers = FieldList(
InternationalPhoneNumberInGuestList(
Add a page to manage a service’s whitelist Services who are in alpha or building prototypes need a way of sending to any email address or phone number without having to sign the MOU. This commit adds a page where they can whitelist up to 5 email addresses and 5 phone numbers. It uses the ‘list entry’ UI pattern from the Digital Marketplace frontend toolkit [1] [2] [3]. I had to do some modification: - of the Javascript, to make it work with the GOV.UK Module pattern - of the template to make it work with WTForms - of the content security policy, because the list entry pattern uses Hogan[1], which needs to use `eval()` (this should be fine if we’re only allowing it for scripts that we serve) - of our SASS lint config, to allow browser-targeting mixins to come after normal rules (so that they can override them) This commit also adds a new form class to validate and populate the two whitelists. The validation is fairly rudimentary at the moment, and doesn’t highlight which item in the list has the error, but it’s probably good enough. The list can only be updated all-at-once, this is how it’s possible to remove items from the list without having to make multiple `POST` requests. 1. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/templates/forms/list-entry.html 2. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/scss/forms/_list-entry.scss 3. https://github.com/alphagov/digitalmarketplace-frontend-toolkit/blob/434ad307913651ecb041ab94bdee748ebe066d1a/toolkit/javascripts/list-entry.js 4. http://twitter.github.io/hogan.js/
2016-09-20 12:30:00 +01:00
'',
validators=[
Optional()
],
default=''
),
min_entries=5,
max_entries=5,
label="Mobile numbers"
)
class DateFilterForm(StripWhitespaceForm):
start_date = DateField("Start Date", [validators.optional()])
end_date = DateField("End Date", [validators.optional()])
include_from_test_key = govukCheckboxField("Include test keys")
Merge email, text message + letter templates pages Right now we have separate pages for email and text message templates. In the future we will also have a separate page for letter templates. This commit changes Notify to only have one page for all templates. What is the problem? --- The left-hand navigation is getting quite crowded, at 8 items for a service that can send letters. Research suggests that the number of objects an average human can hold in working memory is 7 ± 2 [1]. So we’re at the limit of how many items the navigation should have. In the future we will need to search/sort/filter templates by attributes other than type, for example: - show me the ‘confirmation’ templates - show me the most recently used templates - show me all templates containing the placeholder `((ref_no))` These are hypothetical for now, but these needs (or others) may become real in the future. At this point pre-filtering the list of templates by type would restrict what searches a user could do. So by making this change now we’re in a better position to iterate the design in the future. What’s the change? --- This commit replaces the ‘Email templates’, ‘Text message templates’ and ‘Letter templates’ pages with one page called ‘Templates’. This new templates page shows all the templates for the service, sorted by most recently created first (as before). To add a new template there is a new page with a form asking you what kind of template you want to create. This is necessary because in the past we knew what kind of template you wanted to create based on the kind you were looking at. What’s the impact of this change on new users? --- This change alters the onboarding process slightly. We still want to take people through the empty templates page from the call-to-action on the dashboard because it helps them understand that to send a message using Notify you need a template. But because we don’t have separate pages for emails/text messages we will have to send users through the extra step of choosing what kind of template to create. This is a bit clunkier on first use but: - it still gets the point across - it takes them through the actual flow they will be using to create new templates in the future (ie they’re learning how to use Notify, not just being taken through a special onboarding route) I’m not too worried about this change in terms of the experience for new users. Furthermore, by making it now we get to validate whether it’s causing any problems in the lab research booked for next week. What’s the impact of this change on current services? --- Looking at the top 15 services by number of templates[2], most are using either text messages or emails. So this change would not have a significant impact on these services because the page will not get any longer. In other words we wouldn’t be making it worse for them. Those services who do use both are not using as many templates. The worst-case scenario is SSCS, who have 16 templates, evenly split between email and text messages. So they would go from having 8 templates per page to 16, which is still less than half the number that HMPO or Digital Marketplace are managing. References --- 1. https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two 2. Template usage by service Service name | Template count | Template types ---------------------------------------|----------------|--------------- Her Majesty's Passport Office | 40 | sms Digital Marketplace | 40 | email GovWifi-Staging | 19 | sms GovWifi | 18 | sms Digital Apprenticeship Service | 16 | email SSCS | 16 | both Crown Commercial Service MI Collection | 15 | email Help with Prison Visits | 12 | both Digital Future | 12 | email Export Licensing Service | 11 | email Civil Money Claims | 9 | both DVLA Drivers Medical Service | 9 | sms GOV.UK Notify | 8 | both Manage your benefit overpayments | 8 | both Tax Renewals | 8 | both
2017-02-28 12:16:35 +00:00
class RequiredDateFilterForm(StripWhitespaceForm):
start_date = DateField("Start Date")
end_date = DateField("End Date")
class SearchByNameForm(StripWhitespaceForm):
search = SearchField(
'Search by name',
validators=[DataRequired("You need to enter full or partial name to search by.")],
)
Show one field at a time on send yourself a test The send yourself a test feature is useful for two things: - constructing an email/text message/letter without uploading a CSV file - seeing what the thing your going to send will look like (either by getting it in your inbox or downloading the PDF) - learning the concept of placeholders, ie understanding they’re thing that gets populated with _stuff_ The problem we’re seeing is that the current UI breaks when a template has a lot of placeholders. This is especially apparent with letter templates, which have a minimum of 7 placeholders by virtue of the address. The idea behind having the form fields side-by-side was to help people understand the relationship between their spreadsheet columns and the placeholders. But this means that the page was doing a lot of work, trying to teach: - replacement of placeholders - link between placeholders and spreadsheet columns The latter is better explained by the example spreadsheet shown on the upload page. So it can safely be removed from the send yourself a test page – in other words the fields don’t need to be shown side by side. Showing them one-at-a-time works well because: - it’s really obvious, even on first use, what the page is asking you to do - as your step through each placeholder, you see the message build up with the data you’ve entered – you’re learning how replacement of placeholders works by repetition This also means adding a matching endpoint for viewing each step of making the test letter as a PDF/PNG because we can’t reuse the view of the template without any placeholders filled any more.
2017-05-04 09:30:55 +01:00
class SearchUsersByEmailForm(StripWhitespaceForm):
search = SearchField(
'Search by name or email address',
validators=[
DataRequired("You need to enter full or partial email address to search by.")
],
)
class SearchUsersForm(StripWhitespaceForm):
search = SearchField('Search by name or email address')
class SearchNotificationsForm(StripWhitespaceForm):
to = SearchField()
labels = {
'email': 'Search by email address',
'sms': 'Search by phone number',
}
def __init__(self, message_type, *args, **kwargs):
super().__init__(*args, **kwargs)
self.to.label.text = self.labels.get(
message_type,
'Search by phone number or email address',
)
class PlaceholderForm(StripWhitespaceForm):
Show one field at a time on send yourself a test The send yourself a test feature is useful for two things: - constructing an email/text message/letter without uploading a CSV file - seeing what the thing your going to send will look like (either by getting it in your inbox or downloading the PDF) - learning the concept of placeholders, ie understanding they’re thing that gets populated with _stuff_ The problem we’re seeing is that the current UI breaks when a template has a lot of placeholders. This is especially apparent with letter templates, which have a minimum of 7 placeholders by virtue of the address. The idea behind having the form fields side-by-side was to help people understand the relationship between their spreadsheet columns and the placeholders. But this means that the page was doing a lot of work, trying to teach: - replacement of placeholders - link between placeholders and spreadsheet columns The latter is better explained by the example spreadsheet shown on the upload page. So it can safely be removed from the send yourself a test page – in other words the fields don’t need to be shown side by side. Showing them one-at-a-time works well because: - it’s really obvious, even on first use, what the page is asking you to do - as your step through each placeholder, you see the message build up with the data you’ve entered – you’re learning how replacement of placeholders works by repetition This also means adding a matching endpoint for viewing each step of making the test letter as a PDF/PNG because we can’t reuse the view of the template without any placeholders filled any more.
2017-05-04 09:30:55 +01:00
pass
class PasswordFieldShowHasContent(StringField):
widget = widgets.PasswordInput(hide_value=False)
class ServiceInboundNumberForm(StripWhitespaceForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.inbound_number.choices = kwargs['inbound_number_choices']
inbound_number = RadioField(
"Select your inbound number",
thing='an inbound number',
)
class CallbackForm(StripWhitespaceForm):
def validate(self):
return super().validate() or self.url.data == ''
class ServiceReceiveMessagesCallbackForm(CallbackForm):
2017-12-08 10:52:38 +00:00
url = StringField(
"URL",
2019-09-12 16:42:33 +01:00
validators=[DataRequired(message='Cannot be empty'),
Regexp(regex="^https.*", message='Must be a valid https URL')]
)
2017-12-08 10:52:38 +00:00
bearer_token = PasswordFieldShowHasContent(
"Bearer token",
2019-09-12 16:42:33 +01:00
validators=[DataRequired(message='Cannot be empty'),
Length(min=10, message='Must be at least 10 characters')]
)
2017-12-08 10:52:38 +00:00
class ServiceDeliveryStatusCallbackForm(CallbackForm):
2017-12-08 10:52:38 +00:00
url = StringField(
"URL",
2019-09-12 16:42:33 +01:00
validators=[DataRequired(message='Cannot be empty'),
2017-12-08 10:52:38 +00:00
Regexp(regex="^https.*", message='Must be a valid https URL')]
)
bearer_token = PasswordFieldShowHasContent(
"Bearer token",
2019-09-12 16:42:33 +01:00
validators=[DataRequired(message='Cannot be empty'),
2017-12-08 10:52:38 +00:00
Length(min=10, message='Must be at least 10 characters')]
)
class InternationalSMSForm(StripWhitespaceForm):
enabled = RadioField(
'Send text messages to international phone numbers',
choices=[
('on', 'On'),
('off', 'Off'),
],
)
class SMSPrefixForm(StripWhitespaceForm):
enabled = RadioField(
'',
choices=[
('on', 'On'),
('off', 'Off'),
],
)
Show one field at a time on send yourself a test The send yourself a test feature is useful for two things: - constructing an email/text message/letter without uploading a CSV file - seeing what the thing your going to send will look like (either by getting it in your inbox or downloading the PDF) - learning the concept of placeholders, ie understanding they’re thing that gets populated with _stuff_ The problem we’re seeing is that the current UI breaks when a template has a lot of placeholders. This is especially apparent with letter templates, which have a minimum of 7 placeholders by virtue of the address. The idea behind having the form fields side-by-side was to help people understand the relationship between their spreadsheet columns and the placeholders. But this means that the page was doing a lot of work, trying to teach: - replacement of placeholders - link between placeholders and spreadsheet columns The latter is better explained by the example spreadsheet shown on the upload page. So it can safely be removed from the send yourself a test page – in other words the fields don’t need to be shown side by side. Showing them one-at-a-time works well because: - it’s really obvious, even on first use, what the page is asking you to do - as your step through each placeholder, you see the message build up with the data you’ve entered – you’re learning how replacement of placeholders works by repetition This also means adding a matching endpoint for viewing each step of making the test letter as a PDF/PNG because we can’t reuse the view of the template without any placeholders filled any more.
2017-05-04 09:30:55 +01:00
def get_placeholder_form_instance(
placeholder_name,
dict_to_populate_from,
template_type,
allow_international_phone_numbers=False,
Show one field at a time on send yourself a test The send yourself a test feature is useful for two things: - constructing an email/text message/letter without uploading a CSV file - seeing what the thing your going to send will look like (either by getting it in your inbox or downloading the PDF) - learning the concept of placeholders, ie understanding they’re thing that gets populated with _stuff_ The problem we’re seeing is that the current UI breaks when a template has a lot of placeholders. This is especially apparent with letter templates, which have a minimum of 7 placeholders by virtue of the address. The idea behind having the form fields side-by-side was to help people understand the relationship between their spreadsheet columns and the placeholders. But this means that the page was doing a lot of work, trying to teach: - replacement of placeholders - link between placeholders and spreadsheet columns The latter is better explained by the example spreadsheet shown on the upload page. So it can safely be removed from the send yourself a test page – in other words the fields don’t need to be shown side by side. Showing them one-at-a-time works well because: - it’s really obvious, even on first use, what the page is asking you to do - as your step through each placeholder, you see the message build up with the data you’ve entered – you’re learning how replacement of placeholders works by repetition This also means adding a matching endpoint for viewing each step of making the test letter as a PDF/PNG because we can’t reuse the view of the template without any placeholders filled any more.
2017-05-04 09:30:55 +01:00
):
if (
Columns.make_key(placeholder_name) == 'emailaddress' and
template_type == 'email'
):
field = email_address(label=placeholder_name, gov_user=False)
elif (
Columns.make_key(placeholder_name) == 'phonenumber' and
template_type == 'sms'
):
if allow_international_phone_numbers:
field = international_phone_number(label=placeholder_name)
else:
field = uk_mobile_number(label=placeholder_name)
else:
field = StringField(placeholder_name, validators=[
2019-09-12 16:42:33 +01:00
DataRequired(message='Cannot be empty')
])
PlaceholderForm.placeholder_value = field
Show one field at a time on send yourself a test The send yourself a test feature is useful for two things: - constructing an email/text message/letter without uploading a CSV file - seeing what the thing your going to send will look like (either by getting it in your inbox or downloading the PDF) - learning the concept of placeholders, ie understanding they’re thing that gets populated with _stuff_ The problem we’re seeing is that the current UI breaks when a template has a lot of placeholders. This is especially apparent with letter templates, which have a minimum of 7 placeholders by virtue of the address. The idea behind having the form fields side-by-side was to help people understand the relationship between their spreadsheet columns and the placeholders. But this means that the page was doing a lot of work, trying to teach: - replacement of placeholders - link between placeholders and spreadsheet columns The latter is better explained by the example spreadsheet shown on the upload page. So it can safely be removed from the send yourself a test page – in other words the fields don’t need to be shown side by side. Showing them one-at-a-time works well because: - it’s really obvious, even on first use, what the page is asking you to do - as your step through each placeholder, you see the message build up with the data you’ve entered – you’re learning how replacement of placeholders works by repetition This also means adding a matching endpoint for viewing each step of making the test letter as a PDF/PNG because we can’t reuse the view of the template without any placeholders filled any more.
2017-05-04 09:30:55 +01:00
return PlaceholderForm(
placeholder_value=dict_to_populate_from.get(placeholder_name, '')
)
class SetSenderForm(StripWhitespaceForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.sender.choices = kwargs['sender_choices']
self.sender.label.text = kwargs['sender_label']
sender = RadioField()
class SetTemplateSenderForm(StripWhitespaceForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.sender.choices = kwargs['sender_choices']
self.sender.label.text = 'Select your sender'
sender = RadioField()
2018-02-12 09:26:13 +00:00
class LinkOrganisationsForm(StripWhitespaceForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.organisations.choices = kwargs['choices']
organisations = RadioField(
'Select an organisation',
validators=[
DataRequired()
]
)
class BrandingOptions(StripWhitespaceForm):
FALLBACK_OPTION_VALUE = 'something_else'
FALLBACK_OPTION = (FALLBACK_OPTION_VALUE, 'Something else')
options = RadioField('Choose your new branding')
something_else = TextAreaField('Describe the branding you want')
def __init__(self, service, *args, branding_type="email", **kwargs):
super().__init__(*args, **kwargs)
self.options.choices = tuple(self.get_available_choices(service, branding_type))
self.options.label.text = 'Choose your new {} branding'.format(branding_type)
if self.something_else_is_only_option:
self.options.data = self.FALLBACK_OPTION_VALUE
@staticmethod
def get_available_choices(service, branding_type):
if branding_type == "email":
organisation_branding_id = service.organisation.email_branding_id if service.organisation else None
service_branding_id = service.email_branding_id
service_branding_name = service.email_branding_name
elif branding_type == "letter":
organisation_branding_id = service.organisation.letter_branding_id if service.organisation else None
service_branding_id = service.letter_branding_id
service_branding_name = service.letter_branding_name
if (
service.organisation_type == Organisation.TYPE_CENTRAL
and organisation_branding_id is None
and service_branding_id is not None
and branding_type == "email"
):
yield ('govuk', 'GOV.UK')
if (
service.organisation_type == Organisation.TYPE_CENTRAL
and service.organisation
and organisation_branding_id is None
and service_branding_name.lower() != 'GOV.UK and {}'.format(service.organisation.name).lower()
and branding_type == "email"
):
yield ('govuk_and_org', 'GOV.UK and {}'.format(service.organisation.name))
if (
service.organisation_type in {
Organisation.TYPE_NHS_CENTRAL,
Organisation.TYPE_NHS_LOCAL,
Organisation.TYPE_NHS_GP,
}
and service_branding_name != 'NHS'
):
yield ('nhs', 'NHS')
if (
service.organisation
and service.organisation_type not in {
Organisation.TYPE_NHS_LOCAL,
Organisation.TYPE_NHS_CENTRAL,
Organisation.TYPE_NHS_GP,
}
and (
service_branding_id is None
or service_branding_id != organisation_branding_id
)
):
yield ('organisation', service.organisation.name)
yield BrandingOptions.FALLBACK_OPTION
@property
def something_else_is_only_option(self):
return self.options.choices == (self.FALLBACK_OPTION,)
def validate_something_else(self, field):
if (
self.something_else_is_only_option
or self.options.data == self.FALLBACK_OPTION_VALUE
) and not field.data:
raise ValidationError('Cannot be empty')
if self.options.data != self.FALLBACK_OPTION_VALUE:
field.data = ''
class ServiceDataRetentionForm(StripWhitespaceForm):
notification_type = RadioField(
'What notification type?',
choices=[
('email', 'Email'),
('sms', 'SMS'),
('letter', 'Letter'),
],
validators=[DataRequired()],
)
days_of_retention = IntegerField(label="Days of retention",
validators=[validators.NumberRange(min=3, max=90,
message="Must be between 3 and 90")],
)
class ServiceDataRetentionEditForm(StripWhitespaceForm):
days_of_retention = IntegerField(label="Days of retention",
validators=[validators.NumberRange(min=3, max=90,
message="Must be between 3 and 90")],
)
class ReturnedLettersForm(StripWhitespaceForm):
references = TextAreaField(
u'Letter references',
validators=[
2019-09-12 16:42:33 +01:00
DataRequired(message="Cannot be empty"),
]
)
2018-11-01 16:02:43 +00:00
class TemplateFolderForm(StripWhitespaceForm):
def __init__(self, all_service_users=None, *args, **kwargs):
super().__init__(*args, **kwargs)
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
]
2020-04-09 20:56:17 +01:00
users_with_permission = govukCollapsibleCheckboxesField(
'Team members who can see this folder',
field_label='folder')
2019-09-12 16:42:33 +01:00
name = StringField('Folder name', validators=[DataRequired(message='Cannot be empty')])
def required_for_ops(*operations):
operations = set(operations)
def validate(form, field):
if form.op not in operations and any(field.raw_data):
# super weird
raise validators.StopValidation('Must be empty')
if form.op in operations and not any(field.raw_data):
2019-09-12 16:42:33 +01:00
raise validators.StopValidation('Cannot be empty')
return validate
class TemplateAndFoldersSelectionForm(Form):
"""
This form expects the form data to include an operation, based on which submit button is clicked.
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.
* move-to-existing-folder
must have data for templates_and_folders checkboxes, and move_to radios
* move-to-new-folder
must have data for move_to_new_folder_name, cannot have data for move_to_existing_folder_name
* add-new-folder
must have data for move_to_existing_folder_name, cannot have data for move_to_new_folder_name
"""
ALL_TEMPLATES_FOLDER = {
2019-01-04 13:34:15 +00:00
'name': 'Templates',
'id': RadioFieldWithNoneOption.NONE_OPTION_VALUE,
}
def __init__(
self,
all_template_folders,
template_list,
available_template_types,
allow_adding_copy_of_template,
*args,
**kwargs
):
super().__init__(*args, **kwargs)
self.available_template_types = available_template_types
self.templates_and_folders.choices = template_list.as_id_and_name
self.op = None
self.is_move_op = self.is_add_folder_op = self.is_add_template_op = False
self.move_to.all_template_folders = all_template_folders
self.move_to.choices = [
(item['id'], item['name'])
for item in ([self.ALL_TEMPLATES_FOLDER] + all_template_folders)
]
self.add_template_by_template_type.choices = list(filter(None, [
# We want to show email and text message to everyone,
# whether or not the service has them switched on. The
# option to add letter or broadcast templates should only
# be shown to services which have that permission
('email', 'Email'),
('sms', 'Text message'),
('letter', 'Letter') if 'letter' in available_template_types else None,
('broadcast', 'Broadcast') if 'broadcast' in available_template_types else None,
('copy-existing', 'Copy an existing template') if allow_adding_copy_of_template else None,
]))
@property
def trying_to_add_unavailable_template_type(self):
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,
))
def is_selected(self, template_folder_id):
return template_folder_id in (self.templates_and_folders.data or [])
def validate(self):
self.op = request.form.get('operation')
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'}
if not (self.is_add_folder_op or self.is_move_op or self.is_add_template_op):
return False
return super().validate()
def get_folder_name(self):
if self.op == 'add-new-folder':
return self.add_new_folder_name.data
elif self.op == 'move-to-new-folder':
return self.move_to_new_folder_name.data
return None
templates_and_folders = govukCheckboxesField(
'Choose templates or folders',
validators=[required_for_ops('move-to-new-folder', 'move-to-existing-folder')],
choices=[], # added to keep order of arguments, added properly in __init__
param_extensions={
"fieldset": {
"legend": {
"classes": "govuk-visually-hidden"
}
}
}
)
# 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
move_to = NestedRadioField(
'Choose a folder',
default='',
validators=[
required_for_ops('move-to-existing-folder'),
Optional()
])
add_new_folder_name = StringField('Folder name', validators=[required_for_ops('add-new-folder')])
move_to_new_folder_name = StringField('Folder name', validators=[required_for_ops('move-to-new-folder')])
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')
class ClearCacheForm(StripWhitespaceForm):
model_type = RadioField(
'What do you want to clear today',
)
class GoLiveNotesForm(StripWhitespaceForm):
request_to_go_live_notes = TextAreaField(
'Go live notes',
filters=[lambda x: x or None],
)
Add pages to let users accept the agreement online At the moment, the process for accepting the data sharing and financial agreement is: 1. download a pdf * print it out * get someone to sign it * scan it * email it back to us * we rename the file and save it in Google Drive * we then update the organisation to say the MOU is signed * sometimes we also: * print it out and get it counter-signed * scan it again * email it back to the service Let's not do that any more. When the first service for an organisation that doesn't have the agreement in place is in the process of going live, then they should be able to accept the agreement online as part of the go live flow. This commit adds the pages that let someone do that. Where the checklist shows the agreement as **[not completed]** then they can follow a link where they can download it (as happens now). From here, they should then also be able to provide some info to accept it. The info that we need is: **Version** – because we version the agreements occasionally, we need to know which version they are accepting. It may not be the latest one if they downloaded it a while ago and it took time to be signed off **Who is accepting the agreement** – this will often be someone in the finance team, and not necessarily a team member, so we should let the person either accept as themselves, or on behalf of someone else. If it's on behalf of someone else we need to the name and email address of that person so we have that on record. Obvs if it's them accepting it themselves, we have that already (so we just store their user ID and not their name or email address). We then replay the collected info back in a sort of legally binding kind of way pulling in the organisation name too. The wording we’re using is inspired by what GOV.UK Pay have. Then there’s a big green button they can click to accept the agreement, which stores their user ID and and timestamp.
2019-06-18 14:24:29 +01:00
class AcceptAgreementForm(StripWhitespaceForm):
@classmethod
def from_organisation(cls, org):
if org.agreement_signed_on_behalf_of_name and org.agreement_signed_on_behalf_of_email_address:
who = 'someone-else'
elif org.agreement_signed_version: # only set if user has submitted form previously
who = 'me'
else:
who = None
return cls(
version=org.agreement_signed_version,
who=who,
on_behalf_of_name=org.agreement_signed_on_behalf_of_name,
on_behalf_of_email=org.agreement_signed_on_behalf_of_email_address,
)
Add pages to let users accept the agreement online At the moment, the process for accepting the data sharing and financial agreement is: 1. download a pdf * print it out * get someone to sign it * scan it * email it back to us * we rename the file and save it in Google Drive * we then update the organisation to say the MOU is signed * sometimes we also: * print it out and get it counter-signed * scan it again * email it back to the service Let's not do that any more. When the first service for an organisation that doesn't have the agreement in place is in the process of going live, then they should be able to accept the agreement online as part of the go live flow. This commit adds the pages that let someone do that. Where the checklist shows the agreement as **[not completed]** then they can follow a link where they can download it (as happens now). From here, they should then also be able to provide some info to accept it. The info that we need is: **Version** – because we version the agreements occasionally, we need to know which version they are accepting. It may not be the latest one if they downloaded it a while ago and it took time to be signed off **Who is accepting the agreement** – this will often be someone in the finance team, and not necessarily a team member, so we should let the person either accept as themselves, or on behalf of someone else. If it's on behalf of someone else we need to the name and email address of that person so we have that on record. Obvs if it's them accepting it themselves, we have that already (so we just store their user ID and not their name or email address). We then replay the collected info back in a sort of legally binding kind of way pulling in the organisation name too. The wording we’re using is inspired by what GOV.UK Pay have. Then there’s a big green button they can click to accept the agreement, which stores their user ID and and timestamp.
2019-06-18 14:24:29 +01:00
version = StringField(
'Which version of the agreement do you want to accept?'
Add pages to let users accept the agreement online At the moment, the process for accepting the data sharing and financial agreement is: 1. download a pdf * print it out * get someone to sign it * scan it * email it back to us * we rename the file and save it in Google Drive * we then update the organisation to say the MOU is signed * sometimes we also: * print it out and get it counter-signed * scan it again * email it back to the service Let's not do that any more. When the first service for an organisation that doesn't have the agreement in place is in the process of going live, then they should be able to accept the agreement online as part of the go live flow. This commit adds the pages that let someone do that. Where the checklist shows the agreement as **[not completed]** then they can follow a link where they can download it (as happens now). From here, they should then also be able to provide some info to accept it. The info that we need is: **Version** – because we version the agreements occasionally, we need to know which version they are accepting. It may not be the latest one if they downloaded it a while ago and it took time to be signed off **Who is accepting the agreement** – this will often be someone in the finance team, and not necessarily a team member, so we should let the person either accept as themselves, or on behalf of someone else. If it's on behalf of someone else we need to the name and email address of that person so we have that on record. Obvs if it's them accepting it themselves, we have that already (so we just store their user ID and not their name or email address). We then replay the collected info back in a sort of legally binding kind of way pulling in the organisation name too. The wording we’re using is inspired by what GOV.UK Pay have. Then there’s a big green button they can click to accept the agreement, which stores their user ID and and timestamp.
2019-06-18 14:24:29 +01:00
)
who = RadioField(
2019-07-18 15:58:24 +01:00
'Who are you accepting the agreement for?',
Add pages to let users accept the agreement online At the moment, the process for accepting the data sharing and financial agreement is: 1. download a pdf * print it out * get someone to sign it * scan it * email it back to us * we rename the file and save it in Google Drive * we then update the organisation to say the MOU is signed * sometimes we also: * print it out and get it counter-signed * scan it again * email it back to the service Let's not do that any more. When the first service for an organisation that doesn't have the agreement in place is in the process of going live, then they should be able to accept the agreement online as part of the go live flow. This commit adds the pages that let someone do that. Where the checklist shows the agreement as **[not completed]** then they can follow a link where they can download it (as happens now). From here, they should then also be able to provide some info to accept it. The info that we need is: **Version** – because we version the agreements occasionally, we need to know which version they are accepting. It may not be the latest one if they downloaded it a while ago and it took time to be signed off **Who is accepting the agreement** – this will often be someone in the finance team, and not necessarily a team member, so we should let the person either accept as themselves, or on behalf of someone else. If it's on behalf of someone else we need to the name and email address of that person so we have that on record. Obvs if it's them accepting it themselves, we have that already (so we just store their user ID and not their name or email address). We then replay the collected info back in a sort of legally binding kind of way pulling in the organisation name too. The wording we’re using is inspired by what GOV.UK Pay have. Then there’s a big green button they can click to accept the agreement, which stores their user ID and and timestamp.
2019-06-18 14:24:29 +01:00
choices=(
(
'me',
2019-07-18 15:58:24 +01:00
'Yourself',
Add pages to let users accept the agreement online At the moment, the process for accepting the data sharing and financial agreement is: 1. download a pdf * print it out * get someone to sign it * scan it * email it back to us * we rename the file and save it in Google Drive * we then update the organisation to say the MOU is signed * sometimes we also: * print it out and get it counter-signed * scan it again * email it back to the service Let's not do that any more. When the first service for an organisation that doesn't have the agreement in place is in the process of going live, then they should be able to accept the agreement online as part of the go live flow. This commit adds the pages that let someone do that. Where the checklist shows the agreement as **[not completed]** then they can follow a link where they can download it (as happens now). From here, they should then also be able to provide some info to accept it. The info that we need is: **Version** – because we version the agreements occasionally, we need to know which version they are accepting. It may not be the latest one if they downloaded it a while ago and it took time to be signed off **Who is accepting the agreement** – this will often be someone in the finance team, and not necessarily a team member, so we should let the person either accept as themselves, or on behalf of someone else. If it's on behalf of someone else we need to the name and email address of that person so we have that on record. Obvs if it's them accepting it themselves, we have that already (so we just store their user ID and not their name or email address). We then replay the collected info back in a sort of legally binding kind of way pulling in the organisation name too. The wording we’re using is inspired by what GOV.UK Pay have. Then there’s a big green button they can click to accept the agreement, which stores their user ID and and timestamp.
2019-06-18 14:24:29 +01:00
),
(
'someone-else',
2019-07-18 15:58:24 +01:00
'Someone else',
Add pages to let users accept the agreement online At the moment, the process for accepting the data sharing and financial agreement is: 1. download a pdf * print it out * get someone to sign it * scan it * email it back to us * we rename the file and save it in Google Drive * we then update the organisation to say the MOU is signed * sometimes we also: * print it out and get it counter-signed * scan it again * email it back to the service Let's not do that any more. When the first service for an organisation that doesn't have the agreement in place is in the process of going live, then they should be able to accept the agreement online as part of the go live flow. This commit adds the pages that let someone do that. Where the checklist shows the agreement as **[not completed]** then they can follow a link where they can download it (as happens now). From here, they should then also be able to provide some info to accept it. The info that we need is: **Version** – because we version the agreements occasionally, we need to know which version they are accepting. It may not be the latest one if they downloaded it a while ago and it took time to be signed off **Who is accepting the agreement** – this will often be someone in the finance team, and not necessarily a team member, so we should let the person either accept as themselves, or on behalf of someone else. If it's on behalf of someone else we need to the name and email address of that person so we have that on record. Obvs if it's them accepting it themselves, we have that already (so we just store their user ID and not their name or email address). We then replay the collected info back in a sort of legally binding kind of way pulling in the organisation name too. The wording we’re using is inspired by what GOV.UK Pay have. Then there’s a big green button they can click to accept the agreement, which stores their user ID and and timestamp.
2019-06-18 14:24:29 +01:00
),
),
)
on_behalf_of_name = StringField(
'Whats their name?'
Add pages to let users accept the agreement online At the moment, the process for accepting the data sharing and financial agreement is: 1. download a pdf * print it out * get someone to sign it * scan it * email it back to us * we rename the file and save it in Google Drive * we then update the organisation to say the MOU is signed * sometimes we also: * print it out and get it counter-signed * scan it again * email it back to the service Let's not do that any more. When the first service for an organisation that doesn't have the agreement in place is in the process of going live, then they should be able to accept the agreement online as part of the go live flow. This commit adds the pages that let someone do that. Where the checklist shows the agreement as **[not completed]** then they can follow a link where they can download it (as happens now). From here, they should then also be able to provide some info to accept it. The info that we need is: **Version** – because we version the agreements occasionally, we need to know which version they are accepting. It may not be the latest one if they downloaded it a while ago and it took time to be signed off **Who is accepting the agreement** – this will often be someone in the finance team, and not necessarily a team member, so we should let the person either accept as themselves, or on behalf of someone else. If it's on behalf of someone else we need to the name and email address of that person so we have that on record. Obvs if it's them accepting it themselves, we have that already (so we just store their user ID and not their name or email address). We then replay the collected info back in a sort of legally binding kind of way pulling in the organisation name too. The wording we’re using is inspired by what GOV.UK Pay have. Then there’s a big green button they can click to accept the agreement, which stores their user ID and and timestamp.
2019-06-18 14:24:29 +01:00
)
on_behalf_of_email = email_address(
'Whats their email address?',
required=False,
gov_user=False,
)
def __validate_if_nominating(self, field):
if self.who.data == 'someone-else':
if not field.data:
2019-09-12 16:42:33 +01:00
raise ValidationError('Cannot be empty')
else:
field.data = ''
validate_on_behalf_of_name = __validate_if_nominating
validate_on_behalf_of_email = __validate_if_nominating
Add pages to let users accept the agreement online At the moment, the process for accepting the data sharing and financial agreement is: 1. download a pdf * print it out * get someone to sign it * scan it * email it back to us * we rename the file and save it in Google Drive * we then update the organisation to say the MOU is signed * sometimes we also: * print it out and get it counter-signed * scan it again * email it back to the service Let's not do that any more. When the first service for an organisation that doesn't have the agreement in place is in the process of going live, then they should be able to accept the agreement online as part of the go live flow. This commit adds the pages that let someone do that. Where the checklist shows the agreement as **[not completed]** then they can follow a link where they can download it (as happens now). From here, they should then also be able to provide some info to accept it. The info that we need is: **Version** – because we version the agreements occasionally, we need to know which version they are accepting. It may not be the latest one if they downloaded it a while ago and it took time to be signed off **Who is accepting the agreement** – this will often be someone in the finance team, and not necessarily a team member, so we should let the person either accept as themselves, or on behalf of someone else. If it's on behalf of someone else we need to the name and email address of that person so we have that on record. Obvs if it's them accepting it themselves, we have that already (so we just store their user ID and not their name or email address). We then replay the collected info back in a sort of legally binding kind of way pulling in the organisation name too. The wording we’re using is inspired by what GOV.UK Pay have. Then there’s a big green button they can click to accept the agreement, which stores their user ID and and timestamp.
2019-06-18 14:24:29 +01:00
def validate_version(self, field):
try:
float(field.data)
except (TypeError, ValueError):
raise ValidationError("Must be a number")
class BroadcastAreaForm(StripWhitespaceForm):
areas = govukCheckboxesField('Choose areas to broadcast to')
def __init__(self, choices, *args, **kwargs):
super().__init__(*args, **kwargs)
self.areas.choices = choices
self.areas.render_as_list = True
self.areas.param_extensions = {'fieldset': {'legend': {'classes': 'govuk-visually-hidden'}}}
@classmethod
def from_library(cls, library):
return cls(choices=[
(area.id, area.name) for area in sorted(library)
])
class BroadcastAreaFormWithSelectAll(BroadcastAreaForm):
select_all = govukCheckboxField('Select all')
@classmethod
def from_library(cls, library, select_all_choice):
instance = super().from_library(library)
(
instance.select_all.area_slug,
instance.select_all.label.text,
) = select_all_choice
return instance
@property
def selected_areas(self):
if self.select_all.data:
return [self.select_all.area_slug]
return self.areas.data