Merge pull request #795 from alphagov/delivery-info-specific-time

Make ‘Available for…’ text on job page relative
This commit is contained in:
Chris Hill-Scott
2016-07-20 12:01:06 +01:00
committed by GitHub
5 changed files with 68 additions and 28 deletions

View File

@@ -1,6 +1,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import time import time
import dateutil
from datetime import datetime, timedelta, timezone
import ago
from flask import ( from flask import (
render_template, render_template,
@@ -297,10 +300,8 @@ def _get_job_counts(job, help_argument):
def get_job_partials(job): def get_job_partials(job):
filter_args = _parse_filter_args(request.args) filter_args = _parse_filter_args(request.args)
_set_status_filters(filter_args) _set_status_filters(filter_args)
return { return {
'counts': render_template( 'counts': render_template(
'partials/jobs/count.html', 'partials/jobs/count.html',
@@ -310,15 +311,33 @@ def get_job_partials(job):
), ),
'notifications': render_template( 'notifications': render_template(
'partials/jobs/notifications.html', 'partials/jobs/notifications.html',
job=job,
notifications=notification_api_client.get_notifications_for_service( notifications=notification_api_client.get_notifications_for_service(
job['service'], job['id'], status=filter_args.get('status') job['service'], job['id'], status=filter_args.get('status')
)['notifications'], )['notifications'],
status=request.args.get('status', ''), download_link=url_for(
help=get_help_argument() '.view_job_csv',
service_id=current_service['id'],
job_id=job['id'],
status=request.args.get('status', '')
),
help=get_help_argument(),
time_left=get_time_left(job['created_at'])
), ),
'status': render_template( 'status': render_template(
'partials/jobs/status.html', 'partials/jobs/status.html',
job=job job=job
), ),
} }
def get_time_left(job_created_at):
return ago.human(
(
datetime.now(timezone.utc).replace(hour=23, minute=59, second=59)
) - (
dateutil.parser.parse(job_created_at) + timedelta(days=8)
),
future_tense='Data available for {}',
past_tense='Data no longer available', # No-one should ever see this
precision=1
)

View File

@@ -6,9 +6,9 @@
{% if notifications and not help %} {% if notifications and not help %}
<p class="bottom-gutter"> <p class="bottom-gutter">
<a href="{{ url_for('.view_job_csv', service_id=current_service.id, job_id=job.id, status=status) }}" download="download" class="heading-small">Download as a CSV file</a> <a href="{{ download_link }}" download="download" class="heading-small">Download this report</a>
&emsp; &emsp;
Delivery information is available for 7 days <span id="time-left">{{ time_left }}</span>
</p> </p>
{% endif %} {% endif %}

View File

@@ -1,3 +1,4 @@
ago==0.0.8
Flask==0.10.1 Flask==0.10.1
Flask-Script==2.0.5 Flask-Script==2.0.5
Flask-WTF==0.11 Flask-WTF==0.11

View File

@@ -1,6 +1,6 @@
import pytest import pytest
import uuid import uuid
from datetime import datetime, timedelta, date from datetime import datetime, timedelta, date, timezone
from flask.testing import FlaskClient from flask.testing import FlaskClient
from flask import url_for from flask import url_for
from flask_login import login_user from flask_login import login_user
@@ -155,7 +155,7 @@ def job_json(service_id,
if template_id is None: if template_id is None:
template_id = str(generate_uuid()) template_id = str(generate_uuid())
if created_at is None: if created_at is None:
created_at = str(datetime.utcnow().time()) created_at = str(datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%f%z'))
if status is None: if status is None:
status = 'Delivered' status = 'Delivered'
data = { data = {

View File

@@ -3,6 +3,7 @@ from flask import url_for
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import json import json
from app.utils import generate_notifications_csv from app.utils import generate_notifications_csv
from app.main.views.jobs import get_time_left
from tests import (notification_json, job_json) from tests import (notification_json, job_json)
from tests.conftest import fake_uuid from tests.conftest import fake_uuid
from tests.conftest import mock_get_job as mock_get_job1 from tests.conftest import mock_get_job as mock_get_job1
@@ -65,33 +66,39 @@ def test_should_show_page_for_one_job(
expected_api_call expected_api_call
): ):
file_name = mock_get_job(service_one['id'], fake_uuid)['data']['original_file_name'] file_name = mock_get_job(service_one['id'], fake_uuid)['data']['original_file_name']
with app_.test_request_context(): with app_.test_request_context(), app_.test_client() as client:
with app_.test_client() as client: client.login(active_user_with_permissions, mocker, service_one)
client.login(active_user_with_permissions, mocker, service_one) response = client.get(url_for(
response = client.get(url_for( 'main.view_job',
'main.view_job', service_id=service_one['id'],
service_id=service_one['id'], job_id=fake_uuid,
job_id=fake_uuid, status=status_argument
status=status_argument ))
))
assert response.status_code == 200 assert response.status_code == 200
content = response.get_data(as_text=True) page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert "{}: Your vehicle tax is about to expire".format(service_one['name']) in content assert page.h1.text.strip() == 'thisisatest.csv'
assert file_name in content assert page.find('div', {'class': 'sms-message-wrapper'}).text.strip() == (
assert 'Delivered' in content '{}: Your vehicle tax is about to expire'.format(service_one['name'])
assert '11:10' in content )
assert url_for( assert ' '.join(page.find('tbody').find('tr').text.split()) == (
'07123456789 1 January at 11:10 Delivered'
)
assert page.find('div', {'data-key': 'notifications'})['data-resource'] == url_for(
'main.view_job_updates', 'main.view_job_updates',
service_id=service_one['id'], service_id=service_one['id'],
job_id=fake_uuid, job_id=fake_uuid,
status=status_argument, status=status_argument,
) in content )
assert url_for( csv_link = page.find('a', {'download': 'download'})
assert csv_link['href'] == url_for(
'main.view_job_csv', 'main.view_job_csv',
service_id=service_one['id'], service_id=service_one['id'],
job_id=fake_uuid job_id=fake_uuid,
) in content status=status_argument
)
assert csv_link.text == 'Download this report'
assert page.find('span', {'id': 'time-left'}).text == 'Data available for 7 days'
mock_get_notifications.assert_called_with( mock_get_notifications.assert_called_with(
service_one['id'], service_one['id'],
fake_uuid, fake_uuid,
@@ -301,3 +308,16 @@ def test_should_download_notifications_for_a_job(app_,
assert response.get_data(as_text=True) == csv_content assert response.get_data(as_text=True) == csv_content
assert 'text/csv' in response.headers['Content-Type'] assert 'text/csv' in response.headers['Content-Type']
assert 'sample template - 1 January at 11:09.csv"' in response.headers['Content-Disposition'] assert 'sample template - 1 January at 11:09.csv"' in response.headers['Content-Disposition']
@pytest.mark.parametrize(
"job_created_at, expected_message", [
("2016-01-10 11:09:00.000000+00:00", "Data available for 7 days"),
("2016-01-04 11:09:00.000000+00:00", "Data available for 1 day"),
("2016-01-03 11:09:00.000000+00:00", "Data available for 11 hours"),
("2016-01-02 23:59:59.000000+00:00", "Data no longer available")
]
)
@freeze_time("2016-01-10 12:00:00.000000")
def test_time_left(job_created_at, expected_message):
assert get_time_left(job_created_at) == expected_message