mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-08 01:28:24 -04:00
Using new endpoint for template statistics
- gets notifications by template id, returning the most recent to illustrate the last use of that template.
This commit is contained in:
@@ -186,9 +186,21 @@ def delete_service_template(service_id, template_id):
|
|||||||
template['template_content'] = template['content']
|
template['template_content'] = template['content']
|
||||||
form = form_objects[template['template_type']](**template)
|
form = form_objects[template['template_type']](**template)
|
||||||
|
|
||||||
template_statistics = template_statistics_client.get_template_statistics_for_template(service_id, template['id'])
|
try:
|
||||||
last_use_message = get_last_use_message(form.name.data, template_statistics)
|
last_used_notification = template_statistics_client.get_template_statistics_for_template(
|
||||||
flash('{}. Are you sure you want to delete it?'.format(last_use_message), 'delete')
|
service_id, template['id']
|
||||||
|
)
|
||||||
|
message = '{} was last used {} ago'.format(
|
||||||
|
last_used_notification['template']['name'],
|
||||||
|
get_human_readable_delta(
|
||||||
|
parse(last_used_notification['created_at']).replace(tzinfo=None),
|
||||||
|
datetime.utcnow())
|
||||||
|
)
|
||||||
|
except HTTPError as e:
|
||||||
|
if e.status_code == 404:
|
||||||
|
message = '{} has never been used'.format(template['name'])
|
||||||
|
|
||||||
|
flash('{}. Are you sure you want to delete it?'.format(message), 'delete')
|
||||||
return render_template(
|
return render_template(
|
||||||
'views/edit-{}-template.html'.format(template['template_type']),
|
'views/edit-{}-template.html'.format(template['template_type']),
|
||||||
h1='Edit template',
|
h1='Edit template',
|
||||||
|
|||||||
@@ -213,6 +213,10 @@ def notification_json(
|
|||||||
'next': '/service/{}/notifications'.format(service_id),
|
'next': '/service/{}/notifications'.format(service_id),
|
||||||
'last': '/service/{}/notifications'.format(service_id)
|
'last': '/service/{}/notifications'.format(service_id)
|
||||||
}
|
}
|
||||||
|
job_payload = None
|
||||||
|
if job:
|
||||||
|
job_payload = {'id': job['id'], 'original_file_name': job['original_file_name']}
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'notifications': [{
|
'notifications': [{
|
||||||
'to': to,
|
'to': to,
|
||||||
@@ -220,7 +224,7 @@ def notification_json(
|
|||||||
'id': template['id'],
|
'id': template['id'],
|
||||||
'name': template['name'],
|
'name': template['name'],
|
||||||
'template_type': template['template_type']},
|
'template_type': template['template_type']},
|
||||||
'job': {'id': job['id'], 'original_file_name': job['original_file_name']} if job else None,
|
'job': job_payload,
|
||||||
'sent_at': sent_at,
|
'sent_at': sent_at,
|
||||||
'status': status,
|
'status': status,
|
||||||
'created_at': created_at,
|
'created_at': created_at,
|
||||||
@@ -228,13 +232,58 @@ def notification_json(
|
|||||||
'job_row_number': job_row_number,
|
'job_row_number': job_row_number,
|
||||||
'template_version': template['version']
|
'template_version': template['version']
|
||||||
} for i in range(rows)],
|
} for i in range(rows)],
|
||||||
'total': 5,
|
'total': rows,
|
||||||
'page_size': 50,
|
'page_size': 50,
|
||||||
'links': links
|
'links': links
|
||||||
}
|
}
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def single_notification_json(
|
||||||
|
service_id,
|
||||||
|
job=None,
|
||||||
|
template=None,
|
||||||
|
to='07123456789',
|
||||||
|
status=None,
|
||||||
|
sent_at=None,
|
||||||
|
job_row_number=None,
|
||||||
|
created_at=None,
|
||||||
|
updated_at=None
|
||||||
|
):
|
||||||
|
if template is None:
|
||||||
|
template = template_json(service_id, str(generate_uuid()))
|
||||||
|
if sent_at is None:
|
||||||
|
sent_at = str(datetime.utcnow().time())
|
||||||
|
if created_at is None:
|
||||||
|
created_at = str(datetime.utcnow().time())
|
||||||
|
if updated_at is None:
|
||||||
|
updated_at = str((datetime.utcnow() + timedelta(minutes=1)).time())
|
||||||
|
if status is None:
|
||||||
|
status = 'delivered'
|
||||||
|
job_payload = None
|
||||||
|
if job:
|
||||||
|
job_payload = {'id': job['id'], 'original_file_name': job['original_file_name']}
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'sent_at': sent_at,
|
||||||
|
'billable_units': 1,
|
||||||
|
'status': status,
|
||||||
|
'created_at': created_at,
|
||||||
|
'reference': None,
|
||||||
|
'updated_at': updated_at,
|
||||||
|
'template_version': 5,
|
||||||
|
'service': service_id,
|
||||||
|
'id': '29441662-17ce-4ffe-9502-fcaed73b2826',
|
||||||
|
'template': template,
|
||||||
|
'job_row_number': 0,
|
||||||
|
'notification_type': 'sms',
|
||||||
|
'api_key': None,
|
||||||
|
'job': job_payload,
|
||||||
|
'sent_by': 'mmg'
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
def validate_route_permission(mocker,
|
def validate_route_permission(mocker,
|
||||||
app_,
|
app_,
|
||||||
method,
|
method,
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ from . import (
|
|||||||
notification_json,
|
notification_json,
|
||||||
invite_json,
|
invite_json,
|
||||||
sample_uuid,
|
sample_uuid,
|
||||||
generate_uuid)
|
generate_uuid, single_notification_json)
|
||||||
from app.notify_client.models import (
|
from app.notify_client.models import (
|
||||||
User,
|
User,
|
||||||
InvitedUser
|
InvitedUser
|
||||||
@@ -1091,20 +1091,8 @@ def mock_get_template_statistics(mocker, service_one, fake_uuid):
|
|||||||
def mock_get_template_statistics_for_template(mocker, service_one):
|
def mock_get_template_statistics_for_template(mocker, service_one):
|
||||||
def _get_stats(service_id, template_id):
|
def _get_stats(service_id, template_id):
|
||||||
template = template_json(service_id, template_id, "Test template", "sms", "Something very interesting")
|
template = template_json(service_id, template_id, "Test template", "sms", "Something very interesting")
|
||||||
return [
|
notification = single_notification_json(service_id, template=template)
|
||||||
{
|
return notification
|
||||||
"usage_count": 1,
|
|
||||||
"template": {
|
|
||||||
"name": template['name'],
|
|
||||||
"template_type": template['template_type'],
|
|
||||||
"id": template['id']
|
|
||||||
},
|
|
||||||
"service": template['service'],
|
|
||||||
"id": str(generate_uuid()),
|
|
||||||
"day": "2016-04-04",
|
|
||||||
"updated_at": "2016-04-04T12:00:00.000000+00:00"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
return mocker.patch(
|
return mocker.patch(
|
||||||
'app.template_statistics_client.get_template_statistics_for_template', side_effect=_get_stats)
|
'app.template_statistics_client.get_template_statistics_for_template', side_effect=_get_stats)
|
||||||
|
|||||||
Reference in New Issue
Block a user