From ddeed88210052644f77c3f138ca1574dd7349e19 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 20 Jul 2017 08:59:49 +0100 Subject: [PATCH] Refactor check page into 3 separate templates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The check page is a very complex Jinja template. It needs breaking up to make it easier to understand what’s going on. I think there are three di --- app/main/views/send.py | 14 +- .../{check.html => check/column-errors.html} | 0 app/templates/views/check/ok.html | 235 ++++++++++++++++++ app/templates/views/check/row-errors.html | 235 ++++++++++++++++++ 4 files changed, 480 insertions(+), 4 deletions(-) rename app/templates/views/{check.html => check/column-errors.html} (100%) create mode 100644 app/templates/views/check/ok.html create mode 100644 app/templates/views/check/row-errors.html diff --git a/app/main/views/send.py b/app/main/views/send.py index 8b72c3b9a..9cdd56419 100644 --- a/app/main/views/send.py +++ b/app/main/views/send.py @@ -447,10 +447,16 @@ def _check_messages(service_id, template_type, upload_id, letters_as_pdf=False): @login_required @user_has_permissions('send_texts', 'send_emails', 'send_letters') def check_messages(service_id, template_type, upload_id): - return render_template( - 'views/check.html', - **_check_messages(service_id, template_type, upload_id) - ) + + data = _check_messages(service_id, template_type, upload_id) + + if data['row_errors']: + return render_template('views/check/row-errors.html', **data) + + if data['errors']: + return render_template('views/check/column-errors.html', **data) + + return render_template('views/check/ok.html', **data) @main.route("/services///check/.", methods=['GET']) diff --git a/app/templates/views/check.html b/app/templates/views/check/column-errors.html similarity index 100% rename from app/templates/views/check.html rename to app/templates/views/check/column-errors.html diff --git a/app/templates/views/check/ok.html b/app/templates/views/check/ok.html new file mode 100644 index 000000000..ebb207edd --- /dev/null +++ b/app/templates/views/check/ok.html @@ -0,0 +1,235 @@ +{% extends "withnav_template.html" %} +{% from "components/banner.html" import banner_wrapper %} +{% from "components/radios.html" import radio_select %} +{% from "components/table.html" import list_table, field, text_field, index_field, hidden_field_heading %} +{% from "components/file-upload.html" import file_upload %} +{% from "components/page-footer.html" import page_footer %} +{% from "components/message-count-label.html" import message_count_label %} + +{% set file_contents_header_id = 'file-preview' %} +{% macro skip_to_file_contents() %} +

+ Skip to file contents +

+{% endmacro %} + +{% block service_page_title %} + {{ "Error" if errors else "Preview of {}".format(template.name) }} +{% endblock %} + +{% block maincolumn_content %} + + {% if recipients.too_many_rows %} + +
+ {% call banner_wrapper(type='dangerous') %} +

+ Your file has too many rows +

+

+ Notify can process up to + {{ "{:,}".format(recipients.max_rows) }} rows at once. Your + file has {{ "{:,}".format(recipients|length) }} rows. +

+ {{ skip_to_file_contents() }} + {% endcall %} +
+ + {% elif not count_of_recipients %} + +
+ {% call banner_wrapper(type='dangerous') %} +

+ Your file is missing some rows +

+

+ It needs at least one row of data, and {{ recipients.missing_column_headers | sort() | formatted_list( + prefix='a column called', + prefix_plural='columns called' + ) }}. +

+ {{ skip_to_file_contents() }} + {% endcall %} +
+ + {% elif not recipients.has_recipient_columns %} + +
+ {% call banner_wrapper(type='dangerous') %} +

+ Your file needs {{ recipients.recipient_column_headers | formatted_list( + prefix='a column called', + prefix_plural='columns called' + ) }} +

+

+ Right now it has {{ recipients.column_headers | formatted_list( + prefix='one column, called ', + prefix_plural='columns called ' + ) }}. +

+ {{ skip_to_file_contents() }} + {% endcall %} +
+ + {% elif recipients.missing_column_headers %} + +
+ {% call banner_wrapper(type='dangerous') %} +

+ The columns in your file need to match the double brackets in + your template +

+

