Handle multi-column recipients

Implements https://github.com/alphagov/notifications-utils/pull/81

Handles addresses as multiple columns:
- in ‘Send yourself a test’
- in example CSV files
- in validating that a CSV file has recipients (eg at least an ‘address
  line 1’ and ‘postcode’ column)
- when showing the contents of a CSV file

As few UI changes as possible, once we have the thing working end-to-end
we can think about how the UI might need to work differently.
This commit is contained in:
Chris Hill-Scott
2016-11-10 13:39:05 +00:00
parent 5772fd34fe
commit 69ad5fe156
6 changed files with 50 additions and 30 deletions

View File

@@ -17,8 +17,9 @@ from flask import (
)
from flask_login import login_required, current_user
from notifications_utils.columns import Columns
from notifications_utils.template import Template
from notifications_utils.recipients import RecipientCSV, first_column_heading, validate_and_format_phone_number
from notifications_utils.recipients import RecipientCSV, first_column_headings, validate_and_format_phone_number
from app.main import main
from app.main.forms import CsvUploadForm, ChooseTimeForm, get_next_days_until, get_furthest_possible_scheduled_time
@@ -48,15 +49,16 @@ def get_example_csv_fields(column_headers, use_example_as_example, submitted_fie
def get_example_csv_rows(template, use_example_as_example=True, submitted_fields=False):
return [
{
'email': 'test@example.com' if use_example_as_example else current_user.email_address,
'sms': '07700 900321' if use_example_as_example else validate_and_format_phone_number(
current_user.mobile_number, human_readable=True
),
'letter': current_user.name
}[template.template_type]
] + get_example_csv_fields(template.placeholders, use_example_as_example, submitted_fields)
return {
'email': ['test@example.com'] if use_example_as_example else [current_user.email_address],
'sms': ['07700 900321'] if use_example_as_example else [validate_and_format_phone_number(
current_user.mobile_number, human_readable=True
)],
'letter': [current_user.name] + [
(submitted_fields or {}).get(key, 'example' if use_example_as_example else key)
for key in first_column_headings['letter'][1:]
]
}[template.template_type] + get_example_csv_fields(template.placeholders, use_example_as_example, submitted_fields)
@main.route("/services/<service_id>/send/<template_type>", methods=['GET'])
@@ -118,14 +120,13 @@ def send_messages(service_id, template_id):
form.file.data.filename
))
column_headings = first_column_headings[template.template_type] + list(template.placeholders)
return render_template(
'views/send.html',
template=template,
column_headings=list(ascii_uppercase[:len(template.placeholders) + 1]),
example=[
[first_column_heading[template.template_type]] + list(template.placeholders),
get_example_csv_rows(template)
],
column_headings=list(ascii_uppercase[:len(column_headings)]),
example=[column_headings, get_example_csv_rows(template)],
form=form
)
@@ -136,7 +137,7 @@ def send_messages(service_id, template_id):
def get_example_csv(service_id, template_id):
template = Template(service_api_client.get_service_template(service_id, template_id)['data'])
return Spreadsheet.from_rows([
[first_column_heading[template.template_type]] + list(template.placeholders),
first_column_headings[template.template_type] + list(template.placeholders),
get_example_csv_rows(template)
]).as_csv_data, 200, {
'Content-Type': 'text/csv; charset=utf-8',
@@ -163,7 +164,7 @@ def send_test(service_id, template_id):
{
'file_name': file_name,
'data': Spreadsheet.from_rows([
[first_column_heading[template.template_type]] + list(template.placeholders),
first_column_headings[template.template_type] + list(template.placeholders),
get_example_csv_rows(template, use_example_as_example=False, submitted_fields=request.form)
]).as_csv_data
},
@@ -185,7 +186,7 @@ def send_test(service_id, template_id):
return render_template(
'views/send-test.html',
template=template,
recipient_column=first_column_heading[template.template_type],
recipient_columns=first_column_headings[template.template_type],
example=[get_example_csv_rows(template, use_example_as_example=False)],
help=get_help_argument()
)
@@ -259,7 +260,10 @@ def check_messages(service_id, template_type, upload_id):
with suppress(StopIteration):
template.values = next(recipients.rows)
first_recipient = template.values.get(recipients.recipient_column_header, '')
first_recipient = template.values.get(
Columns.make_key(recipients.recipient_column_headers[0]),
''
)
session['upload_data']['notification_count'] = len(list(recipients.rows))
session['upload_data']['valid'] = not recipients.has_errors

View File

@@ -6,7 +6,7 @@ from flask_login import login_required
from dateutil.parser import parse
from notifications_utils.template import Template
from notifications_utils.recipients import first_column_heading
from notifications_utils.recipients import first_column_headings
from notifications_python_client.errors import HTTPError
from app.main import main
@@ -136,7 +136,7 @@ def edit_service_template(service_id, template_id):
new_template=new_template,
column_headings=list(ascii_uppercase[:len(new_template.placeholders) + 1]),
example_rows=[
[first_column_heading[new_template.template_type]] + list(new_template.placeholders),
first_column_headings[new_template.template_type] + list(new_template.placeholders),
get_example_csv_rows(new_template),
get_example_csv_rows(new_template)
],

View File

@@ -15,10 +15,24 @@
You can still send {{ remaining_messages }} messages today, but
{% endif %}
{{ original_file_name }} contains
{{ count_of_recipients }} {{ recipients.recipient_column_header }}
{%- if count_of_recipients != 1 -%}
{{ 'es' if 'email address' == recipients.recipient_column_header else 's' }}
{%- endif %}.
{{ count_of_recipients }}
{% if count_of_recipients == 1 -%}
{%- if template.template_type == 'email' -%}
email address
{%- elif template.template_type == 'sms' -%}
phone number
{%- elif template.template_type == 'letter' -%}
address
{%- endif -%}
{%- else -%}
{%- if template.template_type == 'email' -%}
email addresses
{%- elif template.template_type == 'sms' -%}
phone numbers
{%- elif template.template_type == 'letter' -%}
addresses
{%- endif -%}
{%- endif -%}.
</p>
{% endcall %}
</div>

View File

@@ -31,7 +31,7 @@
{% endcall %}
</div>
{% elif not recipients.has_recipient_column %}
{% elif not recipients.has_recipient_columns %}
<div class="bottom-gutter">
{% call banner_wrapper(type='dangerous') %}
@@ -193,7 +193,9 @@
{{ item['columns'][column].data if item['columns'][column].data != None }}
{% endcall %}
{% else %}
{{ text_field(item['columns'][column].data) }}
{% call field() %}
{{ item['columns'][column].data if item['columns'][column].data != None }}
{% endcall %}
{% endif %}
{% endfor %}
{% if item['columns'].get(None) %}

View File

@@ -46,8 +46,8 @@
<form method="post">
{% call(item, row_number) list_table(
example,
caption="Fill in the {}".format('field' if template.placeholders|length == 1 else 'fields'),
field_headings=[recipient_column] + template.placeholders|list
caption="Fill in the {}".format('field' if (recipient_columns + template.placeholders|list)|length == 2 else 'fields'),
field_headings=recipient_columns + template.placeholders|list
) %}
{% for column in item %}
{% call field() %}

View File

@@ -18,4 +18,4 @@ pytz==2016.4
git+https://github.com/alphagov/notifications-python-client.git@1.3.0#egg=notifications-python-client==1.3.0
git+https://github.com/alphagov/notifications-utils.git@9.3.0#egg=notifications-utils==9.3.0
git+https://github.com/alphagov/notifications-utils.git@10.0.0#egg=notifications-utils==10.0.0