Add check org name is unique endpoint

This commit is contained in:
chrisw
2018-03-06 12:49:46 +00:00
parent d7823fdbd6
commit a487293bf0
2 changed files with 122 additions and 0 deletions

View File

@@ -107,3 +107,26 @@ def get_organisation_users(organisation_id):
result = user_schema.dump(org_users, many=True)
return jsonify(data=result.data)
@organisation_blueprint.route('/unique', methods=["GET"])
def is_organisation_name_unique():
organisation_id, name = check_request_args(request)
name_exists = Organisation.query.filter(Organisation.name.ilike(name)).first()
result = (not name_exists) or str(name_exists.id) == organisation_id
return jsonify(result=result), 200
def check_request_args(request):
org_id = request.args.get('org_id')
name = request.args.get('name', None)
errors = []
if not org_id:
errors.append({'org_id': ["Can't be empty"]})
if not name:
errors.append({'name': ["Can't be empty"]})
if errors:
raise InvalidRequest(errors, status_code=400)
return org_id, name