mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-22 08:21:13 -05:00
improved service whitelist endpoints
* changed POST to PUT - we are modifiying an already present resource * improved error handling on PUT - return 400 if bad - rollback the delete of the previous whitelist on error * return 204 if PUT succeeds ( NO CONTENT )
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import itertools
|
import itertools
|
||||||
from functools import wraps, partial
|
from functools import wraps, partial
|
||||||
|
|
||||||
|
from app import db
|
||||||
from app.history_meta import create_history
|
from app.history_meta import create_history
|
||||||
|
|
||||||
|
|
||||||
@@ -35,3 +36,7 @@ def version_class(model_class, history_cls=None):
|
|||||||
db.session.add(h_obj)
|
db.session.add(h_obj)
|
||||||
return record_version
|
return record_version
|
||||||
return versioned
|
return versioned
|
||||||
|
|
||||||
|
|
||||||
|
def dao_rollback():
|
||||||
|
db.session.rollback()
|
||||||
|
|||||||
@@ -160,7 +160,7 @@ class ServiceWhitelist(db.Model):
|
|||||||
validate_phone_number(contact)
|
validate_phone_number(contact)
|
||||||
instance.mobile_number = contact
|
instance.mobile_number = contact
|
||||||
except InvalidPhoneError:
|
except InvalidPhoneError:
|
||||||
raise ValueError("Invalid contact: {}".format(contact))
|
raise ValueError('Invalid whitelist: "{}"'.format(contact))
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from flask import (
|
|||||||
)
|
)
|
||||||
from sqlalchemy.orm.exc import NoResultFound
|
from sqlalchemy.orm.exc import NoResultFound
|
||||||
|
|
||||||
|
from app.dao.dao_utils import dao_rollback
|
||||||
from app.dao.api_key_dao import (
|
from app.dao.api_key_dao import (
|
||||||
save_model_api_key,
|
save_model_api_key,
|
||||||
get_model_api_keys,
|
get_model_api_keys,
|
||||||
@@ -49,6 +50,7 @@ from app.errors import (
|
|||||||
InvalidRequest
|
InvalidRequest
|
||||||
)
|
)
|
||||||
from app.service import statistics
|
from app.service import statistics
|
||||||
|
from app.models import ServiceWhitelist
|
||||||
|
|
||||||
|
|
||||||
service_blueprint = Blueprint('service', __name__)
|
service_blueprint = Blueprint('service', __name__)
|
||||||
@@ -279,16 +281,24 @@ def get_detailed_services():
|
|||||||
def get_whitelist(service_id):
|
def get_whitelist(service_id):
|
||||||
whitelist = dao_fetch_service_whitelist(service_id)
|
whitelist = dao_fetch_service_whitelist(service_id)
|
||||||
|
|
||||||
return {
|
return jsonify(
|
||||||
'emails': [item.email_address for item in whitelist if item.email_address is not None],
|
email_addresses=[item.email_address for item in whitelist if item.email_address is not None],
|
||||||
'mobile_numbers': [item.mobile_number for item in whitelist if item.mobile_number is not None]
|
mobile_numbers=[item.mobile_number for item in whitelist if item.mobile_number is not None]
|
||||||
}
|
)
|
||||||
|
|
||||||
|
|
||||||
@service_blueprint.route('/<uuid:service_id>/whitelist', methods=['POST'])
|
@service_blueprint.route('/<uuid:service_id>/whitelist', methods=['PUT'])
|
||||||
def update_whitelist(service_id):
|
def update_whitelist(service_id):
|
||||||
|
# doesn't commit so if there are any errors, we preserve old values in db
|
||||||
dao_remove_service_whitelist(service_id)
|
dao_remove_service_whitelist(service_id)
|
||||||
|
|
||||||
whitelist_objs = [ServiceWhitelist.from_string(service_id, contact) for contact in request.get_json()]
|
try:
|
||||||
|
whitelist_objs = [ServiceWhitelist.from_string(service_id, contact) for contact in request.get_json()]
|
||||||
dao_add_and_commit_whitelisted_contacts(whitelist_objs)
|
except ValueError as e:
|
||||||
|
current_app.logger.exception(e)
|
||||||
|
dao_rollback()
|
||||||
|
msg = '{} is not a valid email address or phone number'.format(str(e))
|
||||||
|
return jsonify(result='error', message=msg), 400
|
||||||
|
else:
|
||||||
|
dao_add_and_commit_whitelisted_contacts(whitelist_objs)
|
||||||
|
return '', 204
|
||||||
|
|||||||
Reference in New Issue
Block a user