diff --git a/app/main/views/send.py b/app/main/views/send.py index 02f7d2121..dac927b0c 100644 --- a/app/main/views/send.py +++ b/app/main/views/send.py @@ -653,17 +653,18 @@ def preview_job(service_id, template_id, upload_id, row_index=2): data = _check_messages( service_id, template_id, upload_id, row_index, force_hide_sender=True ) + create_job(service_id, upload_id) return render_template( "views/check/preview.html", - scheduled_for=session["scheduled_for"], + scheduled_for=session.get("scheduled_for"), **data, ) -@main.route("/services//start-job/", methods=["POST"]) +@main.route("/services//create-job/", methods=["POST"]) @user_has_permissions("send_messages", restrict_admin_usage=True) -def start_job(service_id, upload_id): +def create_job(service_id, upload_id): scheduled_for = session.pop("scheduled_for", None) job_api_client.create_job( upload_id, @@ -671,8 +672,12 @@ def start_job(service_id, upload_id): scheduled_for=scheduled_for, ) - session.pop("sender_id", None) +@main.route("/services//start-job/", methods=["POST"]) +@user_has_permissions("send_messages", restrict_admin_usage=True) +def start_job(service_id, upload_id): + job_api_client.start_job(service_id, upload_id) + session.pop("sender_id", None) return redirect( url_for( "main.view_job", @@ -908,36 +913,7 @@ def preview_notification(service_id, template_id): template_id=template_id, ) ) - - session["scheduled_for"] = request.form.get("scheduled_for", "") - - return render_template( - "views/notifications/preview.html", - **_check_notification( - service_id, template_id, show_recipient=False, force_hide_sender=True - ), - scheduled_for=session["scheduled_for"], - recipient=recipient, - ) - - -@main.route( - "/services//template//notification/check", - methods=["POST"], -) -@user_has_permissions("send_messages", restrict_admin_usage=True) -def send_notification(service_id, template_id): scheduled_for = session.pop("scheduled_for", "") - recipient = get_recipient() - if not recipient: - return redirect( - url_for( - ".send_one_off", - service_id=service_id, - template_id=template_id, - ) - ) - keys = [] values = [] for k, v in session["placeholders"].items(): @@ -968,10 +944,39 @@ def send_notification(service_id, template_id): notification_count=1, valid="True", ) + session["recipient"] = recipient + session["upload_id"] = upload_id + session["scheduled_for"] = request.form.get("scheduled_for", "") - session.pop("recipient") - session.pop("placeholders") + return render_template( + "views/notifications/preview.html", + **_check_notification( + service_id, template_id, show_recipient=False, force_hide_sender=True + ), + upload_id=upload_id, + scheduled_for=session["scheduled_for"], + recipient=recipient, + ) + +@main.route( + "/services//template//notification/check", + methods=["POST"], +) +@user_has_permissions("send_messages", restrict_admin_usage=True) +def send_notification(service_id, template_id): + recipient = get_recipient() + if not recipient: + return redirect( + url_for( + ".send_one_off", + service_id=service_id, + template_id=template_id, + ) + ) + + upload_id = session.pop("upload_id") + job_api_client.start_job(service_id, upload_id) # We have to wait for the job to run and create the notification in the database time.sleep(0.1) notifications = notification_api_client.get_notifications_for_service( diff --git a/app/notify_client/job_api_client.py b/app/notify_client/job_api_client.py index 538bdd370..93e161d83 100644 --- a/app/notify_client/job_api_client.py +++ b/app/notify_client/job_api_client.py @@ -147,5 +147,10 @@ class JobApiClient(NotifyAdminAPIClient): url="/service/{}/job/{}/cancel".format(service_id, job_id), data={} ) + def start_job(self, service_id, job_id): + return self.post( + url="/service/{}/job/{}/start-job".format(service_id, job_id), data={} + ) + job_api_client = JobApiClient() diff --git a/app/notify_client/notification_api_client.py b/app/notify_client/notification_api_client.py index 95ac96a04..8f3b75f8e 100644 --- a/app/notify_client/notification_api_client.py +++ b/app/notify_client/notification_api_client.py @@ -37,11 +37,10 @@ class NotificationApiClient(NotifyAdminAPIClient): # we do not want in our logs, so we do a POST request instead of a GET method = self.post if to else self.get kwargs = {"data": params} if to else {"params": params} - if job_id: return method( url="/service/{}/job/{}/notifications".format(service_id, job_id), - **kwargs + **kwargs, ) else: if limit_days is not None: @@ -100,5 +99,8 @@ class NotificationApiClient(NotifyAdminAPIClient): url="/service/{}/job/{}/notification_count".format(service_id, job_id) )["count"] + def get_total_notification_message_parts_by_job_id(self, job_id): + return self.get(url="/notifications/{}".format(job_id)) + notification_api_client = NotificationApiClient() diff --git a/poetry.lock b/poetry.lock index 9fc95cea7..1b201f09b 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1595,7 +1595,6 @@ files = [ {file = "msgpack-1.0.8-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:5fbb160554e319f7b22ecf530a80a3ff496d38e8e07ae763b9e82fadfe96f273"}, {file = "msgpack-1.0.8-cp39-cp39-win32.whl", hash = "sha256:f9af38a89b6a5c04b7d18c492c8ccf2aee7048aff1ce8437c4683bb5a1df893d"}, {file = "msgpack-1.0.8-cp39-cp39-win_amd64.whl", hash = "sha256:ed59dd52075f8fc91da6053b12e8c89e37aa043f8986efd89e61fae69dc1b011"}, - {file = "msgpack-1.0.8-py3-none-any.whl", hash = "sha256:24f727df1e20b9876fa6e95f840a2a2651e34c0ad147676356f4bf5fbb0206ca"}, {file = "msgpack-1.0.8.tar.gz", hash = "sha256:95c02b0e27e706e48d0e5426d1710ca78e0f0628d6e89d5b5a5b91a5f12274f3"}, ]