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
|
|
|
|
|
2019-04-04 11:15:42 +01:00
|
|
|
|
from app import billing_api_client, service_api_client
|
2018-02-20 11:22:17 +00:00
|
|
|
|
from app.main import main
|
2019-07-16 15:37:27 +01:00
|
|
|
|
from app.main.forms import CreateNhsServiceForm, CreateServiceForm
|
2019-07-01 15:22:08 +01:00
|
|
|
|
from app.utils import email_safe, user_is_gov_user, user_is_logged_in
|
2015-12-14 17:12:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-10-04 11:49:32 +01:00
|
|
|
|
def _create_service(service_name, organisation_type, email_from, form):
|
2017-10-31 11:22:57 +00:00
|
|
|
|
free_sms_fragment_limit = current_app.config['DEFAULT_FREE_SMS_FRAGMENT_LIMITS'].get(organisation_type)
|
2019-02-12 14:02:21 +00:00
|
|
|
|
|
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,
|
|
|
|
|
|
organisation_type=organisation_type,
|
|
|
|
|
|
message_limit=current_app.config['DEFAULT_SERVICE_LIMIT'],
|
|
|
|
|
|
restricted=True,
|
|
|
|
|
|
user_id=session['user_id'],
|
|
|
|
|
|
email_from=email_from,
|
2017-10-04 11:49:32 +01:00
|
|
|
|
)
|
2017-08-08 11:13:08 +01:00
|
|
|
|
session['service_id'] = service_id
|
2017-10-31 11:22:57 +00:00
|
|
|
|
|
2017-12-04 16:03:11 +00:00
|
|
|
|
billing_api_client.create_or_update_free_sms_fragment_limit(service_id, free_sms_fragment_limit)
|
2017-10-31 11:22:57 +00:00
|
|
|
|
|
2017-08-08 11:13:08 +01:00
|
|
|
|
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
|
2017-08-08 11:13:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _create_example_template(service_id):
|
|
|
|
|
|
example_sms_template = service_api_client.create_service_template(
|
|
|
|
|
|
'Example text message template',
|
|
|
|
|
|
'sms',
|
|
|
|
|
|
'Hey ((name)), I’m trying out Notify. Today is ((day of week)) and my favourite colour is ((colour)).',
|
|
|
|
|
|
service_id,
|
|
|
|
|
|
)
|
|
|
|
|
|
return example_sms_template
|
2016-10-28 10:47:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-01-04 15:31:50 +00:00
|
|
|
|
@main.route("/add-service", methods=['GET', 'POST'])
|
2019-07-01 15:22:08 +01:00
|
|
|
|
@user_is_logged_in
|
2018-12-12 13:10:46 +00:00
|
|
|
|
@user_is_gov_user
|
2015-12-14 17:12:28 +00:00
|
|
|
|
def add_service():
|
2019-07-16 15:37:27 +01:00
|
|
|
|
default_organisation_type = current_user.default_organisation_type
|
|
|
|
|
|
if default_organisation_type == 'nhs':
|
|
|
|
|
|
form = CreateNhsServiceForm()
|
|
|
|
|
|
default_organisation_type = None
|
|
|
|
|
|
else:
|
|
|
|
|
|
form = CreateServiceForm(
|
|
|
|
|
|
organisation_type=default_organisation_type
|
|
|
|
|
|
)
|
2017-10-04 11:49:32 +01:00
|
|
|
|
heading = 'About your service'
|
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,
|
2019-07-16 15:37:27 +01:00
|
|
|
|
default_organisation_type or form.organisation_type.data,
|
2019-04-18 12:44:18 +01:00
|
|
|
|
email_from,
|
|
|
|
|
|
form,
|
|
|
|
|
|
)
|
2017-08-08 11:13:08 +01:00
|
|
|
|
if error:
|
|
|
|
|
|
return render_template('views/add-service.html', form=form, heading=heading)
|
2017-08-07 11:30:25 +01:00
|
|
|
|
if len(service_api_client.get_active_services({'user_id': session['user_id']}).get('data', [])) > 1:
|
2016-04-28 16:44:12 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
return redirect(url_for(
|
2017-06-29 10:02:53 +01:00
|
|
|
|
'main.start_tour',
|
2016-05-20 12:05:05 +01:00
|
|
|
|
service_id=service_id,
|
2019-07-16 15:37:27 +01:00
|
|
|
|
template_id=example_sms_template['data']['id']
|
2016-05-20 12:05:05 +01:00
|
|
|
|
))
|
2016-02-18 14:54:59 +00:00
|
|
|
|
else:
|
2020-07-01 11:49:11 +01:00
|
|
|
|
if default_organisation_type == 'local':
|
|
|
|
|
|
return render_template(
|
|
|
|
|
|
'views/add-service-local.html',
|
|
|
|
|
|
form=form,
|
|
|
|
|
|
heading=heading,
|
|
|
|
|
|
default_organisation_type=default_organisation_type,
|
|
|
|
|
|
)
|
2020-07-01 12:16:32 +01:00
|
|
|
|
|
2016-10-28 10:47:32 +01:00
|
|
|
|
return render_template(
|
|
|
|
|
|
'views/add-service.html',
|
|
|
|
|
|
form=form,
|
2019-07-16 15:37:27 +01:00
|
|
|
|
heading=heading,
|
|
|
|
|
|
default_organisation_type=default_organisation_type,
|
2016-10-28 10:47:32 +01:00
|
|
|
|
)
|