diff --git a/app/main/views/send.py b/app/main/views/send.py index b240512ed..f263126d1 100644 --- a/app/main/views/send.py +++ b/app/main/views/send.py @@ -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//send/", 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 diff --git a/app/main/views/templates.py b/app/main/views/templates.py index 65886896a..a970a4b21 100644 --- a/app/main/views/templates.py +++ b/app/main/views/templates.py @@ -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) ], diff --git a/app/templates/partials/check/too-many-messages.html b/app/templates/partials/check/too-many-messages.html index b063640ee..761ca0801 100644 --- a/app/templates/partials/check/too-many-messages.html +++ b/app/templates/partials/check/too-many-messages.html @@ -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 -%}.

{% endcall %} diff --git a/app/templates/views/check.html b/app/templates/views/check.html index 3146d32e0..5c3410a8f 100644 --- a/app/templates/views/check.html +++ b/app/templates/views/check.html @@ -31,7 +31,7 @@ {% endcall %} - {% elif not recipients.has_recipient_column %} + {% elif not recipients.has_recipient_columns %}
{% 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) %} diff --git a/app/templates/views/send-test.html b/app/templates/views/send-test.html index 5a6c67643..0ce9d9a0c 100644 --- a/app/templates/views/send-test.html +++ b/app/templates/views/send-test.html @@ -46,8 +46,8 @@
{% 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() %} diff --git a/requirements.txt b/requirements.txt index 55958c75d..b85ce5dd4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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