2018-02-20 11:22:17 +00:00
|
|
|
|
from flask import current_app, redirect, render_template, session, url_for
|
2019-07-01 15:22:08 +01:00
|
|
|
|
from flask_login import current_user
|
2017-08-08 11:13:08 +01:00
|
|
|
|
from notifications_python_client.errors import HTTPError
|
2016-02-18 14:54:59 +00:00
|
|
|
|
|
2021-04-07 09:32:18 +01:00
|
|
|
|
from app import service_api_client
|
2021-01-06 12:12:01 +00:00
|
|
|
|
from app.formatters import email_safe
|
2018-02-20 11:22:17 +00:00
|
|
|
|
from app.main import main
|
2022-09-12 16:30:15 +00:00
|
|
|
|
from app.main.forms import CreateServiceForm
|
2023-11-14 07:51:56 -08:00
|
|
|
|
from app.utils.user import user_is_gov_user, user_is_logged_in
|
2015-12-14 17:12:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
|
def _create_service(service_name, organization_type, email_from, form):
|
2017-08-08 11:13:08 +01:00
|
|
|
|
try:
|
2017-10-04 11:49:32 +01:00
|
|
|
|
service_id = service_api_client.create_service(
|
2018-04-19 14:01:45 +01:00
|
|
|
|
service_name=service_name,
|
2023-07-12 12:09:44 -04:00
|
|
|
|
organization_type=organization_type,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
message_limit=current_app.config["DEFAULT_SERVICE_LIMIT"],
|
2018-04-19 14:01:45 +01:00
|
|
|
|
restricted=True,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
user_id=session["user_id"],
|
2018-04-19 14:01:45 +01:00
|
|
|
|
email_from=email_from,
|
2017-10-04 11:49:32 +01:00
|
|
|
|
)
|
2023-08-25 09:12:23 -07:00
|
|
|
|
session["service_id"] = service_id
|
2017-10-31 11:22:57 +00:00
|
|
|
|
|
2017-08-08 11:13:08 +01:00
|
|
|
|
return service_id, None
|
|
|
|
|
|
except HTTPError as e:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if e.status_code == 400 and e.message["name"]:
|
2017-08-08 11:13:08 +01:00
|
|
|
|
form.name.errors.append("This service name is already in use")
|
|
|
|
|
|
return None, e
|
2017-08-08 15:04:45 +01:00
|
|
|
|
else:
|
|
|
|
|
|
raise e
|
2017-08-08 11:13:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _create_example_template(service_id):
|
|
|
|
|
|
example_sms_template = service_api_client.create_service_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Example text message template",
|
|
|
|
|
|
"sms",
|
|
|
|
|
|
"Hi, I’m trying out Notify.gov. Today is ((day of week)) and my favorite color is ((color)).",
|
2017-08-08 11:13:08 +01:00
|
|
|
|
service_id,
|
|
|
|
|
|
)
|
|
|
|
|
|
return example_sms_template
|
2016-10-28 10:47:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
@main.route("/add-service", methods=["GET", "POST"])
|
2018-12-12 13:10:46 +00:00
|
|
|
|
@user_is_gov_user
|
2023-11-14 07:51:56 -08:00
|
|
|
|
@user_is_logged_in
|
2015-12-14 17:12:28 +00:00
|
|
|
|
def add_service():
|
2023-07-12 12:09:44 -04:00
|
|
|
|
default_organization_type = current_user.default_organization_type
|
2023-09-19 14:30:32 -07:00
|
|
|
|
if default_organization_type is None:
|
|
|
|
|
|
default_organization_type = "federal"
|
2022-09-12 16:30:15 +00:00
|
|
|
|
form = CreateServiceForm(
|
2023-09-19 14:30:32 -07:00
|
|
|
|
# This value is currently not useful but if it is not set it will result in a bug
|
|
|
|
|
|
organization_type=default_organization_type
|
2022-09-12 16:30:15 +00:00
|
|
|
|
)
|
2016-10-28 10:47:32 +01:00
|
|
|
|
|
2015-12-14 17:12:28 +00:00
|
|
|
|
if form.validate_on_submit():
|
2017-08-08 11:13:08 +01:00
|
|
|
|
email_from = email_safe(form.name.data)
|
|
|
|
|
|
service_name = form.name.data
|
|
|
|
|
|
|
2019-04-18 12:44:18 +01:00
|
|
|
|
service_id, error = _create_service(
|
|
|
|
|
|
service_name,
|
2023-07-12 12:09:44 -04:00
|
|
|
|
default_organization_type or form.organization_type.data,
|
2019-04-18 12:44:18 +01:00
|
|
|
|
email_from,
|
|
|
|
|
|
form,
|
|
|
|
|
|
)
|
2017-08-08 11:13:08 +01:00
|
|
|
|
if error:
|
2023-07-12 12:09:44 -04:00
|
|
|
|
return _render_add_service_page(form, default_organization_type)
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if (
|
|
|
|
|
|
len(
|
|
|
|
|
|
service_api_client.get_active_services(
|
|
|
|
|
|
{"user_id": session["user_id"]}
|
|
|
|
|
|
).get("data", [])
|
|
|
|
|
|
)
|
|
|
|
|
|
> 1
|
|
|
|
|
|
):
|
|
|
|
|
|
return redirect(url_for("main.service_dashboard", service_id=service_id))
|
2016-05-20 12:05:05 +01:00
|
|
|
|
|
2017-08-08 11:13:08 +01:00
|
|
|
|
example_sms_template = _create_example_template(service_id)
|
2016-05-20 12:05:05 +01:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return redirect(
|
|
|
|
|
|
url_for(
|
|
|
|
|
|
"main.begin_tour",
|
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
|
template_id=example_sms_template["data"]["id"],
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
2016-02-18 14:54:59 +00:00
|
|
|
|
else:
|
2023-07-12 12:09:44 -04:00
|
|
|
|
return _render_add_service_page(form, default_organization_type)
|
2021-09-16 12:07:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
|
def _render_add_service_page(form, default_organization_type):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
heading = "About your service"
|
2020-07-01 12:16:32 +01:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if default_organization_type == "local":
|
2016-10-28 10:47:32 +01:00
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"views/add-service-local.html",
|
2016-10-28 10:47:32 +01:00
|
|
|
|
form=form,
|
2019-07-16 15:37:27 +01:00
|
|
|
|
heading=heading,
|
2023-07-12 12:09:44 -04:00
|
|
|
|
default_organization_type=default_organization_type,
|
2016-10-28 10:47:32 +01:00
|
|
|
|
)
|
2021-09-16 12:07:03 +01:00
|
|
|
|
|
|
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"views/add-service.html",
|
2021-09-16 12:07:03 +01:00
|
|
|
|
form=form,
|
|
|
|
|
|
heading=heading,
|
2023-07-12 12:09:44 -04:00
|
|
|
|
default_organization_type=default_organization_type,
|
2021-09-16 12:07:03 +01:00
|
|
|
|
)
|