mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
notify-api-412 use black to enforce python style standards
This commit is contained in:
@@ -9,11 +9,13 @@ from app.errors import InvalidRequest, register_errors
|
||||
from app.models import INBOUND_SMS_TYPE, SMS_TYPE, InboundSms
|
||||
from app.notifications.sns_handlers import sns_notification_handler
|
||||
|
||||
receive_notifications_blueprint = Blueprint('receive_notifications', __name__)
|
||||
receive_notifications_blueprint = Blueprint("receive_notifications", __name__)
|
||||
register_errors(receive_notifications_blueprint)
|
||||
|
||||
|
||||
@receive_notifications_blueprint.route('/notifications/sms/receive/sns', methods=['POST'])
|
||||
@receive_notifications_blueprint.route(
|
||||
"/notifications/sms/receive/sns", methods=["POST"]
|
||||
)
|
||||
def receive_sns_sms():
|
||||
"""
|
||||
Expected value of the 'Message' key in the incoming payload from SNS
|
||||
@@ -28,10 +30,8 @@ def receive_sns_sms():
|
||||
"""
|
||||
|
||||
# Whether or not to ignore inbound SMS replies
|
||||
if not current_app.config['RECEIVE_INBOUND_SMS']:
|
||||
return jsonify(
|
||||
result="success", message="SMS-SNS callback succeeded"
|
||||
), 200
|
||||
if not current_app.config["RECEIVE_INBOUND_SMS"]:
|
||||
return jsonify(result="success", message="SMS-SNS callback succeeded"), 200
|
||||
|
||||
try:
|
||||
post_data = sns_notification_handler(request.data, request.headers)
|
||||
@@ -42,9 +42,9 @@ def receive_sns_sms():
|
||||
# TODO wrap this up
|
||||
if "inboundMessageId" in message:
|
||||
# TODO use standard formatting we use for all US numbers
|
||||
inbound_number = message['destinationNumber'].replace('+', '')
|
||||
inbound_number = message["destinationNumber"].replace("+", "")
|
||||
|
||||
service = fetch_potential_service(inbound_number, 'sns')
|
||||
service = fetch_potential_service(inbound_number, "sns")
|
||||
if not service:
|
||||
# since this is an issue with our service <-> number mapping, or no inbound_sms service permission
|
||||
# we should still tell SNS that we received it successfully
|
||||
@@ -52,42 +52,47 @@ def receive_sns_sms():
|
||||
f"Mapping between service and inbound number: {inbound_number} is broken, "
|
||||
f"or service does not have permission to receive inbound sms"
|
||||
)
|
||||
return jsonify(
|
||||
result="success", message="SMS-SNS callback succeeded"
|
||||
), 200
|
||||
return jsonify(result="success", message="SMS-SNS callback succeeded"), 200
|
||||
|
||||
content = message.get("messageBody")
|
||||
from_number = message.get('originationNumber')
|
||||
provider_ref = message.get('inboundMessageId')
|
||||
date_received = post_data.get('Timestamp')
|
||||
from_number = message.get("originationNumber")
|
||||
provider_ref = message.get("inboundMessageId")
|
||||
date_received = post_data.get("Timestamp")
|
||||
provider_name = "sns"
|
||||
|
||||
inbound = create_inbound_sms_object(service,
|
||||
content=content,
|
||||
from_number=from_number,
|
||||
provider_ref=provider_ref,
|
||||
date_received=date_received,
|
||||
provider_name=provider_name)
|
||||
inbound = create_inbound_sms_object(
|
||||
service,
|
||||
content=content,
|
||||
from_number=from_number,
|
||||
provider_ref=provider_ref,
|
||||
date_received=date_received,
|
||||
provider_name=provider_name,
|
||||
)
|
||||
|
||||
tasks.send_inbound_sms_to_service.apply_async([str(inbound.id), str(service.id)], queue=QueueNames.NOTIFY)
|
||||
tasks.send_inbound_sms_to_service.apply_async(
|
||||
[str(inbound.id), str(service.id)], queue=QueueNames.NOTIFY
|
||||
)
|
||||
|
||||
current_app.logger.debug(
|
||||
'{} received inbound SMS with reference {} from SNS'.format(service.id, inbound.provider_reference))
|
||||
"{} received inbound SMS with reference {} from SNS".format(
|
||||
service.id, inbound.provider_reference
|
||||
)
|
||||
)
|
||||
|
||||
return jsonify(
|
||||
result="success", message="SMS-SNS callback succeeded"
|
||||
), 200
|
||||
return jsonify(result="success", message="SMS-SNS callback succeeded"), 200
|
||||
|
||||
|
||||
def unescape_string(string):
|
||||
return string.encode('raw_unicode_escape').decode('unicode_escape')
|
||||
return string.encode("raw_unicode_escape").decode("unicode_escape")
|
||||
|
||||
|
||||
def create_inbound_sms_object(service, content, from_number, provider_ref, date_received, provider_name):
|
||||
def create_inbound_sms_object(
|
||||
service, content, from_number, provider_ref, date_received, provider_name
|
||||
):
|
||||
user_number = try_validate_and_format_phone_number(
|
||||
from_number,
|
||||
international=True,
|
||||
log_msg=f'Invalid from_number received for service "{service.id}"'
|
||||
log_msg=f'Invalid from_number received for service "{service.id}"',
|
||||
)
|
||||
|
||||
provider_date = date_received
|
||||
@@ -98,7 +103,7 @@ def create_inbound_sms_object(service, content, from_number, provider_ref, date_
|
||||
provider_date=provider_date,
|
||||
provider_reference=provider_ref,
|
||||
content=content,
|
||||
provider=provider_name
|
||||
provider=provider_name,
|
||||
)
|
||||
dao_create_inbound_sms(inbound)
|
||||
return inbound
|
||||
@@ -108,14 +113,17 @@ def fetch_potential_service(inbound_number, provider_name):
|
||||
service = dao_fetch_service_by_inbound_number(inbound_number)
|
||||
|
||||
if not service:
|
||||
current_app.logger.warning('Inbound number "{}" from {} not associated with a service'.format(
|
||||
inbound_number, provider_name
|
||||
))
|
||||
current_app.logger.warning(
|
||||
'Inbound number "{}" from {} not associated with a service'.format(
|
||||
inbound_number, provider_name
|
||||
)
|
||||
)
|
||||
return False
|
||||
|
||||
if not has_inbound_sms_permissions(service.permissions):
|
||||
current_app.logger.error(
|
||||
'Service "{}" does not allow inbound SMS'.format(service.id))
|
||||
'Service "{}" does not allow inbound SMS'.format(service.id)
|
||||
)
|
||||
return False
|
||||
|
||||
return service
|
||||
|
||||
Reference in New Issue
Block a user