Add warning message showing date template last used when deleting

This commit is contained in:
Leo Hemsted
2016-06-06 11:09:46 +01:00
parent 2e2e15bd95
commit 4831e068d9
2 changed files with 40 additions and 4 deletions

View File

@@ -1,9 +1,10 @@
from datetime import datetime, timedelta
from string import ascii_uppercase
from flask import request, render_template, redirect, url_for, flash, abort, session
from flask import request, render_template, redirect, url_for, flash, abort
from flask_login import login_required
from notifications_utils.template import Template, TemplateChange
from notifications_utils.template import Template
from notifications_utils.recipients import first_column_heading
from notifications_python_client.errors import HTTPError
@@ -11,7 +12,7 @@ from app.main import main
from app.utils import user_has_permissions
from app.main.forms import SMSTemplateForm, EmailTemplateForm
from app.main.views.send import get_example_csv_rows
from app import service_api_client, current_service
from app import service_api_client, current_service, template_statistics_client
form_objects = {
@@ -183,7 +184,10 @@ def delete_service_template(service_id, template_id):
template['template_content'] = template['content']
form = form_objects[template['template_type']](**template)
flash('Are you sure you want to delete {}?'.format(form.name.data), 'delete')
template_statistics = template_statistics_client.get_template_statistics_for_service(service_id)
last_use_message = get_last_use_message(form.name.data, template['id'], template_statistics)
flash('{}. Are you sure you want to it?'.format(last_use_message), 'delete')
return render_template(
'views/edit-{}-template.html'.format(template['template_type']),
h1='Edit template',
@@ -216,3 +220,33 @@ def view_template_versions(service_id, template_id):
) for template in service_api_client.get_service_template_versions(service_id, template_id)['data']
]
)
def get_last_use_message(template_name, template_id, template_statistics):
try:
most_recent_use = max(
template_stats['updated_at']
for template_stats in template_statistics
if template_stats['id'] == template_id
)
except ValueError:
return '{} has never been used'
return '{} was last used {} ago'.format(
get_human_readable_delta(most_recent_use, datetime.now())
)
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, 's' if minutes == 1 else '')
elif delta < timedelta(days=1):
hours = int(delta.seconds / 3600)
return '{} hour{}'.format(hours, 's' if hours == 1 else '')
else:
days = delta.days
return '{} day{}'.format(days, 's' if days == 1 else '')

View File

@@ -226,6 +226,7 @@ def test_should_show_delete_template_page(app_,
mock_get_user,
mock_get_user_by_email,
mock_has_permissions,
mock_get_template_statistics,
fake_uuid):
with app_.test_request_context():
with app_.test_client() as client:
@@ -292,6 +293,7 @@ def test_route_permissions(mocker,
api_user_active,
service_one,
mock_get_service_template,
mock_get_template_statistics,
fake_uuid):
routes = [
'main.add_service_template',