Created a new service endpoint that checks if the service name or email_from is unique.

Why is this needed?
When a user updates a service name they enter the new name in a form, are then asked to confirm the change by entering their password. Then the API call to update_service is called. If we let the update serivce API call fail with the integrity constraint it will be ackward for the user.
This commit is contained in:
Rebecca Law
2017-08-09 15:12:52 +01:00
parent 14dd18aefc
commit 1ec6a3b73d
2 changed files with 57 additions and 7 deletions

View File

@@ -596,3 +596,27 @@ def handle_sql_errror(e):
def create_one_off_notification(service_id):
resp = send_one_off_notification(service_id, request.get_json())
return jsonify(resp), 201
@service_blueprint.route('/unique', methods=["GET"])
def is_service_name_unique():
name, email_from = check_request_args(request)
name_exists = Service.query.filter_by(name=name).first()
email_from_exists = Service.query.filter_by(email_from=email_from).first()
if name_exists or email_from_exists:
return jsonify(result=False), 200
return jsonify(result=True), 200
def check_request_args(request):
name = request.args.get('name', None)
email_from = request.args.get('email_from', None)
errors = []
if not name:
errors.append({'name': ["Can't be empty"]})
if not email_from:
errors.append({'email_from': ["Can't be empty"]})
if errors:
raise InvalidRequest(errors, status_code=400)
return name, email_from