+ Your file is missing {{ recipients.missing_column_headers | formatted_list( + conjunction='and', + prefix='a column called ', + prefix_plural='columns called ' + ) }}. +

+ {{ skip_to_file_contents() }} + {% endcall %} +
+ + {% elif row_errors %} + +
+ {% call banner_wrapper(type='dangerous') %} + {% if row_errors|length == 1 %} +

+ There is a problem with your data +

+

+ You need to {{ row_errors[0] }} +

+ {% else %} +

+ There are some problems with your data +

+

+ You need to: +

+
    + {% for error in row_errors %} +
  • {{ error }}
  • + {% endfor %} +
+ {% endif %} + {{ skip_to_file_contents() }} + {% endcall %} +
+ + {% elif not recipients.allowed_to_send_to %} + {% with + count_of_recipients=count_of_recipients, + template_type_label=recipients.recipient_column_headers[0] + %} + {% include "partials/check/not-allowed-to-send-to.html" %} + {% endwith %} + {% elif recipients.more_rows_than_can_send %} + {% include "partials/check/too-many-messages.html" %} + {% else %} + +

+ Preview of {{ template.name }} +

+ {{ skip_to_file_contents() }} + + {% endif %} + + {% if not errors %} + {{ template|string }} + {% endif %} + +
+ {% if errors %} + {% if request.args.from_test %} + Back + {% else %} + {{file_upload(form.file, button_text='Re-upload your file')}} + {% endif %} + {% else %} + + {% endif %} +
+ + {% if not request.args.from_test %} + +

{{ original_file_name }}

+ + {% call(item, row_number) list_table( + recipients.initial_annotated_rows_with_errors if row_errors and not recipients.missing_column_headers else recipients.initial_annotated_rows, + caption=original_file_name, + caption_visible=False, + field_headings=[ + 'Row in file'.format("table-field-invisible-error" if errors else "")|safe + ] + recipients.column_headers + ) %} + {% call index_field() %} + + {{ item.index + 2 }} + + {% endcall %} + {% for column in recipients.column_headers %} + {% if item['columns'][column].error and not recipients.missing_column_headers %} + {% call field() %} + + {{ item['columns'][column].error }} + {{ item['columns'][column].data if item['columns'][column].data != None }} + + {% endcall %} + {% elif item['columns'][column].ignore %} + {{ text_field(item['columns'][column].data or '', status='default') }} + {% else %} + {{ text_field(item['columns'][column].data or '') }} + {% endif %} + {% endfor %} + {% if item['columns'].get(None) %} + {% for column in item['columns'][None].data %} + {{ text_field(column, status='default') }} + {% endfor %} + {% endif %} + {% endcall %} + + {% endif %} + + + {% if recipients.too_many_rows %} + + {% elif count_of_displayed_recipients < count_of_recipients %} + + {% elif row_errors and not recipients.missing_column_headers %} + + {% endif %} + + {% if errors %} +

Preview of {{ template.name }}

+ {{ template|string }} + {% endif %} + +{% endblock %} diff --git a/app/templates/views/check/row-errors.html b/app/templates/views/check/row-errors.html new file mode 100644 index 000000000..ebb207edd --- /dev/null +++ b/app/templates/views/check/row-errors.html @@ -0,0 +1,235 @@ +{% extends "withnav_template.html" %} +{% from "components/banner.html" import banner_wrapper %} +{% from "components/radios.html" import radio_select %} +{% from "components/table.html" import list_table, field, text_field, index_field, hidden_field_heading %} +{% from "components/file-upload.html" import file_upload %} +{% from "components/page-footer.html" import page_footer %} +{% from "components/message-count-label.html" import message_count_label %} + +{% set file_contents_header_id = 'file-preview' %} +{% macro skip_to_file_contents() %} +

+ Skip to file contents +

+{% endmacro %} + +{% block service_page_title %} + {{ "Error" if errors else "Preview of {}".format(template.name) }} +{% endblock %} + +{% block maincolumn_content %} + + {% if recipients.too_many_rows %} + +
+ {% call banner_wrapper(type='dangerous') %} +

+ Your file has too many rows +

+

+ Notify can process up to + {{ "{:,}".format(recipients.max_rows) }} rows at once. Your + file has {{ "{:,}".format(recipients|length) }} rows. +

