mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-16 20:49:00 -04:00
[WIP]
Let the API return a 400 error message if the service name is a duplicate.
This commit is contained in:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user