Merge pull request #979 from alphagov/4-days-scheduled

Allow a job to be scheduled any time in next 4 days
This commit is contained in:
Chris Hill-Scott
2016-11-03 10:24:07 +01:00
committed by GitHub
13 changed files with 232 additions and 100 deletions

View File

@@ -117,6 +117,7 @@ def create_app():
application.add_template_filter(format_date)
application.add_template_filter(format_date_normal)
application.add_template_filter(format_date_short)
application.add_template_filter(format_datetime_relative)
application.add_template_filter(format_delta)
application.add_template_filter(format_notification_status)
application.add_template_filter(format_notification_status_as_time)
@@ -232,6 +233,23 @@ def format_datetime_short(date):
)
def format_datetime_relative(date):
return '{} at {}'.format(
get_human_day(date),
format_time(date)
)
def get_human_day(time):
# Add 1 hour to get midnight today instead of midnight tomorrow
time = (gmt_timezones(time) - timedelta(hours=1)).strftime('%A')
if time == datetime.utcnow().strftime('%A'):
return 'today'
if time == (datetime.utcnow() + timedelta(days=1)).strftime('%A'):
return 'tomorrow'
return time
def format_time(date):
return {
'12:00AM': 'Midnight',

View File

@@ -2,88 +2,144 @@
"use strict";
var render = ($options, $button) => (
filterOptionVisibility($options) && setButtonState($options, $button)
);
let states = {
'initial': Hogan.compile(`
<div class="radio-select-column">
<label class="block-label js-block-label" for="{{name}}-0">
<input checked="checked" id="{{name}}-0" name="{{name}}" type="radio" value=""> Now
</label>
</div>
<div class="radio-select-column">
{{#categories}}
<input type='button' class='button tertiary-button js-category-button' value='{{.}}' />
{{/categories}}
</div>
`),
'choose': Hogan.compile(`
<div class="radio-select-column">
<label class="block-label js-block-label" for="{{name}}-0">
<input checked="checked" id="{{name}}-0" name="{{name}}" type="radio" value="" class="js-initial-option"> Now
</label>
</div>
<div class="radio-select-column">
{{#choices}}
<label class="block-label js-block-label" for="{{id}}">
<input type="radio" value="{{value}}" id="{{id}}" name="{{name}}" class="js-option" />
{{label}}
</label>
{{/choices}}
</div>
`),
'chosen': Hogan.compile(`
<div class="radio-select-column">
<label class="block-label js-block-label" for="{{name}}-0">
<input id="{{name}}-0" name="{{name}}" type="radio" value="" class="js-initial-option"> Now
</label>
</div>
<div class="radio-select-column">
{{#choices}}
<label class="block-label js-block-label" for="{{id}}">
<input checked="checked" type="radio" value="{{value}}" id="{{id}}" name="{{name}}" />
{{label}}
</label>
{{/choices}}
</div>
<div class="radio-select-column">
<input type='button' class='button tertiary-button js-reset-button' value='Choose a different time' />
</div>
`)
};
var filterOptionVisibility = $options => $options
.removeClass('js-visible')
.filter(
(index, element) => (index === 0 || $(element).has(':checked').length)
)
.addClass('js-visible');
var setButtonState = ($options, $button) => $button
.addClass('js-visible')
.prop(
'value',
$options.has(':checked').find('input').attr('id') === $options.eq(0).find('input').attr('id') ?
'Later' : 'Choose a different time'
let focusSelected = function() {
setTimeout(
() => $('[type=radio]:checked').parent('label').blur().trigger('focus').addClass('selected'),
10
);
// Workaround because GOV.UK SelectionButtons doesnt deselect in this case
var deselectUnchecked = $options => $options
.filter(
(index, element) => $(element).not(':has(:checked)')
).removeClass('selected');
var refocus = $element => setTimeout(
() => $element.blur().trigger('focus'),
10
);
var renderIfComponentLosesFocus = ($options, $button, $focused) => () =>
($focused.attr('type') !== 'radio') &&
render($options, $button) &&
refocus($focused); // Make sure that window scrolls to focused element
};
Modules.RadioSelect = function() {
this.start = function(component) {
let $component = $(component);
let $options = $('label', $component);
let render = (state, data) => $component.html(states[state].render(data));
let choices = $('label', $component).toArray().map(function(element) {
let $element = $(element);
return {
'id': $element.attr('for'),
'label': $.trim($element.text()),
'value': $element.find('input').attr('value')
};
});
let categories = $component.data('categories').split(',');
let name = $component.find('input').eq(0).attr('name');
$component.append(
$button = $('<input type="button" value="Later" class="tertiary-button" />')
);
$component
.on('click', '.js-category-button', function(event) {
$button.on('click', () =>
$options.addClass('js-visible').has(':checked').focus() &&
$button.removeClass('js-visible')
);
event.preventDefault();
let wordsInDay = $(this).attr('value').split(' ');
let day = wordsInDay[wordsInDay.length - 1].toLowerCase();
render('choose', {
'choices': choices.filter(
element => element.label.toLowerCase().indexOf(day) > -1
),
'name': name
});
$('.js-option').eq(0).parent('label').trigger('focus');
$component.on('keydown', 'input[type=radio]', function() {
})
.on('click', '.js-option', function(event) {
// intercept keypresses which arent enter or space
if (event.which !== 13 && event.which !== 32) {
setTimeout(
renderIfComponentLosesFocus($options, $button, $(document.activeElement)),
200
);
return true;
}
// stop click being triggered by keyboard events
if (!event.pageX) return true;
event.preventDefault();
event.preventDefault();
let value = $(this).attr('value');
render('chosen', {
'choices': choices.filter(
element => element.value == value
),
'name': name
});
focusSelected();
render($options, $button);
refocus($(this));
})
.on('keydown', 'input[type=radio]', function(event) {
// intercept keypresses which arent enter or space
if (event.which !== 13 && event.which !== 32) {
return true;
}
event.preventDefault();
let value = $(this).attr('value');
render('chosen', {
'choices': choices.filter(
element => element.value == value
),
'name': name
});
focusSelected();
})
.on('click', '.js-reset-button', function(event) {
event.preventDefault();
render('initial', {
'categories': categories,
'name': name
});
focusSelected();
});
render('initial', {
'categories': categories,
'name': name
});
$component.on('click', 'input[type=radio]', function(event) {
deselectUnchecked($options);
// stop click being triggered by keyboard events
if (!event.pageX) return true;
render($options, $button);
refocus($(this));
});
render($options, $button);
$component.css({'height': 'auto'});
};

View File

@@ -6,7 +6,9 @@
vertical-align: top;
.block-label {
margin-right: 10px;
margin-right: 5px;
padding-right: $gutter - 10px;
padding-left: 54px - 10px;
}
}
@@ -15,31 +17,21 @@
display: inline-block;
vertical-align: top;
width: auto;
padding: 20px 30px 15px 30px;
padding: 20px $gutter-half 15px $gutter-half;
margin-right: 5px;
}
.js-enabled & {
height: 60px;
overflow: visible;
.block-label {
&:last-child {
margin-bottom: 10px;
}
}
.block-label,
.tertiary-button {
display: none;
}
.js-visible {
display: block;
&.tertiary-button {
display: inline-block;
}
.js-block-label {
display: inline-block;
}
}

View File

@@ -27,27 +27,58 @@ from app.main.validators import (Blacklist, CsvFileValidator, ValidGovEmail, NoC
def get_time_value_and_label(future_time):
return (
future_time.replace(tzinfo=None).isoformat(),
get_human_time(future_time.astimezone(pytz.timezone('Europe/London')))
'{} at {}'.format(
get_human_day(future_time.astimezone(pytz.timezone('Europe/London'))),
get_human_time(future_time.astimezone(pytz.timezone('Europe/London')))
)
)
def get_human_time(time):
return {
'0': 'Midnight',
'12': 'Midday'
'0': 'midnight',
'12': 'midday'
}.get(
time.strftime('%-H'),
time.strftime('%-I%p').lower()
)
def get_next_hours_from(now, hours=23):
def get_human_day(time, prefix_today_with='T'):
# Add 1 hour to get midnight today instead of midnight tomorrow
time = (time - timedelta(hours=1)).strftime('%A')
if time == datetime.utcnow().strftime('%A'):
return '{}oday'.format(prefix_today_with)
if time == (datetime.utcnow() + timedelta(days=1)).strftime('%A'):
return 'Tomorrow'
return time
def get_furthest_possible_scheduled_time():
return (datetime.utcnow() + timedelta(days=4)).replace(hour=0)
def get_next_hours_until(until):
now = datetime.utcnow()
hours = int((until - now).total_seconds() / (60 * 60))
return [
(now + timedelta(hours=i)).replace(minute=0, second=0).replace(tzinfo=pytz.utc)
for i in range(1, hours + 1)
]
def get_next_days_until(until):
now = datetime.utcnow()
days = int((until - now).total_seconds() / (60 * 60 * 24))
return [
get_human_day(
(now + timedelta(days=i)).replace(tzinfo=pytz.utc),
prefix_today_with='Later t'
)
for i in range(0, days + 1)
]
def email_address(label='Email address', gov_user=True):
validators = [
Length(min=5, max=255),
@@ -310,8 +341,11 @@ class ChooseTimeForm(Form):
def __init__(self, *args, **kwargs):
super(ChooseTimeForm, self).__init__(*args, **kwargs)
self.scheduled_for.choices = [('', 'Now')] + [
get_time_value_and_label(hour) for hour in get_next_hours_from(datetime.utcnow())
get_time_value_and_label(hour) for hour in get_next_hours_until(
get_furthest_possible_scheduled_time()
)
]
self.scheduled_for.categories = get_next_days_until(get_furthest_possible_scheduled_time())
scheduled_for = RadioField(
'When should Notify send these messages?',

View File

@@ -21,7 +21,7 @@ from notifications_utils.template import Template
from notifications_utils.recipients import RecipientCSV, first_column_heading, validate_and_format_phone_number
from app.main import main
from app.main.forms import CsvUploadForm, ChooseTimeForm
from app.main.forms import CsvUploadForm, ChooseTimeForm, get_next_days_until, get_furthest_possible_scheduled_time
from app.main.uploader import (
s3upload,
s3download

View File

@@ -38,7 +38,7 @@
</span>
{% endif %}
</legend>
<div class="radio-select" data-module="radio-select">
<div class="radio-select" data-module="radio-select" data-categories="{{ field.categories|join(',') }}">
<div class="radio-select-column">
{% for option in field %}
<label class="block-label" for="{{ option.id }}">

View File

@@ -5,7 +5,7 @@
{% if job.job_status == 'scheduled' %}
<p>
Sending will start at {{ job.scheduled_for|format_time }}
Sending will start {{ job.scheduled_for|format_datetime_relative }}
</p>
<div class="page-footer">
<form method="post">

View File

@@ -1,5 +1,13 @@
<div class="ajax-block-container">
<p class='heading-small bottom-gutter'>
Uploaded by {{ job.created_by.name }} on {{ job.created_at|format_datetime_short }}
{% if job.scheduled_for %}
{% if job.processing_started %}
Sent by {{ job.created_by.name }} on {{ job.processing_started|format_datetime_short }}
{% else %}
Uploaded by {{ job.created_by.name }} on {{ job.created_at|format_datetime_short }}
{% endif %}
{% else %}
Sent by {{ job.created_by.name }} on {{ job.created_at|format_datetime_short }}
{% endif %}
</p>
</div>

View File

@@ -23,7 +23,7 @@
<div class="file-list">
<a class="file-list-filename" href="{{ url_for('.view_job', service_id=current_service.id, job_id=item.id) }}">{{ item.original_file_name }}</a>
<span class="file-list-hint">
Sending at {{ item.scheduled_for|format_time }}
Sending {{ item.scheduled_for|format_datetime_relative }}
</span>
</div>
{% endcall %}

View File

@@ -9,14 +9,37 @@ def test_form_contains_next_24h(app_):
choices = ChooseTimeForm().scheduled_for.choices
# Friday
assert choices[0] == ('', 'Now')
assert choices[1] == ('2016-01-01T12:00:00.061258', 'Midday')
assert choices[23] == ('2016-01-02T10:00:00.061258', '10am')
assert choices[1] == ('2016-01-01T12:00:00.061258', 'Today at midday')
assert choices[13] == ('2016-01-02T00:00:00.061258', 'Today at midnight')
# Saturday
assert choices[14] == ('2016-01-02T01:00:00.061258', 'Tomorrow at 1am')
assert choices[37] == ('2016-01-03T00:00:00.061258', 'Tomorrow at midnight')
# Sunday
assert choices[38] == ('2016-01-03T01:00:00.061258', 'Sunday at 1am')
# Monday
assert choices[84] == ('2016-01-04T23:00:00.061258', 'Monday at 11pm')
assert choices[85] == ('2016-01-05T00:00:00.061258', 'Monday at midnight')
with pytest.raises(IndexError):
assert choices[24]
assert choices[
12 + # hours left in the day
(3 * 24) + # 3 days
2 # magic number
]
@freeze_time("2016-01-01 11:09:00.061258")
def test_form_defaults_to_now(app_):
assert ChooseTimeForm().scheduled_for.data == ''
@freeze_time("2016-01-01 11:09:00.061258")
def test_form_contains_next_three_days(app_):
assert ChooseTimeForm().scheduled_for.categories == [
'Later today', 'Tomorrow', 'Sunday', 'Monday'
]

View File

@@ -194,10 +194,10 @@ def test_should_show_upcoming_jobs_on_dashboard(
assert len(table_rows) == 2
assert 'send_me_later.csv' in table_rows[0].find_all('th')[0].text
assert 'Sending at 11:09am' in table_rows[0].find_all('th')[0].text
assert 'Sending today at 11:09am' in table_rows[0].find_all('th')[0].text
assert table_rows[0].find_all('td')[0].text.strip() == '1'
assert 'even_later.csv' in table_rows[1].find_all('th')[0].text
assert 'Sending at 11:09pm' in table_rows[1].find_all('th')[0].text
assert 'Sending today at 11:09pm' in table_rows[1].find_all('th')[0].text
assert table_rows[1].find_all('td')[0].text.strip() == '1'

View File

@@ -142,6 +142,7 @@ def test_should_show_job_in_progress(
assert page.find('p', {'class': 'hint'}).text.strip() == 'Report is 50% complete…'
@freeze_time("2016-01-01T00:00:00.061258")
def test_should_show_scheduled_job(
app_,
service_one,
@@ -162,7 +163,7 @@ def test_should_show_scheduled_job(
assert response.status_code == 200
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert page.find('main').find_all('p')[2].text.strip() == 'Sending will start at midnight'
assert page.find('main').find_all('p')[2].text.strip() == 'Sending will start today at midnight'
assert page.find('input', {'type': 'submit', 'value': 'Cancel sending'})
@@ -266,7 +267,7 @@ def test_should_show_updates_for_one_job_as_json(
assert 'Status' in content['notifications']
assert 'Delivered' in content['notifications']
assert '12:01am' in content['notifications']
assert 'Uploaded by Test User on 1 January at midnight' in content['status']
assert 'Sent by Test User on 1 January at midnight' in content['status']
@pytest.mark.parametrize(

View File

@@ -903,7 +903,7 @@ def mock_get_scheduled_job(mocker, api_user_active):
api_user_active,
job_id=job_id,
job_status='scheduled',
scheduled_for='2016-01-01T00:00:00.061258'
scheduled_for='2016-01-02T00:00:00.061258'
)}
return mocker.patch('app.job_api_client.get_job', side_effect=_get_job)