mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-18 21:49:37 -04:00
Let the API error when the service name is a duplicate.
- Remove the name_func from the AddServiceForm - Catch the HTTPError and set the error on the form
This commit is contained in:
@@ -4,7 +4,6 @@ import pytz
|
||||
from flask_wtf import FlaskForm as Form
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from notifications_python_client.errors import HTTPError
|
||||
from notifications_utils.recipients import (
|
||||
validate_phone_number,
|
||||
InvalidPhoneError
|
||||
@@ -223,28 +222,6 @@ class AddServiceForm(Form):
|
||||
]
|
||||
)
|
||||
|
||||
service_id = HiddenField('service_id')
|
||||
|
||||
def _create_service(self, service_name, email_from):
|
||||
from app import service_api_client
|
||||
from flask import current_app, session
|
||||
service_id = service_api_client.create_service(service_name=service_name,
|
||||
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
|
||||
|
||||
def validate_name(self, a):
|
||||
from app.utils import email_safe
|
||||
email_from = email_safe(a.data)
|
||||
try:
|
||||
self.service_id = self._create_service(a.data, email_from)
|
||||
except HTTPError as e:
|
||||
if e.status_code == 400 and e.message['name']:
|
||||
raise ValidationError(message=e.message['name'][0])
|
||||
|
||||
|
||||
class ServiceNameForm(Form):
|
||||
def __init__(self, names_func, *args, **kwargs):
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
from flask import (
|
||||
render_template,
|
||||
redirect,
|
||||
@@ -11,6 +10,7 @@ from flask_login import (
|
||||
current_user,
|
||||
login_required
|
||||
)
|
||||
from notifications_python_client.errors import HTTPError
|
||||
from werkzeug.exceptions import abort
|
||||
|
||||
from app.main import main
|
||||
@@ -39,14 +39,30 @@ def _add_invited_user_to_service(invited_user):
|
||||
return service_id
|
||||
|
||||
|
||||
def _create_service(service_name, email_from):
|
||||
service_id = service_api_client.create_service(service_name=service_name,
|
||||
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
|
||||
def _create_service(service_name, email_from, form):
|
||||
try:
|
||||
service_id = service_api_client.create_service(service_name=service_name,
|
||||
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
|
||||
|
||||
|
||||
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,
|
||||
process_type='priority',
|
||||
)
|
||||
return example_sms_template
|
||||
|
||||
|
||||
@main.route("/add-service", methods=['GET', 'POST'])
|
||||
@@ -64,20 +80,16 @@ def add_service():
|
||||
heading = 'Which service do you want to set up notifications for?'
|
||||
|
||||
if form.validate_on_submit():
|
||||
# email_from = email_safe(form.name.data)
|
||||
# service_name = form.name.data
|
||||
# service_id = _create_service(service_name, email_from)
|
||||
service_id = form.service_id
|
||||
email_from = email_safe(form.name.data)
|
||||
service_name = form.name.data
|
||||
|
||||
service_id, error = _create_service(service_name, 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 = 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,
|
||||
process_type='priority',
|
||||
)
|
||||
example_sms_template = _create_example_template(service_id)
|
||||
|
||||
return redirect(url_for(
|
||||
'main.start_tour',
|
||||
|
||||
Reference in New Issue
Block a user