def _get_response(self, form_code): response = Response(reporters=[], survey_response_id=None, exception=self._get_exception()) response.success = True response.errors = data_sender_not_linked_handler(self.dbm, self.request, form_code=form_code) return response
def delete_survey(self, survey_response, additional_details): feed_delete_errors = None try: survey_response.void() form_model = FormModel.get(self.dbm, survey_response.form_model_id) if self.feeds_dbm: feed_delete_errors = EnrichedSurveyResponseBuilder( self.dbm, survey_response, form_model, additional_details).delete_feed_document(self.feeds_dbm) except MangroveException as e: return Response(errors=e.message, feed_error_message=feed_delete_errors) return Response(success=True, feed_error_message=feed_delete_errors)
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)
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)
def test_should_call_parser_post_processor_and_return_if_there_is_response_from_post_processor( self): parser_mock = Mock(spec=OrderSMSParser) parser_mock.parse.return_value = ('FORM_CODE', {'id': '1'}, []) parser_mock.parse.return_value = ('FORM_CODE', {'id': '1'}, []) post_sms_processor_mock = Mock() expected_response = Response(reporters=None, survey_response_id=None) post_sms_processor_mock.process.return_value = expected_response message = 'FORM_CODE 1' sms_player = SMSPlayerV2( self.dbm, post_sms_parser_processors=[post_sms_processor_mock]) with patch("inspect.getargspec") as get_arg_spec_mock: with patch('mangrove.form_model.project.get_project_by_code' ) as get_project_by_code: with patch( 'mangrove.form_model.project.check_if_form_code_is_poll' ) as mock_check_if_form_code_is_poll: get_arg_spec_mock.return_value = ([ 'self', 'form_code', 'submission_values', 'extra_elements' ], ) response = sms_player.add_survey_response( Request(message=message, transportInfo=self.transport)) self.assertEqual(expected_response, response)
def test_should_fetch_questions_from_form_model_for_non_subject_entity( self): manager = Mock(spec=DatabaseManager) with patch("datawinners.entity.import_data.get_form_model_by_code" ) as get_form_model_by_code: _get_form_model_questions( manager, (1, Response(form_code='formcode', entity_type=['clinic']))) get_form_model_by_code.assert_called_with(manager, 'formcode')
def test_should_fetch_questions_from_for_reporter(self): manager = Mock(spec=DatabaseManager) with patch("datawinners.entity.import_data.get_form_model_by_code" ) as get_form_model_by_code: return_dict = _get_form_model_questions( manager, (1, Response(form_code='formcode', entity_type=['reporter']))) self.assertEquals(return_dict, { 'n': ''Name'', 'm': ''Mobile Number'' })
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)))
def edit_survey(self, form_code, values, reporter_names, survey_response, additional_feed_dictionary=None, owner_id=None): form_model = get_form_model_by_code(self.dbm, form_code) form = EditSurveyResponseForm(self.dbm, survey_response, form_model, values) try: if form.is_valid: if owner_id: reporter = by_short_code(self.dbm, owner_id, REPORTER_ENTITY_TYPE) survey_response.owner_uid = reporter.id survey_response.modified_by = self.admin_id or owner_id survey_response = form.save() try: feed_create_errors = None if self.feeds_dbm: builder = EnrichedSurveyResponseBuilder( self.dbm, survey_response, form_model, additional_feed_dictionary) event_document = builder.update_event_document( self.feeds_dbm) self.feeds_dbm._save_document(event_document) except Exception as e: feed_create_errors = 'error while editing feed doc for %s \n' % survey_response.id feed_create_errors += e.message + '\n' feed_create_errors += traceback.format_exc() except MangroveException as exception: raise return Response(reporter_names, survey_response.uuid, form.saved, form.errors, form.data_record_id, None, form._cleaned_data, form.is_registration, form.entity_type, form.form_model.form_code, feed_create_errors, created=survey_response.created, version=survey_response.version)
def handle(self, form_model, cleaned_data, errors, reporter_names, location_tree=None): short_code = cleaned_data[SHORT_CODE] entity_type = cleaned_data[ENTITY_TYPE_FIELD_CODE] if is_empty(errors): if entity_type[0] == 'reporter': void_contact(self.dbm, short_code) else: void_entity(self.dbm, entity_type, short_code) return Response(reporter_names, None, is_empty(errors), errors, None, short_code, cleaned_data, False, entity_type, form_model.form_code)
def _appendFailedResponse(self, message, values=None): response = Response(reporters=[], survey_response_id=None) response.success = False response.errors = dict(error=ugettext(message), row=values) return response
def _get_wrong_number_of_question_response(self): response = Response(reporters=[], survey_response_id=None) response.success = False response.errors = get_wrong_number_of_answer_error_message() return response
def _get_response(self): response = Response(reporters=[], survey_response_id=None) response.errors = data_sender_not_registered_handler(self.dbm, self.request) return response
def save_survey(self, form_code, values, reporter_names, transport_info, reporter_id, additional_feed_dictionary=None, translation_processor=None): try: form_model = get_form_model_by_code(self.dbm, form_code) except FormModelDoesNotExistsException: form_model = get_active_form_model(self.dbm, form_code) #TODO : validate_submission should use form_model's bound values cleaned_data, errors = form_model.validate_submission(values=values) form_model.bind(form_model.remove_invalid_meta_answers(values)) if reporter_id is not None: survey_response = self.create_survey_response_from_known_datasender( transport_info, form_model, form_model.bound_values(), reporter_id, self.response) else: survey_response = self.create_survey_response_from_unknown_datasender( transport_info, form_model.id, form_model.bound_values(), self.response) survey_response.set_form(form_model) form_submission = DataFormSubmission(form_model, cleaned_data, errors) feed_create_errors = None try: if form_submission.is_valid: form_submission.save(self.dbm) except MangroveException as exception: errors = exception.message raise finally: if translation_processor is not None: translated_errors = translation_processor( form_model, self.response).process() survey_response.set_status(translated_errors) else: survey_response.set_status(errors) survey_response.create(form_submission.data_record_id) try: if self.feeds_dbm: builder = EnrichedSurveyResponseBuilder( self.dbm, survey_response, form_model, additional_feed_dictionary, ds_mobile_number=transport_info.source) event_document = builder.feed_document() self.feeds_dbm._save_document(event_document) except Exception as e: feed_create_errors = 'error while creating feed doc for %s \n' % survey_response.id feed_create_errors += e.message + '\n' feed_create_errors += traceback.format_exc() if self.response is None: errors = form_submission.errors success = form_submission.saved else: errors = self.response.errors success = False return Response(reporter_names, survey_response.uuid, success, errors, form_submission.data_record_id, form_submission.short_code, form_submission.cleaned_data, form_submission.is_registration, form_submission.entity_type, form_submission.form_model.form_code, feed_create_errors, created=survey_response.created, version=survey_response.version)