<commit_before>from urllib.parse import unquote

import iso8601
from flask import jsonify, Blueprint, current_app, request
from notifications_utils.recipients import validate_and_format_phone_number

from app import statsd_client, firetext_client, mmg_client
from app.celery import tasks
from app.config import QueueNames
from app.dao.services_dao import dao_fetch_services_by_sms_sender
from app.dao.inbound_sms_dao import dao_create_inbound_sms
from app.models import InboundSms
from app.errors import register_errors
from app.utils import convert_bst_to_utc

receive_notifications_blueprint = Blueprint('receive_notifications', __name__)
register_errors(receive_notifications_blueprint)


@receive_notifications_blueprint.route('/notifications/sms/receive/mmg', methods=['POST'])
def receive_mmg_sms():
    """
    {
        'MSISDN': '447123456789'
        'Number': '40604',
        'Message': 'some+uri+encoded+message%3A',
        'ID': 'SOME-MMG-SPECIFIC-ID',
        'DateRecieved': '2017-05-21+11%3A56%3A11'
    }
    """
    post_data = request.get_json()

    inbound_number = strip_leading_forty_four(post_data['Number'])

    potential_services = fetch_potential_services(inbound_number, 'mmg')
    if not potential_services:
        # since this is an issue with our service <-> number mapping, we should still tell MMG that we received
        # successfully
        return 'RECEIVED', 200

    statsd_client.incr('inbound.mmg.successful')

    service = potential_services[0]

    inbound = create_inbound_sms_object(service,
                                        content=format_mmg_message(post_data["Message"]),
                                        from_number=post_data['MSISDN'],
                                        provider_ref=post_data["ID"],
                                        date_received=post_data.get('DateRecieved'),
                                        provider_name="mmg")

    tasks.send_inbound_sms_to_service.apply_async([str(inbound.id), str(service.id)], queue=QueueNames.NOTIFY)
    current_app.logger.info(
        '{} received inbound SMS with reference {} from MMG'.format(service.id, inbound.provider_reference))

    return 'RECEIVED', 200


@receive_notifications_blueprint.route('/notifications/sms/receive/firetext', methods=['POST'])
def receive_firetext_sms():
    post_data = request.form<commit_msg>add logs to see the IP address of the inbound sms<commit_after>from urllib.parse import unquote

import iso8601
from flask import jsonify, Blueprint, current_app, request
from notifications_utils.recipients import validate_and_format_phone_number

from app import statsd_client, firetext_client, mmg_client
from app.celery import tasks
from app.config import QueueNames
from app.dao.services_dao import dao_fetch_services_by_sms_sender
from app.dao.inbound_sms_dao import dao_create_inbound_sms
from app.models import InboundSms
from app.errors import register_errors
from app.utils import convert_bst_to_utc

receive_notifications_blueprint = Blueprint('receive_notifications', __name__)
register_errors(receive_notifications_blueprint)


@receive_notifications_blueprint.route('/notifications/sms/receive/mmg', methods=['POST'])
def receive_mmg_sms():
    """
    {
        'MSISDN': '447123456789'
        'Number': '40604',
        'Message': 'some+uri+encoded+message%3A',
        'ID': 'SOME-MMG-SPECIFIC-ID',
        'DateRecieved': '2017-05-21+11%3A56%3A11'
    }
    """
    current_app.logger.info("Inbound sms sender IP information {}".format(request.headers.getlist("X-Forwarded-For")))
    post_data = request.get_json()

    inbound_number = strip_leading_forty_four(post_data['Number'])

    potential_services = fetch_potential_services(inbound_number, 'mmg')
    if not potential_services:
        # since this is an issue with our service <-> number mapping, we should still tell MMG that we received
        # successfully
        return 'RECEIVED', 200

    statsd_client.incr('inbound.mmg.successful')

    service = potential_services[0]

    inbound = create_inbound_sms_object(service,
                                        content=format_mmg_message(post_data["Message"]),
                                        from_number=post_data['MSISDN'],
                                        provider_ref=post_data["ID"],
                                        date_received=post_data.get('DateRecieved'),
                                        provider_name="mmg")

    tasks.send_inbound_sms_to_service.apply_async([str(inbound.id), str(service.id)], queue=QueueNames.NOTIFY)
    current_app.logger.info(
        '{} received inbound SMS with reference {} from MMG'.format(service.id, inbound.provider_reference))

    return 'RECEIVED', 200


@receive_notifications_blueprint.route('/notifications/sms/receive/firetext', methods=['POST'])
def receive_firetext_sms():
    post_data = request.form
{'fraction_non_alphanumeric': 0.09433190096173522, 'fraction_numerical': 0.01841620626151013, 'mean_word_length': 3.443636363636364, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1072262', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1430, 'n_words': 279}