Merge pull request #3135 from alphagov/enforce_letter_10_page_limit

Enforce letter 10 page limit
This commit is contained in:
Pea (Malgorzata Tyczynska)
2019-10-16 13:14:30 +01:00
committed by GitHub
12 changed files with 188 additions and 25 deletions

View File

@@ -15,8 +15,9 @@ from flask import (
)
from flask_login import current_user
from notifications_python_client.errors import HTTPError
from notifications_utils import SMS_CHAR_COUNT_LIMIT
from notifications_utils import LETTER_MAX_PAGE_COUNT, SMS_CHAR_COUNT_LIMIT
from notifications_utils.columns import Columns
from notifications_utils.pdf import is_letter_too_long
from notifications_utils.recipients import (
RecipientCSV,
first_column_headings,
@@ -522,6 +523,7 @@ def _check_messages(service_id, template_id, upload_id, preview_row, letters_as_
email_reply_to = get_email_reply_to_address_from_session()
elif db_template['template_type'] == 'sms':
sms_sender = get_sms_sender_from_session()
template = get_template(
db_template,
current_service,
@@ -567,6 +569,8 @@ def _check_messages(service_id, template_id, upload_id, preview_row, letters_as_
elif preview_row > 2:
abort(404)
page_count = get_page_count_for_letter(db_template, template.values)
return dict(
recipients=recipients,
template=template,
@@ -589,7 +593,10 @@ def _check_messages(service_id, template_id, upload_id, preview_row, letters_as_
preview_row=preview_row,
sent_previously=job_api_client.has_sent_previously(
service_id, template.id, db_template['version'], request.args.get('original_file_name', '')
)
),
letter_too_long=is_letter_too_long(page_count),
letter_max_pages=LETTER_MAX_PAGE_COUNT,
page_count=page_count
)
@@ -601,12 +608,12 @@ def check_messages(service_id, template_id, upload_id, row_index=2):
data = _check_messages(service_id, template_id, upload_id, row_index)
if (
data['recipients'].too_many_rows or
not data['count_of_recipients'] or
not data['recipients'].has_recipient_columns or
data['recipients'].duplicate_recipient_column_headers or
data['recipients'].missing_column_headers or
data['sent_previously']
data['recipients'].too_many_rows
or not data['count_of_recipients']
or not data['recipients'].has_recipient_columns
or data['recipients'].duplicate_recipient_column_headers
or data['recipients'].missing_column_headers
or data['sent_previously']
):
return render_template('views/check/column-errors.html', **data)
@@ -614,8 +621,8 @@ def check_messages(service_id, template_id, upload_id, row_index=2):
return render_template('views/check/row-errors.html', **data)
if (
data['errors'] or
data['trying_to_send_letters_in_trial_mode']
data['errors']
or data['trying_to_send_letters_in_trial_mode']
):
return render_template('views/check/column-errors.html', **data)
@@ -877,10 +884,14 @@ def _check_notification(service_id, template_id, exception=None):
raise PermanentRedirect(back_link)
template.values = get_recipient_and_placeholders_from_session(template.template_type)
page_count = get_page_count_for_letter(db_template, template.values)
return dict(
template=template,
back_link=back_link,
help=get_help_argument(),
letter_too_long=is_letter_too_long(page_count),
letter_max_pages=LETTER_MAX_PAGE_COUNT,
page_count=page_count,
**(get_template_error_dict(exception) if exception else {}),
)

View File

@@ -6,7 +6,9 @@ from flask import abort, flash, redirect, render_template, request, url_for
from flask_login import current_user
from markupsafe import Markup
from notifications_python_client.errors import HTTPError
from notifications_utils import LETTER_MAX_PAGE_COUNT
from notifications_utils.formatters import nl2br
from notifications_utils.pdf import is_letter_too_long
from notifications_utils.recipients import first_column_headings
from app import (
@@ -58,6 +60,8 @@ def view_template(service_id, template_id):
'.send_one_off', service_id=service_id, template_id=template_id
))
page_count = get_page_count_for_letter(template)
return render_template(
'views/templates/template.html',
template=get_template(
@@ -74,6 +78,9 @@ def view_template(service_id, template_id):
),
template_postage=template["postage"],
user_has_template_permission=user_has_template_permission,
letter_too_long=is_letter_too_long(page_count),
letter_max_pages=LETTER_MAX_PAGE_COUNT,
page_count=page_count
)

View File

@@ -1,12 +1,15 @@
{% from "components/form.html" import form_wrapper %}
{% macro banner(body, type=None, with_tick=False, delete_button=None, subhead=None, context=None, action=None) %}
{% macro banner(body, type=None, with_tick=False, delete_button=None, subhead=None, context=None, action=None, id=None) %}
<div
class='banner{% if type %}-{{ type }}{% endif %}{% if with_tick %}-with-tick{% endif %}'
{% if type == 'dangerous' %}
role='group'
tabindex='-1'
{% endif %}
{% if id %}
id={{ id }}
{% endif %}
>
{% if subhead -%}
<h1 class="banner-title">{{ subhead }}</h1>
@@ -26,6 +29,6 @@
</div>
{% endmacro %}
{% macro banner_wrapper(type=None, with_tick=False, delete_button=None, subhead=None, action=None) %}
{{ banner(caller()|safe, type=type, with_tick=with_tick, delete_button=delete_button, subhead=subhead, action=action) }}
{% macro banner_wrapper(type=None, with_tick=False, delete_button=None, subhead=None, action=None, id=None) %}
{{ banner(caller()|safe, type=type, with_tick=with_tick, delete_button=delete_button, subhead=subhead, action=action, id=id) }}
{% endmacro %}

View File

@@ -0,0 +1,8 @@
<h1 class='banner-title' data-module="track-error" data-error-type="Trying to send a letter that's too long" data-error-label="{{ upload_id }}">
Your letter is too long
</h1>
<p>
Letters must be {{ letter_max_pages }} pages or less.
<br>
Your letter is {{ page_count }} pages long.
</p>

View File

@@ -25,10 +25,15 @@
back_link=back_link
) }}
{% if letter_too_long %}
{% call banner_wrapper(type='dangerous', id='letter-too-long') %}
{% include "partials/check/letter-too-long.html" %}
{% endcall %}
{% endif %}
{{ skip_to_file_contents() }}
{{ template|string }}
<div class="bottom-gutter-3-2">
<form method="post" enctype="multipart/form-data" action="{{url_for('main.start_job', service_id=current_service.id, upload_id=upload_id, original_file_name=original_file_name)}}" class='page-footer'>
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
@@ -39,7 +44,7 @@
wrapping_class='bottom-gutter-2-3'
) }}
{% endif %}
{% if template.template_type != 'letter' or not request.args.from_test %}
{% if (template.template_type != 'letter' or not request.args.from_test) and not letter_too_long %}
<button type="submit" class="button">Send {{ count_of_recipients }} {{ message_count_label(count_of_recipients, template.template_type, suffix='') }}</button>
{% else %}
<a href="{{ url_for('main.check_messages_preview', service_id=current_service.id, template_id=template.id, upload_id=upload_id, filetype='pdf') }}" download class="button">Download as a PDF</a>

