Example #1
0
    def test_should_handle_SMSParserWrongNumberOfAnswersException(self):
        exception = SMSParserWrongNumberOfAnswersException('test_code')

        form_model_mock = Mock(spec=FormModel)
        form_model_mock.activeLanguages = ['en']
        self.request['dbm'] = Mock(spec=DatabaseManager)
        expected_message = get_exception_message_for(exception, channel=SMS)

        response = handle(exception, self.request)

        self.assertEqual(expected_message, response)
    def test_should_handle_NumberNotRegisteredException(self):
        patcher = patch('datawinners.messageprovider.handlers.create_failure_log')
        create_failure_log_mock = patcher.start()

        exception = NumberNotRegisteredException('1234312')

        expected_message = get_exception_message_for(exception, SMS)
        response = handle(exception, self.request)
        patcher.stop()

        self.assertEqual(expected_message, response)
        create_failure_log_mock.assert_called_with(expected_message,self.request)
    def test_should_handle_SMSParserWrongNumberOfAnswersException(self):
        exception = SMSParserWrongNumberOfAnswersException('test_code')

        form_model_mock = Mock(spec=FormModel)
        patcher = patch('datawinners.messageprovider.handlers.get_form_model_by_code')
        get_form_model_mock = patcher.start()
        get_form_model_mock.return_value = form_model_mock
        form_model_mock.activeLanguages = ['en']
        self.request['dbm'] = Mock(spec=DatabaseManager)
        expected_message = get_exception_message_for(exception, channel=SMS)

        response = handle(exception, self.request)

        self.assertEqual(expected_message, response)
        self.assertEqual('en', get_language())
        patcher.stop()
Example #4
0
def submit_to_player(incoming_request):
    try:
        dbm = incoming_request['dbm']
        post_sms_parser_processors = [PostSMSProcessorLanguageActivator(dbm, incoming_request),
                    PostSMSProcessorNumberOfAnswersValidators(dbm, incoming_request)]
        sms_player = SMSPlayer(dbm, LocationBridge(get_location_tree(),get_loc_hierarchy=get_location_hierarchy),
            post_sms_parser_processors=post_sms_parser_processors)
        mangrove_request = Request(message=incoming_request['incoming_message'],
            transportInfo=incoming_request['transport_info'])
        response = sms_player.accept(mangrove_request, logger=incoming_request.get("logger"))
        message = SMSResponse(response).text(dbm)
        send_message(incoming_request, response)
    except DataObjectAlreadyExists as e:
        message = ugettext("%s with %s = %s already exists.") % (ugettext(e.data[2]), ugettext(e.data[0]), e.data[1])
    except Exception as exception:
        message = handle(exception, incoming_request)

    incoming_request['outgoing_message'] = message
    return incoming_request
Example #5
0
    def test_should_handle_NumberNotRegisteredException(self):
        patcher = patch(
            'datawinners.messageprovider.handlers.create_failure_log')
        create_failure_log_mock = patcher.start()

        exception = NumberNotRegisteredException('1234312')

        expected_message = "error msg"

        with patch(
                "datawinners.messageprovider.handlers.get_account_wide_sms_reply"
        ) as get_account_wide_sms_reply_mock:
            get_account_wide_sms_reply_mock.return_value = "error msg"
            response = handle(exception, self.request)

        patcher.stop()

        self.assertEqual(expected_message, response)
        create_failure_log_mock.assert_called_with(expected_message,
                                                   self.request)
def submit_to_player(incoming_request):
    sent_via_sms_test_questionnaire = incoming_request.get(
        'test_sms_questionnaire', False)
    organization = incoming_request.get('organization')
    organization = organization

    try:
        dbm = incoming_request['dbm']
        post_sms_parser_processors = [
            PostSMSProcessorLanguageActivator(dbm, incoming_request),
            PostSMSProcessorNumberOfAnswersValidators(dbm, incoming_request)
        ]
        sms_player = SMSPlayer(
            dbm,
            LocationBridge(get_location_tree(),
                           get_loc_hierarchy=get_location_hierarchy),
            post_sms_parser_processors=post_sms_parser_processors,
            feeds_dbm=incoming_request['feeds_dbm'])
        mangrove_request = Request(
            message=incoming_request['incoming_message'],
            transportInfo=incoming_request['transport_info'])
        response = sms_player.accept(mangrove_request,
                                     logger=incoming_request.get("logger"))

        if response.is_registration and not sent_via_sms_test_questionnaire:
            organization.increment_message_count_for(sms_registration_count=1)

        if not response.is_registration and sent_via_sms_test_questionnaire:
            organization.increment_message_count_for(incoming_web_count=1)

        mail_feed_errors(response, dbm.database_name)
        message = SMSResponse(response).text(dbm)
        send_message(incoming_request, response)
    except DataObjectAlreadyExists as e:
        message = ugettext("The Unique ID Number %s is already used for the %s %s. Register your %s with a different ID.") % \
                  (e.data[1], e.data[2], e.data[3], e.data[2])
        if not sent_via_sms_test_questionnaire:
            organization.increment_message_count_for(sms_registration_count=1)
    except DataObjectNotFound as exception:
        if sent_via_sms_test_questionnaire:
            organization.increment_message_count_for(incoming_web_count=1)
        message = handle(exception, incoming_request)
    except FormModelDoesNotExistsException as exception:
        if sent_via_sms_test_questionnaire:
            organization.increment_message_count_for(incoming_web_count=1)
        message = handle(exception, incoming_request)
    except Exception as exception:
        message = handle(exception, incoming_request)

    incoming_request['outgoing_message'] = message

    if not sent_via_sms_test_questionnaire:
        SMS(message=message,
            message_id=incoming_request['message_id'],
            organization=incoming_request['organization'],
            msg_from=mangrove_request.transport.source,
            msg_to=mangrove_request.transport.destination,
            msg_type=MSG_TYPE_SUBMISSION_REPLY,
            status="Submitted").save()

    return incoming_request
