def test_should_return_expected_success_response_for_registration(self):
        self.form_submission_mock.is_registration = True

        response = Response([{
            NAME_FIELD: "Mr. X"
        }], None, None, self.form_submission_mock.saved,
                            self.form_submission_mock.errors,
                            self.form_submission_mock.data_record_id,
                            self.form_submission_mock.short_code,
                            self.form_submission_mock.cleaned_data,
                            self.form_submission_mock.is_registration,
                            ['clinic'],
                            self.form_submission_mock.form_model.form_code)

        dbm_mock = Mock()
        form_model_mock = Mock(spec=FormModel)
        form_model_mock.stringify.return_value = {
            'name': 'Clinic X',
            'eid': 'cli001'
        }
        form_model_mock.entity_question.code = 'eid'
        with patch(
                "datawinners.messageprovider.message_handler.get_form_model_by_code"
        ) as get_form_model_mock:
            get_form_model_mock.return_value = form_model_mock
            response_text = SMSResponse(response).text(dbm_mock)
        self.assertEqual("Thank you Mr., We registered your clinic: Clinic X",
                         response_text)
Exemplo n.º 2
0
    def test_should_return_expected_success_response_for_registration(self):
        self.form_submission_mock.is_registration = True

        response = Response([{NAME_FIELD: "Mr. X"}],  None, self.form_submission_mock.saved,
            self.form_submission_mock.errors,
            self.form_submission_mock.data_record_id,
            self.form_submission_mock.short_code, self.form_submission_mock.cleaned_data,
            self.form_submission_mock.is_registration,
            ['clinic'],
            self.form_submission_mock.form_model.form_code)

        dbm_mock = Mock()
        form_model_mock = Mock(spec=EntityFormModel)
        form_model_mock.stringify.return_value = {'name': 'Clinic X', 'q2':'cli001'}
        short_code_field = Mock()
        short_code_field.code = 'q2'
        form_model_mock.entity_questions = [short_code_field]
        form_model_mock.get_entity_name_question_code.return_value = 'name'
        form_model_mock.entity_type = ["clinic"]
        with patch("datawinners.messageprovider.message_handler.get_form_model_by_code") as get_form_model_mock:
            with patch("datawinners.messageprovider.customized_message.account_wide_customized_message_details") as account_message:
                get_form_model_mock.return_value = form_model_mock
                account_message.return_value = [{"code":"reply_success_identification_number_registration",
                                                "message":"Thanks {Name of Data Sender}.registered {Identification Number Type} "
                                                          "{Name of Identification Number} {Submitted Identification Number}",
                                                "title": "Succesful register"}]
                response_text = SMSResponse(response, None).text(dbm_mock)
                self.assertEqual("Thanks Mr..registered clinic Clinic X cli001", response_text)
Exemplo n.º 3
0
    def test_should_return_expected_error_response(self):
        self.form_submission_mock.saved = False
        error_response = "horrible hack. feeling bad about it. But need to change mangrove error handling and error response"
        self.form_submission_mock.errors = error_response

        response = create_response_from_form_submission(
            reporters=[],
            submission_id=123,
            form_submission=self.form_submission_mock)
        self.assertEqual(error_response,
                         SMSResponse(response).text(Mock(spec=FormModel)))
Exemplo n.º 4
0
 def test_should_return_expected_success_response(self):
     response = Response(reporters=[{
         NAME_FIELD: "Mr. X"
     }],
                         submission_response=SubmissionResponse(
                             True,
                             "1",
                             datarecord_id="2",
                             short_code="CLI001",
                             processed_data={'name': 'Clinic X'}))
     self.assertEqual(u"Thank you Mr. X. We received : name: Clinic X",
                      SMSResponse(response).text())
Exemplo n.º 5
0
 def test_should_return_expected_success_response_for_registration(self):
     response = Response(reporters=[{
         NAME_FIELD: "Mr. X"
     }],
                         submission_response=SubmissionResponse(
                             True,
                             "1",
                             datarecord_id="2",
                             short_code="CLI001",
                             processed_data={'name': 'Clinic X'},
                             is_registration=True))
     self.assertEqual(
         u'Registration successful. Unique identification number(ID) is: CLI001.We received : name: Clinic X',
         SMSResponse(response).text())
Exemplo n.º 6
0
    def test_should_return_expected_error_response(self):
        self.form_submission_mock.saved = False
        error_response = "horrible hack. feeling bad about it. But need to change mangrove error handling and error response"
        self.form_submission_mock.errors = error_response

        response = Response([],  None, self.form_submission_mock.saved,
            self.form_submission_mock.errors,
            self.form_submission_mock.data_record_id,
            self.form_submission_mock.short_code, self.form_submission_mock.cleaned_data,
            self.form_submission_mock.is_registration,
            self.form_submission_mock.entity_type,
            self.form_submission_mock.form_model.form_code)
        with patch("datawinners.messageprovider.message_handler.get_form_model_by_code") as get_form_model_by_code:
            get_form_model_by_code.return_value = None
            self.assertEqual(error_response, SMSResponse(response, None).text(Mock(spec=DatabaseManager)))
Exemplo n.º 7
0
 def test_should_return_expected_success_response(self):
     self.form_submission_mock.is_registration = False
     response = create_response_from_form_submission(
         reporters=[{
             NAME_FIELD: "Mr. X"
         }],
         submission_id=123,
         form_submission=self.form_submission_mock)
     dbm_mock = Mock()
     form_model_mock = Mock(spec=FormModel)
     form_model_mock.stringify.return_value = {'name': 'Clinic X'}
     with patch(
             "datawinners.messageprovider.message_handler.get_form_model_by_code"
     ) as get_form_model_mock:
         get_form_model_mock.return_value = form_model_mock
         response_text = SMSResponse(response).text(dbm_mock)
     self.assertEqual(THANKS + u" name: Clinic X", response_text)
Exemplo n.º 8
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
Exemplo n.º 9
0
def sms(request):
    _message = request.POST["message"]
    _from, _to = _get_from_and_to_numbers(request)
    try:
        dbm = get_db_manager_for(_to)
        sms_player = SMSPlayer(dbm, SubmissionHandler(dbm))
        transportInfo = TransportInfo(transport=SMS,
                                      source=_from,
                                      destination=_to)
        response = sms_player.accept(
            Request(transportInfo=transportInfo, message=_message))
        message = SMSResponse(response).text()

    except (
            SubmissionParseException,
            FormModelDoesNotExistsException,
    ) as exception:
        message = get_exception_message_for(exception=exception, channel=SMS)
        log = DatawinnerLog(message=_message,
                            from_number=_from,
                            to_number=_to,
                            form_code=exception.data[0],
                            error=message)
        log.save()
    except NumberNotRegisteredException as exception:
        message = get_exception_message_for(exception=exception, channel=SMS)
        log = DatawinnerLog(message=_message,
                            from_number=_from,
                            to_number=_to,
                            form_code=None,
                            error=message)
        log.save()
    except MangroveException as exception:
        message = get_exception_message_for(exception=exception, channel=SMS)
    except Exception as exception:
        logger.exception('SMS Processing failure: message')
        message = get_exception_message_for(exception=exception, channel=SMS)

    return HttpResponse(message)
Exemplo n.º 10
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']
        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
Exemplo n.º 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)
Exemplo n.º 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