Files
notifications-admin/app/main/views/add_service.py

107 lines
3.4 KiB
Python
Raw Permalink Normal View History

from flask import current_app, redirect, render_template, session, url_for
from flask_login import current_user
from notifications_python_client.errors import HTTPError
from app import service_api_client
from app.formatters import email_safe
from app.main import main
from app.main.forms import CreateServiceForm
from app.utils.user import user_is_gov_user, user_is_logged_in
2023-07-12 12:09:44 -04:00
def _create_service(service_name, organization_type, email_from, form):
try:
service_id = service_api_client.create_service(
service_name=service_name,
2023-07-12 12:09:44 -04:00
organization_type=organization_type,
message_limit=current_app.config["DEFAULT_SERVICE_LIMIT"],
restricted=True,
user_id=session["user_id"],
email_from=email_from,
)
session["service_id"] = service_id
return service_id, None
except HTTPError as e:
if e.status_code == 400 and e.message["name"]:
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
def _create_example_template(service_id):
example_sms_template = service_api_client.create_service_template(
"Example text message template",
"sms",
"Hi, Im trying out Notify.gov. Today is ((day of week)) and my favorite color is ((color)).",
service_id,
)
return example_sms_template
2016-10-28 10:47:32 +01:00
@main.route("/add-service", methods=["GET", "POST"])
@user_is_gov_user
@user_is_logged_in
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"
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
)
2016-10-28 10:47:32 +01:00
if form.validate_on_submit():
email_from = email_safe(form.name.data)
service_name = form.name.data
service_id, error = _create_service(
service_name,
2023-07-12 12:09:44 -04:00
default_organization_type or form.organization_type.data,
email_from,
form,
)
if error:
2023-07-12 12:09:44 -04:00
return _render_add_service_page(form, default_organization_type)
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))
example_sms_template = _create_example_template(service_id)
return redirect(
url_for(
"main.begin_tour",
service_id=service_id,
template_id=example_sms_template["data"]["id"],
)
)
else:
2023-07-12 12:09:44 -04:00
return _render_add_service_page(form, default_organization_type)
2023-07-12 12:09:44 -04:00
def _render_add_service_page(form, default_organization_type):
heading = "About your service"
if default_organization_type == "local":
2016-10-28 10:47:32 +01:00
return render_template(
"views/add-service-local.html",
2016-10-28 10:47:32 +01:00
form=form,
heading=heading,
2023-07-12 12:09:44 -04:00
default_organization_type=default_organization_type,
2016-10-28 10:47:32 +01:00
)
return render_template(
"views/add-service.html",
form=form,
heading=heading,
2023-07-12 12:09:44 -04:00
default_organization_type=default_organization_type,
)