Make AJAX requests on activity page POST not GET

See parent commit for the reason we’re doing this.

Currently our AJAX requests only work as `GET` requests. So this commit
does a bit of work to make them work as `POST` requests. This is
optional behaviour, and will only happen when the element in the page
that should be updated with AJAX has the `data-form` attribute set. It
will take the form that has the corresponding `id`, serialise it, and
use that data as the body of the post request. If not form is specified
it will not do the serialisation, and submit as a `GET` request as
before.
This commit is contained in:
Chris Hill-Scott
2017-06-13 11:20:41 +01:00
parent e65dcbe199
commit 681cea1d34
5 changed files with 16 additions and 16 deletions

View File

@@ -19,10 +19,14 @@
var clearQueue = queue => (queue.length = 0); var clearQueue = queue => (queue.length = 0);
var poll = function(renderer, resource, queue, interval) { var poll = function(renderer, resource, queue, interval, form) {
if (queue.push(renderer) === 1) $.ajax( if (queue.push(renderer) === 1) $.ajax(
resource resource,
{
'method': form ? 'post' : 'get',
'data': form ? $('#' + form).serialize() : {}
}
).done( ).done(
response => flushQueue(queue, response) response => flushQueue(queue, response)
).fail( ).fail(
@@ -41,7 +45,8 @@
getRenderer($(component)), getRenderer($(component)),
$(component).data('resource'), $(component).data('resource'),
getQueue($(component).data('resource')), getQueue($(component).data('resource')),
($(component).data('interval-seconds') || 1.5) * 1000 ($(component).data('interval-seconds') || 1.5) * 1000,
$(component).data('form')
); );
}; };

View File

@@ -205,7 +205,7 @@ def view_notifications(service_id, message_type):
) )
@main.route('/services/<service_id>/notifications/<message_type>.json') @main.route('/services/<service_id>/notifications/<message_type>.json', methods=['GET', 'POST'])
@user_has_permissions('view_activity', admin_override=True) @user_has_permissions('view_activity', admin_override=True)
def get_notifications_as_json(service_id, message_type): def get_notifications_as_json(service_id, message_type):
return jsonify(get_notifications( return jsonify(get_notifications(

View File

@@ -1,10 +1,11 @@
{% macro ajax_block(partials, url, key, interval=2, finished=False) %} {% macro ajax_block(partials, url, key, interval=2, finished=False, form='') %}
{% if not finished %} {% if not finished %}
<div <div
data-module="update-content" data-module="update-content"
data-resource="{{ url }}" data-resource="{{ url }}"
data-key="{{ key }}" data-key="{{ key }}"
data-interval-seconds="{{ interval }}" data-interval-seconds="{{ interval }}"
data-form="{{ form }}"
aria-live="polite" aria-live="polite"
> >
{% endif %} {% endif %}

View File

@@ -24,9 +24,9 @@
method="post" method="post"
action="{{ url_for('.view_notifications', service_id=current_service.id, message_type=message_type) }}" action="{{ url_for('.view_notifications', service_id=current_service.id, message_type=message_type) }}"
class="grid-row" class="grid-row"
id="search-form"
> >
<div class="column-three-quarters"> <div class="column-three-quarters">
<input type="hidden" name="status" value="{{ status }}">
{{ textbox( {{ textbox(
search_form.to, search_form.to,
width='1-1', width='1-1',
@@ -41,8 +41,9 @@
{{ ajax_block( {{ ajax_block(
partials, partials,
url_for('.get_notifications_as_json', service_id=current_service.id, message_type=message_type, status=status, page=page, to=to), url_for('.get_notifications_as_json', service_id=current_service.id, message_type=message_type, status=status, page=page),
'notifications' 'notifications',
form='search-form'
) }} ) }}
{% endblock %} {% endblock %}

View File

@@ -110,8 +110,7 @@ def test_can_show_notifications(
assert query_dict['status'] == [status_argument] assert query_dict['status'] == [status_argument]
if expected_page_argument: if expected_page_argument:
assert query_dict['page'] == [str(expected_page_argument)] assert query_dict['page'] == [str(expected_page_argument)]
if to_argument: assert 'to' not in query_dict
assert query_dict['to'] == [to_argument]
mock_get_notifications.assert_called_with( mock_get_notifications.assert_called_with(
limit_days=7, limit_days=7,
@@ -135,7 +134,6 @@ def test_can_show_notifications(
@pytest.mark.parametrize(( @pytest.mark.parametrize((
'initial_query_arguments,' 'initial_query_arguments,'
'form_post_data,' 'form_post_data,'
'expected_status_field_value,'
'expected_search_box_contents' 'expected_search_box_contents'
), [ ), [
( (
@@ -143,7 +141,6 @@ def test_can_show_notifications(
'message_type': 'sms', 'message_type': 'sms',
}, },
{}, {},
'sending,delivered,failed',
'', '',
), ),
( (
@@ -153,7 +150,6 @@ def test_can_show_notifications(
{ {
'to': '+33(0)5-12-34-56-78', 'to': '+33(0)5-12-34-56-78',
}, },
'sending,delivered,failed',
'+33(0)5-12-34-56-78', '+33(0)5-12-34-56-78',
), ),
( (
@@ -165,7 +161,6 @@ def test_can_show_notifications(
{ {
'to': 'test@example.com', 'to': 'test@example.com',
}, },
'failed',
'test@example.com', 'test@example.com',
), ),
]) ])
@@ -175,7 +170,6 @@ def test_search_recipient_form(
mock_get_detailed_service, mock_get_detailed_service,
initial_query_arguments, initial_query_arguments,
form_post_data, form_post_data,
expected_status_field_value,
expected_search_box_contents, expected_search_box_contents,
): ):
response = logged_in_client.post( response = logged_in_client.post(
@@ -199,7 +193,6 @@ def test_search_recipient_form(
query_dict = parse_qs(url.query) query_dict = parse_qs(url.query)
assert query_dict == {} assert query_dict == {}
assert page.find("input", {'name': 'status'})['value'] == expected_status_field_value
assert page.find("input", {'name': 'to'})['value'] == expected_search_box_contents assert page.find("input", {'name': 'to'})['value'] == expected_search_box_contents