Update message seen when deleting a template.

This commit is contained in:
Rebecca Law
2020-02-04 16:00:51 +00:00
parent fd59491d19
commit 143564c7f0
2 changed files with 4 additions and 41 deletions

View File

@@ -1,7 +1,5 @@
from datetime import datetime, timedelta
from functools import partial
from dateutil.parser import parse
from flask import abort, flash, redirect, render_template, request, url_for
from flask_login import current_user
from notifications_python_client.errors import HTTPError
@@ -10,6 +8,7 @@ from notifications_utils.pdf import is_letter_too_long
from app import (
current_service,
format_delta,
nl2br,
service_api_client,
template_folder_api_client,
@@ -676,12 +675,8 @@ def delete_service_template(service_id, template_id):
last_used_notification = template_statistics_client.get_template_statistics_for_template(
service_id, template['id']
)
message = 'This template was last used {} ago.'.format(
'more than seven days' if not last_used_notification else get_human_readable_delta(
parse(last_used_notification['created_at']).replace(tzinfo=None),
datetime.utcnow()
)
)
message = 'This template has never been used.' if not last_used_notification else \
'This template was last used {}.'.format(format_delta(last_used_notification['created_at']))
except HTTPError as e:
if e.status_code == 404:
@@ -848,18 +843,3 @@ def get_template_sender_form_dict(service_id, template):
context['current_choice'] = template['service_letter_contact'] if template['service_letter_contact'] else ''
return context
def get_human_readable_delta(from_time, until_time):
delta = until_time - from_time
if delta < timedelta(seconds=60):
return 'under a minute'
elif delta < timedelta(hours=1):
minutes = int(delta.seconds / 60)
return '{} minute{}'.format(minutes, '' if minutes == 1 else 's')
elif delta < timedelta(days=1):
hours = int(delta.seconds / 3600)
return '{} hour{}'.format(hours, '' if hours == 1 else 's')
else:
days = delta.days
return '{} day{}'.format(days, '' if days == 1 else 's')

View File

@@ -1,4 +1,3 @@
from datetime import datetime
from functools import partial
from unittest.mock import ANY, Mock
@@ -7,7 +6,6 @@ from flask import url_for
from freezegun import freeze_time
from notifications_python_client.errors import HTTPError
from app.main.views.templates import get_human_readable_delta
from tests import (
sample_uuid,
single_notification_json,
@@ -1730,7 +1728,7 @@ def test_should_show_delete_template_page_with_time_block_for_empty_notification
)
assert "Are you sure you want to delete Two week reminder?" in page.select('.banner-dangerous')[0].text
assert normalize_spaces(page.select('.banner-dangerous p')[0].text) == (
'This template was last used more than seven days ago.'
'This template has never been used.'
)
assert normalize_spaces(page.select('.sms-message-wrapper')[0].text) == (
'service one: Template <em>content</em> with & entity'
@@ -1920,21 +1918,6 @@ def test_route_invalid_permissions(
service_one)
@pytest.mark.parametrize('from_time, until_time, message', [
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 1, 12, 0, 59), 'under a minute'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 1, 12, 1), '1 minute'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 1, 12, 2, 35), '2 minutes'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 1, 12, 59), '59 minutes'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 1, 13, 0), '1 hour'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 1, 14, 0), '2 hours'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 2, 11, 59), '23 hours'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 2, 12, 0), '1 day'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 3, 14, 0), '2 days'),
])
def test_get_human_readable_delta(from_time, until_time, message):
assert get_human_readable_delta(from_time, until_time) == message
def test_can_create_email_template_with_emoji(
client_request,
mock_create_service_template