2016-04-26 13:31:57 +01:00
|
|
|
|
import requests
|
2016-01-18 17:35:28 +00:00
|
|
|
|
from flask import (
|
2016-03-09 14:30:50 +00:00
|
|
|
|
render_template,
|
|
|
|
|
|
redirect,
|
|
|
|
|
|
request,
|
|
|
|
|
|
url_for,
|
|
|
|
|
|
session,
|
2016-04-26 13:31:57 +01:00
|
|
|
|
flash,
|
|
|
|
|
|
abort,
|
|
|
|
|
|
current_app
|
2016-03-09 14:30:50 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
from flask_login import (
|
|
|
|
|
|
login_required,
|
|
|
|
|
|
current_user
|
|
|
|
|
|
)
|
2016-04-15 11:04:35 +01:00
|
|
|
|
from notifications_python_client.errors import HTTPError
|
2016-03-09 14:30:50 +00:00
|
|
|
|
|
2016-03-29 22:50:40 +01:00
|
|
|
|
from app import service_api_client
|
2016-01-07 12:29:56 +00:00
|
|
|
|
from app.main import main
|
2016-03-30 17:12:00 +01:00
|
|
|
|
from app.utils import user_has_permissions, email_safe
|
2016-05-16 13:09:58 +01:00
|
|
|
|
from app.main.forms import (
|
|
|
|
|
|
ConfirmPasswordForm,
|
|
|
|
|
|
ServiceNameForm,
|
|
|
|
|
|
RequestToGoLiveForm,
|
2016-07-01 13:47:22 +01:00
|
|
|
|
ServiceReplyToEmailFrom,
|
2016-08-08 10:28:40 +01:00
|
|
|
|
ServiceSmsSender,
|
|
|
|
|
|
ServiceBrandingOrg
|
2016-05-16 13:09:58 +01:00
|
|
|
|
)
|
2016-08-08 10:28:40 +01:00
|
|
|
|
from app import user_api_client, current_service, organisations_client
|
2016-01-07 12:29:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-02-02 14:24:08 +00:00
|
|
|
|
@main.route("/services/<service_id>/service-settings")
|
2016-01-11 16:03:41 +00:00
|
|
|
|
@login_required
|
2016-03-17 10:46:47 +00:00
|
|
|
|
@user_has_permissions('manage_settings', admin_override=True)
|
2016-01-13 12:10:29 +00:00
|
|
|
|
def service_settings(service_id):
|
2016-08-24 09:34:14 +01:00
|
|
|
|
if current_service['organisation']:
|
|
|
|
|
|
organisation = organisations_client.get_organisation(current_service['organisation'])['organisation']
|
|
|
|
|
|
else:
|
|
|
|
|
|
organisation = None
|
|
|
|
|
|
return render_template(
|
|
|
|
|
|
'views/service-settings.html',
|
|
|
|
|
|
organisation=organisation
|
|
|
|
|
|
)
|
2016-01-07 12:29:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-02-02 14:24:08 +00:00
|
|
|
|
@main.route("/services/<service_id>/service-settings/name", methods=['GET', 'POST'])
|
2016-01-11 16:03:41 +00:00
|
|
|
|
@login_required
|
2016-03-17 10:46:47 +00:00
|
|
|
|
@user_has_permissions('manage_settings', admin_override=True)
|
2016-01-18 16:01:04 +00:00
|
|
|
|
def service_name_change(service_id):
|
2016-03-31 15:17:05 +01:00
|
|
|
|
form = ServiceNameForm(service_api_client.find_all_service_email_from)
|
2016-01-11 13:15:10 +00:00
|
|
|
|
|
2016-06-20 13:33:29 +01:00
|
|
|
|
if request.method == 'GET':
|
|
|
|
|
|
form.name.data = current_service.get('name')
|
|
|
|
|
|
|
2016-01-18 16:01:04 +00:00
|
|
|
|
if form.validate_on_submit():
|
2016-01-18 17:35:28 +00:00
|
|
|
|
session['service_name_change'] = form.name.data
|
2016-01-18 16:01:04 +00:00
|
|
|
|
return redirect(url_for('.service_name_change_confirm', service_id=service_id))
|
|
|
|
|
|
|
|
|
|
|
|
return render_template(
|
2016-01-18 17:35:28 +00:00
|
|
|
|
'views/service-settings/name.html',
|
2016-04-04 16:53:52 +01:00
|
|
|
|
form=form)
|
2016-01-07 16:24:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-02-02 14:24:08 +00:00
|
|
|
|
@main.route("/services/<service_id>/service-settings/name/confirm", methods=['GET', 'POST'])
|
2016-01-11 16:03:41 +00:00
|
|
|
|
@login_required
|
2016-03-17 10:46:47 +00:00
|
|
|
|
@user_has_permissions('manage_settings', admin_override=True)
|
2016-01-18 16:01:04 +00:00
|
|
|
|
def service_name_change_confirm(service_id):
|
2016-01-22 16:34:36 +00:00
|
|
|
|
# Validate password for form
|
|
|
|
|
|
def _check_password(pwd):
|
2016-03-30 09:58:10 +01:00
|
|
|
|
return user_api_client.verify_password(current_user.id, pwd)
|
2016-06-01 16:07:43 +01:00
|
|
|
|
|
2016-01-22 16:34:36 +00:00
|
|
|
|
form = ConfirmPasswordForm(_check_password)
|
2016-01-11 13:15:10 +00:00
|
|
|
|
|
2016-01-18 16:01:04 +00:00
|
|
|
|
if form.validate_on_submit():
|
2016-03-10 14:29:31 +00:00
|
|
|
|
try:
|
2016-03-29 22:50:40 +01:00
|
|
|
|
service_api_client.update_service(
|
2016-04-04 16:53:52 +01:00
|
|
|
|
current_service['id'],
|
2016-08-11 12:32:38 +01:00
|
|
|
|
name=session['service_name_change'],
|
|
|
|
|
|
email_from=email_safe(session['service_name_change'])
|
|
|
|
|
|
)
|
2016-03-10 14:29:31 +00:00
|
|
|
|
except HTTPError as e:
|
|
|
|
|
|
error_msg = "Duplicate service name '{}'".format(session['service_name_change'])
|
|
|
|
|
|
if e.status_code == 400 and error_msg in e.message['name']:
|
|
|
|
|
|
# Redirect the user back to the change service name screen
|
|
|
|
|
|
flash('This service name is already in use', 'error')
|
|
|
|
|
|
return redirect(url_for('main.service_name_change', service_id=service_id))
|
|
|
|
|
|
else:
|
|
|
|
|
|
raise e
|
|
|
|
|
|
else:
|
|
|
|
|
|
session.pop('service_name_change')
|
|
|
|
|
|
return redirect(url_for('.service_settings', service_id=service_id))
|
2016-01-18 16:01:04 +00:00
|
|
|
|
return render_template(
|
2016-01-18 17:35:28 +00:00
|
|
|
|
'views/service-settings/confirm.html',
|
|
|
|
|
|
heading='Change your service name',
|
2016-04-04 16:53:52 +01:00
|
|
|
|
form=form)
|
2016-01-07 12:29:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-02-02 14:24:08 +00:00
|
|
|
|
@main.route("/services/<service_id>/service-settings/request-to-go-live", methods=['GET', 'POST'])
|
2016-01-11 16:03:41 +00:00
|
|
|
|
@login_required
|
2016-03-17 10:46:47 +00:00
|
|
|
|
@user_has_permissions('manage_settings', admin_override=True)
|
2016-01-18 16:01:04 +00:00
|
|
|
|
def service_request_to_go_live(service_id):
|
2016-04-26 13:31:57 +01:00
|
|
|
|
form = RequestToGoLiveForm()
|
|
|
|
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
2016-08-04 17:02:21 +01:00
|
|
|
|
'person_email': current_user.email_address,
|
|
|
|
|
|
'person_name': current_user.name,
|
2016-05-18 15:03:23 +01:00
|
|
|
|
'department_id': current_app.config.get('DESKPRO_DEPT_ID'),
|
2016-05-20 15:58:20 +01:00
|
|
|
|
'agent_team_id': current_app.config.get('DESKPRO_ASSIGNED_AGENT_TEAM_ID'),
|
2016-04-26 13:31:57 +01:00
|
|
|
|
'subject': 'Request to go live',
|
2016-10-24 15:35:51 +01:00
|
|
|
|
'message': (
|
|
|
|
|
|
'On behalf of {} ({})\n\nExpected usage\n---'
|
2016-11-01 11:31:39 +00:00
|
|
|
|
'\nMOU in place: {}'
|
2016-10-24 15:35:51 +01:00
|
|
|
|
'\nChannel: {}\nStart date: {}\nStart volume: {}'
|
|
|
|
|
|
'\nPeak volume: {}\nUpload or API: {}'
|
|
|
|
|
|
).format(
|
2016-04-26 13:31:57 +01:00
|
|
|
|
current_service['name'],
|
2016-04-26 16:28:16 +01:00
|
|
|
|
url_for('main.service_dashboard', service_id=current_service['id'], _external=True),
|
2016-11-01 11:31:39 +00:00
|
|
|
|
form.mou.data,
|
2016-10-24 13:01:23 +01:00
|
|
|
|
form.channel.data,
|
|
|
|
|
|
form.start_date.data,
|
|
|
|
|
|
form.start_volume.data,
|
|
|
|
|
|
form.peak_volume.data,
|
|
|
|
|
|
form.upload_or_api.data
|
2016-10-24 13:38:55 +01:00
|
|
|
|
|
2016-04-26 13:31:57 +01:00
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
headers = {
|
|
|
|
|
|
"X-DeskPRO-API-Key": current_app.config.get('DESKPRO_API_KEY'),
|
|
|
|
|
|
'Content-Type': "application/x-www-form-urlencoded"
|
|
|
|
|
|
}
|
|
|
|
|
|
resp = requests.post(
|
|
|
|
|
|
current_app.config.get('DESKPRO_API_HOST') + '/api/tickets',
|
|
|
|
|
|
data=data,
|
|
|
|
|
|
headers=headers
|
2016-01-07 12:29:56 +00:00
|
|
|
|
)
|
2016-04-26 13:31:57 +01:00
|
|
|
|
if resp.status_code != 201:
|
|
|
|
|
|
current_app.logger.error(
|
|
|
|
|
|
"Deskpro create ticket request failed with {} '{}'".format(
|
|
|
|
|
|
resp.status_code,
|
|
|
|
|
|
resp.json())
|
2016-06-01 16:07:43 +01:00
|
|
|
|
)
|
2016-04-26 13:31:57 +01:00
|
|
|
|
abort(500, "Request to go live submission failed")
|
|
|
|
|
|
|
|
|
|
|
|
flash('We’ve received your request to go live', 'default')
|
2016-01-13 12:10:29 +00:00
|
|
|
|
return redirect(url_for('.service_settings', service_id=service_id))
|
2016-01-07 12:29:56 +00:00
|
|
|
|
|
2016-04-26 13:31:57 +01:00
|
|
|
|
return render_template('views/service-settings/request-to-go-live.html', form=form)
|
|
|
|
|
|
|
2016-01-07 12:29:56 +00:00
|
|
|
|
|
2016-04-14 15:38:27 +01:00
|
|
|
|
@main.route("/services/<service_id>/service-settings/switch-live")
|
|
|
|
|
|
@login_required
|
|
|
|
|
|
@user_has_permissions(admin_override=True)
|
|
|
|
|
|
def service_switch_live(service_id):
|
|
|
|
|
|
service_api_client.update_service(
|
|
|
|
|
|
current_service['id'],
|
|
|
|
|
|
# TODO This limit should be set depending on the agreement signed by
|
|
|
|
|
|
# with Notify.
|
2016-08-11 12:32:38 +01:00
|
|
|
|
message_limit=250000 if current_service['restricted'] else 50,
|
|
|
|
|
|
restricted=(not current_service['restricted'])
|
|
|
|
|
|
)
|
2016-04-14 15:38:27 +01:00
|
|
|
|
return redirect(url_for('.service_settings', service_id=service_id))
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-01 16:07:43 +01:00
|
|
|
|
@main.route("/services/<service_id>/service-settings/research-mode")
|
|
|
|
|
|
@login_required
|
|
|
|
|
|
@user_has_permissions(admin_override=True)
|
|
|
|
|
|
def service_switch_research_mode(service_id):
|
|
|
|
|
|
service_api_client.update_service_with_properties(
|
|
|
|
|
|
service_id,
|
2016-10-26 16:56:51 +01:00
|
|
|
|
{"research_mode": not current_service['research_mode']}
|
|
|
|
|
|
)
|
|
|
|
|
|
return redirect(url_for('.service_settings', service_id=service_id))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route("/services/<service_id>/service-settings/can-send-letters")
|
|
|
|
|
|
@login_required
|
|
|
|
|
|
@user_has_permissions(admin_override=True)
|
|
|
|
|
|
def service_switch_can_send_letters(service_id):
|
|
|
|
|
|
service_api_client.update_service_with_properties(
|
|
|
|
|
|
service_id,
|
|
|
|
|
|
{"can_send_letters": not current_service['can_send_letters']}
|
2016-06-01 16:07:43 +01:00
|
|
|
|
)
|
|
|
|
|
|
return redirect(url_for('.service_settings', service_id=service_id))
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-11-02 16:53:40 +00:00
|
|
|
|
@main.route("/services/<service_id>/service-settings/deactivate", methods=['GET', 'POST'])
|
2016-01-11 16:03:41 +00:00
|
|
|
|
@login_required
|
2016-03-17 10:46:47 +00:00
|
|
|
|
@user_has_permissions('manage_settings', admin_override=True)
|
2016-11-02 16:53:40 +00:00
|
|
|
|
def deactivate_service(service_id):
|
2016-11-08 14:33:53 +00:00
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
|
service_api_client.deactivate_service(service_id)
|
|
|
|
|
|
return redirect(url_for('.service_settings', service_id=service_id))
|
|
|
|
|
|
else:
|
|
|
|
|
|
flash('There\'s no way to reverse this! Are you sure you want to archive this service?', 'delete')
|
|
|
|
|
|
return service_settings(service_id)
|
2016-05-16 13:09:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route("/services/<service_id>/service-settings/set-reply-to-email", methods=['GET', 'POST'])
|
|
|
|
|
|
@login_required
|
|
|
|
|
|
@user_has_permissions('manage_settings', admin_override=True)
|
|
|
|
|
|
def service_set_reply_to_email(service_id):
|
|
|
|
|
|
form = ServiceReplyToEmailFrom()
|
2016-05-17 11:34:49 +01:00
|
|
|
|
if request.method == 'GET':
|
|
|
|
|
|
form.email_address.data = current_service.get('reply_to_email_address')
|
2016-05-16 13:09:58 +01:00
|
|
|
|
if form.validate_on_submit():
|
|
|
|
|
|
service_api_client.update_service(
|
|
|
|
|
|
current_service['id'],
|
2016-08-11 12:32:38 +01:00
|
|
|
|
reply_to_email_address=form.email_address.data
|
|
|
|
|
|
)
|
2016-05-16 13:09:58 +01:00
|
|
|
|
return redirect(url_for('.service_settings', service_id=service_id))
|
|
|
|
|
|
return render_template(
|
|
|
|
|
|
'views/service-settings/set-reply-to-email.html',
|
|
|
|
|
|
form=form)
|
2016-07-01 13:47:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route("/services/<service_id>/service-settings/set-sms-sender", methods=['GET', 'POST'])
|
|
|
|
|
|
@login_required
|
|
|
|
|
|
@user_has_permissions('manage_settings', admin_override=True)
|
|
|
|
|
|
def service_set_sms_sender(service_id):
|
|
|
|
|
|
form = ServiceSmsSender()
|
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
|
|
form.sms_sender.data = current_service.get('sms_sender')
|
|
|
|
|
|
if form.validate_on_submit():
|
|
|
|
|
|
service_api_client.update_service(
|
|
|
|
|
|
current_service['id'],
|
2016-08-11 12:32:38 +01:00
|
|
|
|
sms_sender=form.sms_sender.data or None
|
|
|
|
|
|
)
|
2016-07-01 13:47:22 +01:00
|
|
|
|
return redirect(url_for('.service_settings', service_id=service_id))
|
|
|
|
|
|
return render_template(
|
|
|
|
|
|
'views/service-settings/set-sms-sender.html',
|
|
|
|
|
|
form=form)
|
2016-08-08 10:28:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route("/services/<service_id>/service-settings/set-branding-and-org", methods=['GET', 'POST'])
|
|
|
|
|
|
@login_required
|
|
|
|
|
|
@user_has_permissions(admin_override=True)
|
|
|
|
|
|
def service_set_branding_and_org(service_id):
|
|
|
|
|
|
organisations = organisations_client.get_organisations()
|
|
|
|
|
|
|
2016-08-12 11:48:04 +01:00
|
|
|
|
form = ServiceBrandingOrg(branding_type=current_service.get('branding'))
|
|
|
|
|
|
# dynamically create org choices, including the null option
|
2016-08-15 10:39:27 +01:00
|
|
|
|
form.organisation.choices = [('None', 'None')] + get_branding_as_value_and_label(organisations)
|
2016-08-08 10:28:40 +01:00
|
|
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2016-08-15 10:39:27 +01:00
|
|
|
|
organisation = None if form.organisation.data == 'None' else form.organisation.data
|
2016-08-08 10:28:40 +01:00
|
|
|
|
service_api_client.update_service(
|
|
|
|
|
|
service_id,
|
|
|
|
|
|
branding=form.branding_type.data,
|
2016-08-15 11:00:20 +01:00
|
|
|
|
organisation=organisation
|
2016-08-08 10:28:40 +01:00
|
|
|
|
)
|
|
|
|
|
|
return redirect(url_for('.service_settings', service_id=service_id))
|
2016-08-12 12:37:18 +01:00
|
|
|
|
|
2016-08-15 10:39:27 +01:00
|
|
|
|
form.organisation.data = current_service['organisation'] or 'None'
|
|
|
|
|
|
|
2016-08-08 10:28:40 +01:00
|
|
|
|
return render_template(
|
|
|
|
|
|
'views/service-settings/set-branding-and-org.html',
|
|
|
|
|
|
form=form,
|
|
|
|
|
|
branding_dict=get_branding_as_dict(organisations)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_branding_as_value_and_label(organisations):
|
|
|
|
|
|
return [
|
|
|
|
|
|
(organisation['id'], organisation['name'])
|
|
|
|
|
|
for organisation in organisations
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_branding_as_dict(organisations):
|
|
|
|
|
|
return {
|
|
|
|
|
|
organisation['id']: {
|
|
|
|
|
|
'logo': '/static/images/email-template/crests/{}'.format(organisation['logo']),
|
|
|
|
|
|
'colour': organisation['colour']
|
|
|
|
|
|
} for organisation in organisations
|
|
|
|
|
|
}
|