Пример #1
0
 def test_should_raise_no_exception_if_ds_is_linked_and_number_of_answers_is_correct(self):
     self.form_model_patch.return_value = self._get_form_model_mock(is_registration_form=False, fields=[1, 2])
     self.project_mock.return_value = self._get_project_mock()
     reporter_entity = Mock()
     reporter_entity.short_code = 'rep2'
     incoming_request = {'reporter_entity': reporter_entity}
     processor = PostSMSProcessorCheckDSIsLinkedToProject(dbm=None, request=incoming_request)
     response = processor.process("form_code", {'q1': 'ans', 'q2': 'ans2'})
     self.assertEqual(None, response)
Пример #2
0
 def test_should_raise_number_of_response_exception_if_number_of_responses_is_incorrect_and_datasender_is_linked(self):
     self.form_model_patch.return_value = self._get_form_model_mock(is_registration_form=False, fields=[1, 2])
     self.project_mock.return_value = self._get_project_mock()
     reporter_entity = Mock()
     reporter_entity.short_code = 'rep2'
     incoming_request = {'reporter_entity': reporter_entity, 'exception': SMSParserWrongNumberOfAnswersException('form_code')}
     processor = PostSMSProcessorCheckDSIsLinkedToProject(dbm=None, request=incoming_request)
     self.assertRaises(SMSParserWrongNumberOfAnswersException, processor.process, "form_code", {'q1': 'ans', 'q2': 'ans2'})
Пример #3
0
    def test_should_raise_datasender_not_linked_exception_if_number_of_responses_is_incorrect_and_datasender_is_not_linked(self):
        self.form_model_patch.return_value = self._get_form_model_mock(is_registration_form=False, fields=[1, 2])
        self.project_mock.return_value = self._get_project_mock()
        reporter_entity = Mock()
        reporter_entity.short_code = 'rep23'
        incoming_request = {'reporter_entity': reporter_entity, 'exception': SMSParserWrongNumberOfAnswersException('form_code')}
        processor = PostSMSProcessorCheckDSIsLinkedToProject(dbm=None, request=incoming_request)
        self.assertRaises(DatasenderIsNotLinkedException, processor.process, "form_code", {'q1': 'ans', 'q2': 'ans2'})

    #def test_should_not_raise_exception_if_number_of_responses_is_correct_and_datasender_is_not_linked(self):
    #    self.form_model_patch.return_value = self._get_form_model_mock(is_registration_form=False, fields=[1, 2])
    #    self.project_mock.return_value = self._get_project_mock()
    #    reporter_entity = Mock()
    #    reporter_entity.short_code = 'rep23'
    #    incoming_request = {'reporter_entity': reporter_entity}
    #    processor = PostSMSProcessorCheckDSIsLinkedToProject(dbm=None, request=incoming_request)
    #
    #    response = processor.process("form_code", {'q1': 'ans', 'q2': 'ans2'})
    #
    #    self.assertNotEqual(None, response)
    #    self.assertEqual(NOT_AUTHORIZED_DATASENDER_MSG, response.errors)
Пример #4
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)
Пример #5
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