Files
notifications-admin/app/main/views/add_service.py
Chris Hill-Scott 01fdcb8998 Use NHS.UK branding is for all NHS services
Anyone choosing ‘NHS’ for their organisation type gets should get the
NHS branding. We don’t want to hard-code an ID for NHS branding anywhere
because it won’t be consistent between environments.

So instead we can say that anyone who chooses ‘NHS’ as their
organisation type should get whatever branding has `nhs.uk` as its
domain.

This allows us to easily manage the branding the same way we do other
brands, but gives us the efficiency of having it auto applied.
2018-09-05 10:32:44 +01:00

107 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from flask import current_app, redirect, render_template, session, url_for
from flask_login import current_user, login_required
from notifications_python_client.errors import HTTPError
from werkzeug.exceptions import abort
from app import (
billing_api_client,
email_branding_client,
invite_api_client,
service_api_client,
user_api_client,
)
from app.main import main
from app.main.forms import CreateServiceForm
from app.notify_client.models import InvitedUser
from app.utils import AgreementInfo, email_safe, is_gov_user
def _add_invited_user_to_service(invited_user):
invitation = InvitedUser(**invited_user)
# if invited user add to service and redirect to dashboard
user = user_api_client.get_user(session['user_id'])
service_id = invited_user['service']
user_api_client.add_user_to_service(service_id, user.id, invitation.permissions)
invite_api_client.accept_invite(service_id, invitation.id)
return service_id
def _create_service(service_name, organisation_type, email_from, form):
free_sms_fragment_limit = current_app.config['DEFAULT_FREE_SMS_FRAGMENT_LIMITS'].get(organisation_type)
email_branding = email_branding_client.get_email_branding_id_for_domain(
'nhs.uk' if organisation_type == 'nhs' else
AgreementInfo.from_current_user().canonical_domain
)
try:
service_id = service_api_client.create_service(
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,
)
session['service_id'] = service_id
if email_branding:
service_api_client.update_service(service_id, email_branding=email_branding)
billing_api_client.create_or_update_free_sms_fragment_limit(service_id, free_sms_fragment_limit)
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
else:
raise e
def _create_example_template(service_id):
example_sms_template = service_api_client.create_service_template(
'Example text message template',
'sms',
'Hey ((name)), Im trying out Notify. Today is ((day of week)) and my favourite colour is ((colour)).',
service_id,
)
return example_sms_template
@main.route("/add-service", methods=['GET', 'POST'])
@login_required
def add_service():
invited_user = session.get('invited_user')
if invited_user:
service_id = _add_invited_user_to_service(invited_user)
return redirect(url_for('main.service_dashboard', service_id=service_id))
if not is_gov_user(current_user.email_address):
abort(403)
form = CreateServiceForm()
heading = 'About your service'
if form.validate_on_submit():
email_from = email_safe(form.name.data)
service_name = form.name.data
service_id, error = _create_service(service_name, form.organisation_type.data, email_from, form)
if error:
return render_template('views/add-service.html', form=form, heading=heading)
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.start_tour',
service_id=service_id,
template_id=example_sms_template['data']['id'],
))
else:
return render_template(
'views/add-service.html',
form=form,
heading=heading
)