mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-24 20:31:19 -05:00
User can cancel a letter job - happy path
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
from flask import (
|
||||
Response,
|
||||
abort,
|
||||
flash,
|
||||
jsonify,
|
||||
redirect,
|
||||
render_template,
|
||||
@@ -11,6 +12,7 @@ from flask import (
|
||||
url_for,
|
||||
)
|
||||
from flask_login import current_user
|
||||
from notifications_python_client.errors import HTTPError
|
||||
from notifications_utils.letter_timings import get_letter_timings
|
||||
from notifications_utils.template import Template, WithSubjectTemplate
|
||||
|
||||
@@ -100,6 +102,8 @@ def view_job(service_id, job_id):
|
||||
finished=(total_notifications == processed_notifications),
|
||||
uploaded_file_name=job['original_file_name'],
|
||||
template_id=job['template'],
|
||||
job_id=job_id,
|
||||
template_type=template["template_type"],
|
||||
status=request.args.get('status', ''),
|
||||
updates_url=url_for(
|
||||
".view_job_updates",
|
||||
@@ -109,8 +113,8 @@ def view_job(service_id, job_id):
|
||||
),
|
||||
partials=get_job_partials(job, template),
|
||||
just_sent=bool(
|
||||
request.args.get('just_sent') == 'yes' and
|
||||
template['template_type'] == 'letter'
|
||||
request.args.get('just_sent') == 'yes'
|
||||
and template['template_type'] == 'letter'
|
||||
),
|
||||
just_sent_message=just_sent_message,
|
||||
)
|
||||
@@ -157,6 +161,23 @@ def cancel_job(service_id, job_id):
|
||||
return redirect(url_for('main.service_dashboard', service_id=service_id))
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/jobs/<uuid:job_id>/cancel", methods=['GET', 'POST'])
|
||||
@user_has_permissions()
|
||||
def cancel_letter_job(service_id, job_id):
|
||||
|
||||
if request.method == 'POST':
|
||||
try:
|
||||
number_of_letters = job_api_client.cancel_letter_job(current_service.id, job_id)
|
||||
except HTTPError as e:
|
||||
flash(e.message, 'dangerous')
|
||||
return redirect(url_for('main.view_job', service_id=service_id, job_id=job_id))
|
||||
flash(" {} letters have been cancelled succesfully".format(number_of_letters), 'default_with_tick')
|
||||
return redirect(url_for('main.service_dashboard', service_id=service_id))
|
||||
|
||||
flash("Are you sure you want to cancel sending those letters?", 'cancel')
|
||||
return view_job(service_id, job_id)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/jobs/<job_id>.json")
|
||||
@user_has_permissions()
|
||||
def view_job_updates(service_id, job_id):
|
||||
|
||||
@@ -135,5 +135,12 @@ class JobApiClient(NotifyAdminAPIClient):
|
||||
|
||||
return job
|
||||
|
||||
@cache.delete('has_jobs-{service_id}')
|
||||
def cancel_letter_job(self, service_id, job_id):
|
||||
return self.post(
|
||||
url='/service/{}/job/{}/cancel-letter-job'.format(service_id, job_id),
|
||||
data={}
|
||||
)
|
||||
|
||||
|
||||
job_api_client = JobApiClient()
|
||||
|
||||
@@ -21,4 +21,14 @@
|
||||
{{ ajax_block(partials, updates_url, 'counts', finished=finished) }}
|
||||
{{ ajax_block(partials, updates_url, 'notifications', finished=finished) }}
|
||||
|
||||
{% if template_type == "letter" %}
|
||||
<div class="js-stick-at-bottom-when-scrolling">
|
||||
<div class="page-footer">
|
||||
<span class="page-footer-delete-link page-footer-delete-link-without-button">
|
||||
<a href="{{ url_for('main.cancel_letter_job', service_id=current_service.id, job_id=job_id) }}">Cancel sending those letters</a>
|
||||
</span>
|
||||
{% else %}
|
||||
<div> </div>
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import json
|
||||
|
||||
import pytest
|
||||
import uuid
|
||||
|
||||
from flask import url_for
|
||||
from freezegun import freeze_time
|
||||
|
||||
@@ -486,6 +488,26 @@ def test_should_not_show_cancelled_job(
|
||||
)
|
||||
|
||||
|
||||
def test_should_cancel_letter_job(
|
||||
client_request,
|
||||
mocker,
|
||||
):
|
||||
job_id = uuid.uuid4()
|
||||
mock_cancel = mocker.patch('app.main.jobs.job_api_client.cancel_letter_job')
|
||||
client_request.post(
|
||||
'main.cancel_letter_job',
|
||||
service_id=SERVICE_ONE_ID,
|
||||
job_id=job_id,
|
||||
_expected_status=302,
|
||||
_expected_redirect=url_for(
|
||||
'main.service_dashboard',
|
||||
service_id=SERVICE_ONE_ID,
|
||||
_external=True,
|
||||
)
|
||||
)
|
||||
mock_cancel.assert_called_once_with(SERVICE_ONE_ID, job_id)
|
||||
|
||||
|
||||
@freeze_time("2016-01-01 00:00:00.000001")
|
||||
def test_should_show_updates_for_one_job_as_json(
|
||||
logged_in_client,
|
||||
|
||||
Reference in New Issue
Block a user