Example #7
0
 def test_should_handle_DataObjectNotFoundException(self):
     exception = DataObjectNotFound('test_entity', 'id', '123')
     exception_message_dict = exception_messages[type(exception)]
     expected_message = exception_message_dict.get(SMS) % ('123')
     response = handle(exception, self.request)
     self.assertEqual(expected_message, response)
Example #8
0
def submit_to_player(incoming_request):
    sent_via_sms_test_questionnaire = incoming_request.get('test_sms_questionnaire', False)
    organization = incoming_request.get('organization')
    organization = organization
    should_increment_incoming_sms_count = True if not sent_via_sms_test_questionnaire else False
    response =None
    try:

        dbm = incoming_request['dbm']
        mangrove_request = Request(message=incoming_request['incoming_message'],
                                   transportInfo=incoming_request['transport_info'])

        post_sms_parser_processors = [PostSMSProcessorLanguageActivator(dbm, incoming_request),
                                      PostSMSProcessorCheckDSIsRegistered(dbm, incoming_request)]
        if organization.in_trial_mode:
            post_sms_parser_processors.append(PostSMSProcessorCheckLimits(dbm, incoming_request))

        post_sms_parser_processors.extend([PostSMSProcessorNumberOfAnswersValidators(dbm, incoming_request),
                                           PostSMSProcessorCheckDSIsLinkedToProject(dbm, incoming_request)])

        sms_player = SMSPlayer(dbm, LocationBridge(get_location_tree(), get_loc_hierarchy=get_location_hierarchy),
                               post_sms_parser_processors=post_sms_parser_processors,
                               feeds_dbm=incoming_request['feeds_dbm'])

        response = sms_player.accept(mangrove_request, logger=incoming_request.get("logger"),
                                     translation_processor=TranslationProcessor)

        if response.is_registration:
            incoming_request['is_registration'] = True
            if not sent_via_sms_test_questionnaire:
                organization.increment_message_count_for(sms_registration_count=1)
        else:
            if sent_via_sms_test_questionnaire:
                organization.increment_message_count_for(incoming_web_count=1)
            check_quotas_and_update_users(organization, sms_channel=True)

        mail_feed_errors(response, dbm.database_name)
        message = SMSResponse(response, incoming_request).text(dbm)
        send_message(incoming_request, response)
    except DataObjectAlreadyExists as e:
        message = identification_number_already_exists_handler(dbm, e.data[1], e.data[2])
        if not sent_via_sms_test_questionnaire:
            organization.increment_message_count_for(sms_registration_count=1)

    except DataObjectNotFound as exception:
        if sent_via_sms_test_questionnaire:
            organization.increment_message_count_for(incoming_web_count=1)
        message = handle(exception, incoming_request)

    except FormModelDoesNotExistsException as exception:
        if sent_via_sms_test_questionnaire:
            organization.increment_message_count_for(incoming_web_count=1)
        message = incorrect_questionnaire_code_handler(dbm, exception.data[0], incoming_request)

    except SMSParserWrongNumberOfAnswersException:
        form_model = sms_player.get_form_model(mangrove_request)
        if not form_model.is_entity_registration_form():
            if sent_via_sms_test_questionnaire:
                organization.increment_message_count_for(incoming_web_count=1)

            message = incorrect_number_of_answers_for_submission_handler(dbm, form_model.form_code, incoming_request)
        elif form_model.is_entity_registration_form():
            message = incorrect_number_of_answers_for_uid_registration_handler(dbm, form_model.form_code, incoming_request)

    except (ExceedSubmissionLimitException, ExceedSMSLimitException) as exception:
        should_increment_incoming_sms_count = False
        message = handle(exception, incoming_request)

    except Exception as exception:
        if sent_via_sms_test_questionnaire:
            organization.increment_message_count_for(incoming_web_count=1)
        message = handle(exception, incoming_request)

    if should_increment_incoming_sms_count:
        organization.increment_incoming_message_count()

    if response and not response.is_registration:
        check_quotas_and_update_users(organization, )
    return post_player_handler(incoming_request, message)
Example #9
0
def submit_to_player(incoming_request):
    sent_via_sms_test_questionnaire = incoming_request.get(
        'test_sms_questionnaire', False)
    organization = incoming_request.get('organization')
    organization = organization

    try:
        dbm = incoming_request['dbm']
        mangrove_request = Request(
            message=incoming_request['incoming_message'],
            transportInfo=incoming_request['transport_info'])
        response = _is_datasender_registered(dbm, incoming_request)
        if not response:
            post_sms_parser_processors = [
                PostSMSProcessorLanguageActivator(dbm, incoming_request)
            ]
            if organization.in_trial_mode:
                post_sms_parser_processors.append(
                    PostSMSProcessorCheckLimits(dbm, incoming_request))

            post_sms_parser_processors.extend([
                PostSMSProcessorNumberOfAnswersValidators(
                    dbm, incoming_request),
                PostSMSProcessorCheckDSIsLinkedToProject(
                    dbm, incoming_request)
            ])

            sms_player = SMSPlayer(
                dbm,
                LocationBridge(get_location_tree(),
                               get_loc_hierarchy=get_location_hierarchy),
                post_sms_parser_processors=post_sms_parser_processors,
                feeds_dbm=incoming_request['feeds_dbm'])

            response = sms_player.accept(
                mangrove_request,
                logger=incoming_request.get("logger"),
                translation_processor=TranslationProcessor)

            if response.is_registration:
                incoming_request['is_registration'] = True
                if not sent_via_sms_test_questionnaire:
                    organization.increment_message_count_for(
                        sms_registration_count=1)

            if not response.is_registration:
                if sent_via_sms_test_questionnaire:
                    organization.increment_message_count_for(
                        incoming_web_count=1)
                check_quotas_and_update_users(organization, sms_channel=True)

        mail_feed_errors(response, dbm.database_name)
        message = SMSResponse(response, incoming_request).text(dbm)
        send_message(incoming_request, response)
    except DataObjectAlreadyExists as e:
        message = identification_number_already_exists_handler(
            dbm, e.data[1], e.data[2])
        if not sent_via_sms_test_questionnaire:
            organization.increment_message_count_for(sms_registration_count=1)

    except DataObjectNotFound as exception:
        if sent_via_sms_test_questionnaire:
            organization.increment_message_count_for(incoming_web_count=1)
        message = handle(exception, incoming_request)

    except FormModelDoesNotExistsException as exception:
        if sent_via_sms_test_questionnaire:
            organization.increment_message_count_for(incoming_web_count=1)
        message = incorrect_questionnaire_code_handler(dbm, exception.data[0])

    except SMSParserWrongNumberOfAnswersException as exception:
        form_model = sms_player.get_form_model(mangrove_request)
        if not form_model.is_entity_registration_form():
            organization.increment_message_count_for(
                incoming_web_count=1
            ) if sent_via_sms_test_questionnaire else organization.increment_message_count_for(
                incoming_sms_count=1)
            message = incorrect_number_of_answers_for_submission_handler(
                dbm, form_model.form_code, incoming_request)
        elif form_model.is_entity_registration_form():
            message = incorrect_number_of_answers_for_uid_registration_handler(
                dbm, form_model.form_code, incoming_request)
        elif not sent_via_sms_test_questionnaire:
            organization.increment_message_count_for(sms_registration_count=1)

    except Exception as exception:
        if sent_via_sms_test_questionnaire:
            organization.increment_message_count_for(incoming_web_count=1)
        message = handle(exception, incoming_request)

    message = incoming_request.get('error_message', message)
    incoming_request['outgoing_message'] = message if incoming_request.get(
        'is_outgoing_reply_sms_enabled', True) else ""

    if not sent_via_sms_test_questionnaire:

        if incoming_request.get('is_outgoing_reply_sms_enabled', True):
            organization.increment_message_count_for(outgoing_sms_count=1)

        SMS(message=message,
            message_id=incoming_request['message_id'],
            organization=incoming_request['organization'],
            msg_from=mangrove_request.transport.source,
            msg_to=mangrove_request.transport.destination,
            msg_type=MSG_TYPE_SUBMISSION_REPLY,
            status="Submitted").save()

    return incoming_request