diff --git a/app/main/views/jobs.py b/app/main/views/jobs.py index e19f72210..62ff957d5 100644 --- a/app/main/views/jobs.py +++ b/app/main/views/jobs.py @@ -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//jobs//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//jobs/.json") @user_has_permissions() def view_job_updates(service_id, job_id): diff --git a/app/notify_client/job_api_client.py b/app/notify_client/job_api_client.py index b6e8fd97d..16f104409 100644 --- a/app/notify_client/job_api_client.py +++ b/app/notify_client/job_api_client.py @@ -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() diff --git a/app/templates/views/jobs/job.html b/app/templates/views/jobs/job.html index 25d4745c9..8128fbceb 100644 --- a/app/templates/views/jobs/job.html +++ b/app/templates/views/jobs/job.html @@ -21,4 +21,14 @@ {{ ajax_block(partials, updates_url, 'counts', finished=finished) }} {{ ajax_block(partials, updates_url, 'notifications', finished=finished) }} + {% if template_type == "letter" %} +
+