This commit is contained in:
Kenneth Kehl
2023-12-14 13:24:34 -08:00
parent 931a48b3c6
commit 32b4e6918f
4 changed files with 268 additions and 34 deletions

View File

@@ -1,5 +1,7 @@
import itertools
from string import ascii_uppercase
import time
import uuid
from zipfile import BadZipFile
from flask import (
@@ -42,7 +44,12 @@ from app.s3_client.s3_csv_client import (
s3upload,
set_metadata_on_csv_upload,
)
from app.utils import PermanentRedirect, should_skip_template_page, unicode_truncate
from app.utils import (
PermanentRedirect,
hilite,
should_skip_template_page,
unicode_truncate,
)
from app.utils.csv import Spreadsheet, get_errors_for_csv
from app.utils.templates import get_template
from app.utils.user import user_has_permissions
@@ -328,6 +335,9 @@ def get_sender_details(service_id, template_type):
@main.route("/services/<uuid:service_id>/send/<uuid:template_id>/one-off")
@user_has_permissions("send_messages", restrict_admin_usage=True)
def send_one_off(service_id, template_id):
print(
hilite(f"ENTER send_one_off service_id {service_id} template_id {template_id}")
)
session["recipient"] = None
session["placeholders"] = {}
@@ -372,6 +382,11 @@ def get_notification_check_endpoint(service_id, template):
)
@user_has_permissions("send_messages", restrict_admin_usage=True)
def send_one_off_step(service_id, template_id, step_index):
print(
hilite(
f"ENTER send_one_off_step service_id {service_id} template_id {template_id} step_index {step_index}"
)
)
if {"recipient", "placeholders"} - set(session.keys()):
return redirect(
url_for(
@@ -706,6 +721,7 @@ def get_back_link(service_id, template, step_index, placeholders=None):
def get_skip_link(step_index, template):
print(hilite(f"ENTER get_skip_link step_index {step_index}"))
if (
request.endpoint == "main.send_one_off_step"
and step_index == 0
@@ -729,6 +745,11 @@ def get_skip_link(step_index, template):
)
@user_has_permissions("send_messages", restrict_admin_usage=True)
def send_one_off_to_myself(service_id, template_id):
print(
hilite(
f"ENTER send_one_off_to_myself service_id {service_id} template_id {template_id}"
)
)
db_template = current_service.get_template_with_user_permission_or_403(
template_id, current_user
)
@@ -835,8 +856,13 @@ def get_template_error_dict(exception):
)
@user_has_permissions("send_messages", restrict_admin_usage=True)
def send_notification(service_id, template_id):
print(
hilite(
f"ENTER send_notification service_id {service_id} template_id {template_id}"
)
)
recipient = get_recipient()
print(hilite(f"recipient {recipient}"))
if not recipient:
return redirect(
url_for(
@@ -850,34 +876,65 @@ def send_notification(service_id, template_id):
template_id, current_user
)
try:
noti = notification_api_client.send_notification(
service_id,
template_id=db_template["id"],
recipient=recipient,
personalisation=session["placeholders"],
sender_id=session.get("sender_id", None),
)
except HTTPError as exception:
current_app.logger.error(
'Service {} could not send notification: "{}"'.format(
current_service.id, exception.message
)
)
return render_template(
"views/notifications/check.html",
**_check_notification(service_id, template_id, exception),
)
print(hilite(f"SESSION PLACEHOLDERS = {session['placeholders']}"))
keys = []
values = []
for k, v in session["placeholders"].items():
keys.append(k)
values.append(v)
data = ",".join(keys)
vals = ",".join(values)
data = f"{data}\r\n{vals}"
filename = f"{uuid.uuid4()}.csv"
my_data = {"file_name": filename, "template_id": template_id, "data": data}
upload_id = s3upload(service_id, my_data)
print(hilite(f"MY UPLOAD ID {upload_id}"))
print(hilite("HERE IT IS:"))
print(hilite(s3download(service_id, upload_id)))
# column_headings = get_spreadsheet_column_headings_from_template(template)
# print(hilite(f"COLUMN HEADINGS {column_headings}"))
form = CsvUploadForm()
form.file.data = my_data
form.file.name = filename
print(hilite(f"FORM data {form.file.data} name = {form.file.name} "))
response = ""
try:
job_api_client.create_job(
upload_id,
service_id,
scheduled_for="",
template_id=template_id,
original_file_name=filename,
notification_count=1,
valid="True",
)
except Exception as e:
print(hilite(f"WHAT IS THE ERROR {e}"))
session.pop('recipient')
session.pop('placeholders')
print(hilite(f"try to get notifications for job_id = {upload_id} and service_id {service_id}"))
time.sleep(0.2)
notis = notification_api_client.get_notifications_for_service(service_id, job_id=upload_id, include_one_off=True)
print(f"HERE ARE INITIAL NOTIS {notis}")
while notis['total'] == 0:
print(hilite('retry notis'))
notis = notification_api_client.get_notifications_for_service(service_id, job_id=upload_id, include_one_off=True)
time.sleep(0.2)
print(hilite(f"HERE ARE THE NOTIS {notis}"))
session.pop("placeholders")
session.pop("recipient")
session.pop("sender_id", None)
return redirect(
url_for(
".view_notification",
service_id=service_id,
notification_id=noti["id"],
notification_id=notis["notifications"][0]["id"],
# used to show the final step of the tour (help=3) or not show
# a back link on a just sent one off notification (help=0)
help=request.args.get("help"),
@@ -916,3 +973,81 @@ def get_recipient():
return session["recipient"] or InsensitiveDict(session["placeholders"]).get(
"address line 1"
)
def send_messages_one_off_jobs(service_id, template_id):
notification_count = service_api_client.get_notification_count(service_id)
remaining_messages = current_service.message_limit - notification_count
db_template = current_service.get_template_with_user_permission_or_403(
template_id, current_user
)
email_reply_to = None
sms_sender = None
if db_template["template_type"] == "email":
email_reply_to = get_email_reply_to_address_from_session()
elif db_template["template_type"] == "sms":
sms_sender = get_sms_sender_from_session()
if db_template["template_type"] not in current_service.available_template_types:
return redirect(
url_for(
".action_blocked",
service_id=service_id,
notification_type=db_template["template_type"],
return_to="view_template",
template_id=template_id,
)
)
template = get_template(
db_template,
current_service,
show_recipient=True,
email_reply_to=email_reply_to,
sms_sender=sms_sender,
)
filename = f"{uuid.uuid4()}.csv"
my_data = {
"file_name": filename,
"template_id": template.id,
"data": "phone number\r\n16617550763\r\n16617550763\r\n16617550763\r\n16617550763\r\n16617550763",
}
upload_id = s3upload(service_id, my_data)
print(hilite(f"MY UPLOAD ID {upload_id}"))
print(hilite("HERE IT IS:"))
print(hilite(s3download(service_id, upload_id)))
column_headings = get_spreadsheet_column_headings_from_template(template)
print(hilite(f"COLUMN HEADINGS {column_headings}"))
form = CsvUploadForm()
form.file.data = my_data
form.file.name = filename
print(hilite(f"FORM data {form.file.data} name = {form.file.name} "))
response = ""
try:
job_api_client.create_job(
upload_id,
service_id,
scheduled_for="",
template_id=template_id,
original_file_name=filename,
notification_count=5,
)
except Exception as e:
print(hilite(f"WHAT IS THE ERROR {e}"))
session.pop("sender_id", None)
# return redirect(
# url_for(
# "main.service_dashboard",
# service_id=service_id,
# )
# )
raise PermanentRedirect(
url_for("main.send_messages", service_id=service_id, template_id=template_id)
)

View File

@@ -174,7 +174,7 @@ def sign_in():
current_app.logger.info(
f"LOGIN_DOT_GOV_SIGNOUT_REDIRECT={os.getenv('LOGIN_DOT_GOV_SIGNOUT_REDIRECT')}"
)
initial_signin_url = os.getenv('LOGIN_DOT_GOV_INITIAL_SIGNIN_URL')
initial_signin_url = os.getenv("LOGIN_DOT_GOV_INITIAL_SIGNIN_URL")
current_app.logger.info(f"LOGIN_DOT_GOV_INITIAL_SIGNIN_URL={initial_signin_url}")
return render_template(

View File

@@ -103,7 +103,16 @@ class JobApiClient(NotifyAdminAPIClient):
return scheduled_for
def create_job(self, job_id, service_id, scheduled_for=None):
def create_job(
self,
job_id,
service_id,
scheduled_for=None,
template_id=None,
original_file_name=None,
notification_count=None,
valid=None,
):
data = {"id": job_id}
# make a datetime object in the user's preferred timezone
@@ -112,6 +121,15 @@ class JobApiClient(NotifyAdminAPIClient):
scheduled_for = JobApiClient.convert_user_time_to_utc(scheduled_for)
data.update({"scheduled_for": scheduled_for})
if template_id:
data.update({"template_id": template_id})
if original_file_name:
data.update({"original_file_name": original_file_name})
if notification_count:
data.update({"notification_count": notification_count})
if valid:
data.update({"valid": valid})
data = _attach_current_user(data)
job = self.post(url="/service/{}/job".format(service_id), data=data)