Let the API return a 400 error message if the service name is a duplicate.
This commit is contained in:
Rebecca Law
2017-08-07 11:30:25 +01:00
parent 8b313ed8d1
commit 186202cc9d
5 changed files with 47 additions and 37 deletions

View File

@@ -3,6 +3,8 @@ 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
@@ -211,13 +213,7 @@ class TextNotReceivedForm(Form):
class AddServiceForm(Form):
def __init__(self, names_func, *args, **kwargs):
"""
Keyword arguments:
names_func -- Returns a list of unique service_names already registered
on the system.
"""
self._names_func = names_func
def __init__(self, *args, **kwargs):
super(AddServiceForm, self).__init__(*args, **kwargs)
name = StringField(
@@ -227,11 +223,27 @@ 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
# make sure the email_from will be unique to all services
if email_safe(a.data) in self._names_func():
raise ValidationError('This service name is already in use')
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):