Move letter too long banner message over from utils, also refactor

This commit is contained in:
Pea Tyczynska
2019-10-01 17:16:15 +01:00
parent b42c7c4c9f
commit 12ec2870af
14 changed files with 63 additions and 38 deletions

View File

@@ -53,7 +53,6 @@ class Config(object):
EMAIL_2FA_EXPIRY_SECONDS = 1800 # 30 Minutes
HEADER_COLOUR = '#FFBF47' # $yellow
HTTP_PROTOCOL = 'http'
LETTER_MAX_PAGES = 10
MAX_FAILED_LOGIN_COUNT = 10
NOTIFY_APP_NAME = 'admin'
NOTIFY_LOG_LEVEL = 'DEBUG'

View File

@@ -54,6 +54,7 @@ from app.utils import (
get_errors_for_csv,
get_help_argument,
get_template,
is_letter_too_long,
should_skip_template_page,
unicode_truncate,
user_has_permissions,
@@ -591,8 +592,7 @@ def _check_messages(service_id, template_id, upload_id, preview_row, letters_as_
sent_previously=job_api_client.has_sent_previously(
service_id, template.id, db_template['version'], request.args.get('original_file_name', '')
),
page_count=page_count,
letter_max_pages=current_app.config['LETTER_MAX_PAGES'],
letter_too_long=is_letter_too_long(page_count),
)
@@ -885,8 +885,7 @@ def _check_notification(service_id, template_id, exception=None):
template=template,
back_link=back_link,
help=get_help_argument(),
page_count=page_count,
letter_max_pages=current_app.config['LETTER_MAX_PAGES'],
letter_too_long=is_letter_too_long(page_count),
**(get_template_error_dict(exception) if exception else {}),
)

View File

@@ -2,15 +2,7 @@ from datetime import datetime, timedelta
from string import ascii_uppercase
from dateutil.parser import parse
from flask import (
abort,
current_app,
flash,
redirect,
render_template,
request,
url_for,
)
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
@@ -41,6 +33,7 @@ from app.template_previews import TemplatePreview, get_page_count_for_letter
from app.utils import (
email_or_sms_not_enabled,
get_template,
is_letter_too_long,
should_skip_template_page,
user_has_permissions,
user_is_platform_admin,
@@ -82,8 +75,7 @@ def view_template(service_id, template_id):
),
template_postage=template["postage"],
user_has_template_permission=user_has_template_permission,
page_count=get_page_count_for_letter(template),
letter_max_pages=current_app.config['LETTER_MAX_PAGES'],
letter_too_long=is_letter_too_long(get_page_count_for_letter(template)),
)

View File

@@ -26,7 +26,7 @@ from app.s3_client.s3_letter_upload_client import (
upload_letter_to_s3,
)
from app.template_previews import TemplatePreview, sanitise_letter
from app.utils import get_template, user_has_permissions
from app.utils import get_template, is_letter_too_long, user_has_permissions
MAX_FILE_UPLOAD_SIZE = 2 * 1024 * 1024 # 2MB
@@ -79,8 +79,6 @@ def upload_letter(service_id):
raise ex
else:
status = 'valid'
if page_count > current_app.config['LETTER_MAX_PAGES']:
status = 'invalid'
file_contents = base64.b64decode(response.json()['file'].encode())
upload_letter_to_s3(
file_contents,
@@ -111,6 +109,7 @@ def uploaded_letter_preview(service_id, file_id):
metadata = get_letter_metadata(service_id, file_id)
original_filename = metadata.get('filename')
page_count = metadata.get('page_count')
letter_too_long = is_letter_too_long(int(page_count))
status = metadata.get('status')
template_dict = service_api_client.get_precompiled_template(service_id)
@@ -132,6 +131,7 @@ def uploaded_letter_preview(service_id, file_id):
template=template,
status=status,
file_id=file_id,
letter_too_long=letter_too_long,
)

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,6 @@
<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 }}">
This letter is too long
</h1>
<p>
Letters must be {{ letter_max_pages }} pages or fewer
</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) and (not page_count or page_count <= letter_max_pages) %}
{% 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),
@@ -66,7 +74,7 @@
help='3' if help else 0
)}}" class='page-footer'>
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
{% if not error and (not page_count or page_count <= letter_max_pages) %}
{% if not error %}
<button type="submit" class="button">Send 1 {{ message_count_label(1, template.template_type, suffix='') }}</button>
{% endif %}
{% if template.template_type == 'letter' %}

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) and page_count <= letter_max_pages %}
{% 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

@@ -1,5 +1,6 @@
{% extends "withnav_template.html" %}
{% from "components/page-header.html" import page_header %}
{% from "components/banner.html" import banner_wrapper %}
{% block service_page_title %}
{{ original_filename }}
@@ -16,12 +17,16 @@
Validation failed
</p>
{% endif %}
{% if letter_too_long %}
{% call banner_wrapper(type='dangerous', id='letter-too-long') %}
{% include "partials/check/letter-too-long.html" %}
{% endcall %}
{% endif %}
<div class="letter-sent">
{{ template|string }}
</div>
{% if status == 'valid' %}
{% if status == 'valid' and not letter_too_long %}
<div class="js-stick-at-bottom-when-scrolling">
<form method="post" enctype="multipart/form-data" action="{{url_for(
'main.send_uploaded_letter',

View File

@@ -43,6 +43,8 @@ FAILURE_STATUSES = ['failed', 'temporary-failure', 'permanent-failure',
'technical-failure', 'virus-scan-failed', 'validation-failed']
REQUESTED_STATUSES = SENDING_STATUSES + DELIVERED_STATUSES + FAILURE_STATUSES
LETTER_MAX_PAGES = 10
with open('{}/email_domains.txt'.format(
os.path.dirname(os.path.realpath(__file__))
)) as email_domains:
@@ -543,6 +545,12 @@ def get_letter_printing_statement(status, created_at):
return 'Printed on {} at 5:30pm'.format(printed_date)
def is_letter_too_long(page_count):
if not page_count:
return False
return page_count > LETTER_MAX_PAGES
class PermanentRedirect(RequestRedirect):
"""
In Werkzeug 0.15.0 the status code for RequestRedirect changed from 301 to 308.