+ {{ skip_to_file_contents() }} + {% endcall %} +
+ + {% elif not count_of_recipients %} + +
+ {% call banner_wrapper(type='dangerous') %} +

+ Your file is missing some rows +

+

+ It needs at least one row of data, and {{ recipients.missing_column_headers | sort() | formatted_list( + prefix='a column called', + prefix_plural='columns called' + ) }}. +

+ {{ skip_to_file_contents() }} + {% endcall %} +
+ + {% elif not recipients.has_recipient_columns %} + +
+ {% call banner_wrapper(type='dangerous') %} +

+ Your file needs {{ recipients.recipient_column_headers | formatted_list( + prefix='a column called', + prefix_plural='columns called' + ) }} +

+

+ Right now it has {{ recipients.column_headers | formatted_list( + prefix='one column, called ', + prefix_plural='columns called ' + ) }}. +

+ {{ skip_to_file_contents() }} + {% endcall %} +
+ + {% elif recipients.missing_column_headers %} + +
+ {% call banner_wrapper(type='dangerous') %} +

+ The columns in your file need to match the double brackets in + your template +

+

+ Your file is missing {{ recipients.missing_column_headers | formatted_list( + conjunction='and', + prefix='a column called ', + prefix_plural='columns called ' + ) }}. +

+ {{ skip_to_file_contents() }} + {% endcall %} +
+ + {% elif row_errors %} + +
+ {% call banner_wrapper(type='dangerous') %} + {% if row_errors|length == 1 %} +

+ There is a problem with your data +

+

+ You need to {{ row_errors[0] }} +

+ {% else %} +

+ There are some problems with your data +

+

+ You need to: +

+
    + {% for error in row_errors %} +
  • {{ error }}
  • + {% endfor %} +
+ {% endif %} + {{ skip_to_file_contents() }} + {% endcall %} +
+ + {% elif not recipients.allowed_to_send_to %} + {% with + count_of_recipients=count_of_recipients, + template_type_label=recipients.recipient_column_headers[0] + %} + {% include "partials/check/not-allowed-to-send-to.html" %} + {% endwith %} + {% elif recipients.more_rows_than_can_send %} + {% include "partials/check/too-many-messages.html" %} + {% else %} + +

+ Preview of {{ template.name }} +

+ {{ skip_to_file_contents() }} + + {% endif %} + + {% if not errors %} + {{ template|string }} + {% endif %} + +
+ {% if errors %} + {% if request.args.from_test %} + Back + {% else %} + {{file_upload(form.file, button_text='Re-upload your file')}} + {% endif %} + {% else %} + + {% endif %} +
+ + {% if not request.args.from_test %} + +

{{ original_file_name }}

+ + {% call(item, row_number) list_table( + recipients.initial_annotated_rows_with_errors if row_errors and not recipients.missing_column_headers else recipients.initial_annotated_rows, + caption=original_file_name, + caption_visible=False, + field_headings=[ + 'Row in file'.format("table-field-invisible-error" if errors else "")|safe + ] + recipients.column_headers + ) %} + {% call index_field() %} + + {{ item.index + 2 }} + + {% endcall %} + {% for column in recipients.column_headers %} + {% if item['columns'][column].error and not recipients.missing_column_headers %} + {% call field() %} + + {{ item['columns'][column].error }} + {{ item['columns'][column].data if item['columns'][column].data != None }} + + {% endcall %} + {% elif item['columns'][column].ignore %} + {{ text_field(item['columns'][column].data or '', status='default') }} + {% else %} + {{ text_field(item['columns'][column].data or '') }} + {% endif %} + {% endfor %} + {% if item['columns'].get(None) %} + {% for column in item['columns'][None].data %} + {{ text_field(column, status='default') }} + {% endfor %} + {% endif %} + {% endcall %} + + {% endif %} + + + {% if recipients.too_many_rows %} + + {% elif count_of_displayed_recipients < count_of_recipients %} + + {% elif row_errors and not recipients.missing_column_headers %} + + {% endif %} + + {% if errors %} +

Preview of {{ template.name }}

+ {{ template|string }} + {% endif %} + +{% endblock %}