View File

@@ -49,6 +49,14 @@
{% include "partials/check/message-too-long.html" %}
{% endcall %}
</div>
{% elif letter_too_long %}
{% set error = 'letter-too-long' %}
{{ govuk_back_link(back_link) }}
<div class="bottom-gutter">
{% call banner_wrapper(type='dangerous', id='letter-too-long') %}
{% include "partials/check/letter-too-long.html" %}
{% endcall %}
</div>
{% else %}
{{ page_header(
'Preview of {}'.format(template.name),

View File

@@ -1,4 +1,5 @@
{% from 'components/message-count-label.html' import message_count_label %}
{% from "components/banner.html" import banner_wrapper %}
<div class="column-whole">
{% if template._template.archived %}
@@ -15,7 +16,12 @@
<div class="bottom-gutter-2-3">
<div class="grid-row">
{% if template.template_type == 'letter' %}
{% if current_user.has_permissions('send_messages', restrict_admin_usage=True) %}
{% if letter_too_long %}
{% call banner_wrapper(type='dangerous', id='letter-too-long') %}
{% include "partials/check/letter-too-long.html" %}
{% endcall %}
{% endif %}
{% if current_user.has_permissions('send_messages', restrict_admin_usage=True) and not letter_too_long %}
<div class="column-half">
<a href="{{ url_for(".set_sender", service_id=current_service.id, template_id=template.id) }}" class="pill-separate-item">
Send

View File

@@ -43,6 +43,7 @@ FAILURE_STATUSES = ['failed', 'temporary-failure', 'permanent-failure',
'technical-failure', 'virus-scan-failed', 'validation-failed']
REQUESTED_STATUSES = SENDING_STATUSES + DELIVERED_STATUSES + FAILURE_STATUSES
with open('{}/email_domains.txt'.format(
os.path.dirname(os.path.realpath(__file__))
)) as email_domains: