def test_for_activity_report_question_should_send_error_for_data_submission_with_extra_answers(self):
        self.form_model_patch.return_value = self._get_form_model_mock(is_registration_form=False, fields=[1, 2, 3])

        processor = PostSMSProcessorNumberOfAnswersValidators(dbm=None, request={})
        response = processor.process("form_code", {'q1': 'ans', 'q2': 'ans2'}, ["tsara", "bien", "cool"])
        self.assertEqual(False, response.success)
        self.assertEqual(get_wrong_number_of_answer_error_message(), response.errors)
 def test_for_registration_should_send_error_if_number_of_answers_incorrect_when_subject_question_is_not_present(
         self):
     self.form_model_patch.return_value = self._get_form_model_mock(is_registration_form=True, fields=[1, 2, 3])
     processor = PostSMSProcessorNumberOfAnswersValidators(dbm=None, request={})
     response = processor.process("form_code", {'q1': 'ans', 'q2': 'ans2'})
     self.assertEqual(False, response.success)
     self.assertEqual(get_wrong_number_of_answer_error_message(), response.errors)
 def test_should_not_send_error_for_data_submission_if_number_of_answers_are_correct(
         self):
     self.form_model_patch.return_value = self._get_form_model_mock(
         is_registration_form=False, fields=[1, 2])
     processor = PostSMSProcessorNumberOfAnswersValidators(dbm=None,
                                                           request={})
     response = processor.process("form_code", {'q1': 'ans', 'q2': 'ans2'})
     self.assertEqual(None, response)
    def test_for_activity_report_question_should_send_error_for_data_submission_if_number_of_answers_are_not_equal_to_number_of_questions(self):
        # there won't be any answers for entity question
        self.form_model_patch.return_value = self._get_form_model_mock(is_registration_form=False, fields=[1, 2, 3])

        processor = PostSMSProcessorNumberOfAnswersValidators(dbm=None, request={})
        response = processor.process("form_code", {'q1': 'ans', })
        self.assertEqual(False, response.success)
        self.assertEqual(get_wrong_number_of_answer_error_message(), response.errors)
 def test_for_registration_should_not_send_error_for_global_registration_form(
         self):
     self.form_model_patch.return_value = self._get_form_model_mock(
         is_registration_form=True, fields=[1, 2])
     processor = PostSMSProcessorNumberOfAnswersValidators(dbm=None,
                                                           request={})
     response = processor.process(GLOBAL_REGISTRATION_FORM_CODE, {
         'q1': 'ans',
         'q2': 'ans2'
     })
     self.assertEqual(None, response)
 def test_for_registration_should_send_error_if_number_of_answers_incorrect_when_subject_question_is_not_present(
         self):
     self.form_model_patch.return_value = self._get_form_model_mock(
         is_registration_form=True, fields=[1, 2, 3])
     incoming_request = {}
     processor = PostSMSProcessorNumberOfAnswersValidators(
         dbm=None, request=incoming_request)
     processor.process("form_code", {'q1': 'ans'})
     self.assertTrue(
         isinstance(incoming_request.get('exception'),
                    SMSParserWrongNumberOfAnswersException))
    def test_for_activity_report_question_should_send_error_for_data_submission_if_number_of_answers_are_not_equal_to_number_of_questions(
            self):
        # there won't be any answers for entity question
        self.form_model_patch.return_value = self._get_form_model_mock(
            is_registration_form=False, fields=[1, 2, 3])

        incoming_request = {}
        processor = PostSMSProcessorNumberOfAnswersValidators(
            dbm=None, request=incoming_request)
        processor.process("form_code", {
            'q1': 'ans',
        })
        self.assertTrue(
            isinstance(incoming_request.get('exception'),
                       SMSParserWrongNumberOfAnswersException))
    def test_for_activity_report_question_should_send_error_for_data_submission_with_extra_answers(
            self):
        self.form_model_patch.return_value = self._get_form_model_mock(
            is_registration_form=False, fields=[1, 2, 3])

        incoming_request = {}
        processor = PostSMSProcessorNumberOfAnswersValidators(
            dbm=None, request=incoming_request)
        processor.process("form_code", {
            'q1': 'ans',
            'q2': 'ans2'
        }, ["tsara", "bien", "cool"])
        self.assertTrue(
            isinstance(incoming_request.get('exception'),
                       SMSParserWrongNumberOfAnswersException))
Exemple #9
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
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
Exemple #11
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)
Exemple #12
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