def test_response_parsing(self): self.assertEqual(parse_responses('', self.test_form)[0], {}) self.assertEqual( parse_responses('AA1BA2', self.test_form)[0], { 'AA': '1', 'BA': 1 }) self.assertEqual( parse_responses('EA135DBAAA3', self.test_form)[0], { 'AA': '3', 'BA': 1, 'D': 1, 'EA': '135' }) self.assertNotIn('Comment1', parse_responses('EA135DBAAA3', self.test_form)[0]) self.assertEqual( parse_responses('ZX1CV2EA135DBAAA3', self.test_form)[0], { 'AA': '3', 'BA': 1, 'D': 1, 'EA': '135', 'ZX': '1', 'CV': '2' })
def test_leftover_processing(self): response_dict, extra = parse_responses('ZX1CV2EA135DBAAA3', self.test_form) self.assertEqual(extra, '') response_dict, extra = parse_responses( 'ZX1CV2EA135DBAAA3THIS IS A TEST', self.test_form) self.assertEqual(extra, 'THIS IS A TEST') response_dict, extra = parse_responses( 'ZX1CV2EA135DBAAA3 THIS IS A TEST ', self.test_form) self.assertEqual(extra, 'THIS IS A TEST')
def parse_message(form): message = form.get_message() submission = None had_errors = False response_dict = None (prefix, participant_id, exclamation, responses, comment) = parse_text(message['text']) if (prefix and participant_id and responses): form_doc = retrieve_form(prefix, exclamation) if form_doc: response_dict = parse_responses(responses, form_doc) if form_doc and response_dict: form_data = MultiDict({ 'form': form_doc.pk, 'participant': participant_id, 'sender': message['sender'], 'comment': comment }) form_data.update(response_dict) questionnaire = build_questionnaire(form_doc, form_data) if questionnaire.validate(): submission = questionnaire.save() return (_( 'Thank you! Your report was received!' ' You sent: {text}').format(text=message.get('text', '')), submission, had_errors) else: had_errors = True if 'participant' in questionnaire.errors: return (_( 'Observer ID not found. Please resend with valid ' 'Observer ID. You sent: {text}').format( text=message.get('text', '')), submission, had_errors) elif '__all__' in questionnaire.errors: # Save any valid data submission = questionnaire.save() return (_('Unknown question codes: "{questions}". ' 'You sent: {text}').format(questions=', '.join( sorted([ q for q in questionnaire.errors['__all__'] ])), text=message.get( 'text', '')), submission, had_errors) else: # Save any valid data submission = questionnaire.save() return (_('Invalid response(s) for question(s):' ' "{questions}". You sent: {text}').format( questions=', '.join( sorted(questionnaire.errors.keys())), text=message.get('text', '')), submission, had_errors) had_errors = True return (_('Invalid message: "{text}". Please check and resend!').format( text=message.get('text', '')), submission, had_errors)
def test_incident_parsing(self): sample_text = 'AB' responses = parse_responses(sample_text, self.incident_form) q = build_questionnaire(self.incident_form, MultiDict(responses)) flag = q.validate() data = q.data self.assertEqual(data['A'], 1) self.assertEqual(data['B'], 1) self.assertFalse(flag)
def test_incident_parsing(self): sample_text = "AB" responses = parse_responses(sample_text, self.incident_form) q = build_questionnaire(self.incident_form, MultiDict(responses)) flag = q.validate() data = q.data self.assertEqual(data["A"], 1) self.assertEqual(data["B"], 1) self.assertFalse(flag)
def test_checklist_parsing(self): sample_text = "AA2AB12" q = build_questionnaire(self.checklist_form, MultiDict(parse_responses(sample_text, self.checklist_form))) flag = q.validate() data = q.data self.assertEqual(data["AA"], 2) self.assertEqual(data["AB"], [1, 2]) # invalid due to missing data self.assertFalse(flag)
def test_checklist_parsing(self): sample_text = 'AA2AB12' q = build_questionnaire( self.checklist_form, MultiDict(parse_responses(sample_text, self.checklist_form))) flag = q.validate() data = q.data self.assertEqual(data['AA'], 2) self.assertEqual(data['AB'], [1, 2]) # invalid due to missing data self.assertFalse(flag)
def test_incident_parsing(self, filter_form, filter_participants): filter_form.return_value = [self.checklist_form] filter_participants.return_value = [] sample_text = 'AB' responses = parse_responses(sample_text, self.incident_form)[0] q = build_questionnaire(self.incident_form, MultiDict(responses)) flag = q.validate() data = q.data self.assertEqual(data['A'], 1) self.assertEqual(data['B'], 1) self.assertFalse(flag)
def test_checklist_parsing(self, filter_form, filter_participants): filter_form.return_value = [self.checklist_form] filter_participants.return_value = [] sample_text = 'AA2AB12' q = build_questionnaire( self.checklist_form, MultiDict(parse_responses(sample_text, self.checklist_form)[0])) flag = q.validate() data = q.data self.assertEqual(data['AA'], 2) self.assertEqual(data['AB'], [1, 2]) # invalid due to missing data self.assertFalse(flag)
def parse_message(form): message = form.get_message() submission = None had_errors = False response_dict = None (prefix, participant_id, exclamation, responses, comment) = parse_text( message['text']) if (prefix and participant_id and responses): form_doc = retrieve_form(prefix, exclamation) if form_doc: response_dict = parse_responses(responses, form_doc) if form_doc and response_dict: form_data = MultiDict( {'form': form_doc.pk, 'participant': participant_id, 'sender': message['sender'], 'comment': comment}) form_data.update(response_dict) questionnaire = build_questionnaire(form_doc, form_data) if questionnaire.validate(): submission = questionnaire.save() return ( _('Thank you! Your report was received!' ' You sent: {text}') .format(text=message.get('text', '')), submission, had_errors ) else: had_errors = True if 'participant' in questionnaire.errors: return ( _('Observer ID not found. Please resend with valid ' 'Observer ID. You sent: {text}') .format(text=message.get('text', '')), submission, had_errors ) elif '__all__' in questionnaire.errors: # Save any valid data submission = questionnaire.save() return ( _('Unknown question codes: "{questions}". ' 'You sent: {text}') .format(questions=', '.join( sorted([ q for q in questionnaire.errors['__all__'] ])), text=message.get('text', '')), submission, had_errors ) else: # Save any valid data submission = questionnaire.save() return ( _('Invalid response(s) for question(s):' ' "{questions}". You sent: {text}') .format( questions=', '.join(sorted( questionnaire.errors.keys())), text=message.get('text', '')), submission, had_errors ) had_errors = True return ( _('Invalid message: "{text}". Please check and resend!').format( text=message.get('text', '')), submission, had_errors )
def parse_message(form): message = form.get_message() submission = None had_errors = False response_dict = None (prefix, participant_id, exclamation, responses, comment) = parse_text(message['text']) if (prefix and participant_id and responses): form_doc = retrieve_form(prefix, exclamation) if form_doc: response_dict, extra = parse_responses(responses, form_doc) if form_doc and response_dict: form_data = MultiDict({ 'form': form_doc.pk, 'participant': participant_id, 'sender': message['sender'], 'comment': comment }) form_data.update(response_dict) questionnaire = build_questionnaire(form_doc, form_data) if questionnaire.validate(): submission = questionnaire.save() # if submission returns empty, then the participant # was not meant to send this text. # TODO: add response for no recorded submission # check if there were extra fields sent in diff = set(response_dict.keys()).difference( set(questionnaire.data.keys())) if not diff and not extra: return (_('Thank you! Your report was received!' ' You sent: {text}').format( text=message.get('text', '')), submission, had_errors) elif diff: had_errors = True return (_('Unknown question codes: "{questions}". ' 'You sent: {text}').format( questions=', '.join(sorted(diff)), text=message.get('text', '')), submission, had_errors) elif extra: had_errors = True return (_('Invalid text sent: "{extra}". ' 'You sent: {text}').format(extra=extra, text=message.get( 'text', '')), submission, had_errors) else: had_errors = True if 'participant' in questionnaire.errors: return (_( 'Observer ID not found. Please resend with valid ' 'Observer ID. You sent: {text}').format( text=message.get('text', '')), submission, had_errors) else: # Save any valid data submission = questionnaire.save() return (_('Invalid response(s) for question(s):' ' "{questions}". You sent: {text}').format( questions=', '.join( sorted(questionnaire.errors.keys())), text=message.get('text', '')), submission, had_errors) had_errors = True return (_('Invalid message: "{text}". Please check and resend!').format( text=message.get('text', '')), submission, had_errors)
def parse_message(form): message = form.get_message() submission = None had_errors = False response_dict = None (prefix, participant_id, exclamation, form_serial, responses, comment) = \ parse_text(message['text']) if (prefix and participant_id and responses): form_doc = retrieve_form(prefix, exclamation) if form_doc: response_dict, extra = parse_responses(responses, form_doc) if form_doc and response_dict: form_data = MultiDict( {'form': form_doc.id, 'participant': participant_id, 'sender': message['sender'], 'form_serial': form_serial, 'comment': comment}) form_data.update(response_dict) questionnaire = build_questionnaire(form_doc, form_data) if questionnaire.validate(): submission = questionnaire.save() # if submission returns empty, then the participant # was not meant to send this text. if submission is None: reply = gettext( 'Invalid message: %(text)s. Please check and resend!', text=message.get('text', '')) return reply, submission, True # check if there were extra fields sent in diff = set(response_dict.keys()).difference( set(questionnaire.data.keys())) if not diff and not extra: # check that the data sent was not partial unused_tags = get_unsent_codes( form_doc, response_dict.keys()) if ( unused_tags and getattr(g, 'deployment', False) and getattr(g.deployment, 'enable_partial_response_for_messages', False) ): reply = gettext( 'Thank you, but your message may be missing ' '%(unused_codes)s. You sent: %(text)s', unused_codes=', '.join(unused_tags), text=message.get('text', '')) return reply, submission, had_errors return ( _('Thank you! Your report was received!' ' You sent: {text}') .format(text=message.get('text', '')), submission, had_errors ) elif diff: # TODO: replace .format() calls had_errors = True return ( _('Unknown question codes: "{questions}". ' 'You sent: {text}') .format(questions=', '.join(sorted(diff)), text=message.get('text', '')), submission, had_errors ) elif extra: had_errors = True return (_('Invalid text sent: "{extra}". ' 'You sent: {text}').format( extra=extra, text=message.get('text', '')), submission, had_errors) else: had_errors = True if 'participant' in questionnaire.errors: return ( _('Observer ID not found. Please resend with valid ' 'Observer ID. You sent: {text}').format( text=message.get('text', '')), submission, had_errors ) else: # Save any valid data submission = questionnaire.save() return ( _('Invalid response(s) for question(s):' ' "{questions}". You sent: {text}').format( questions=', '.join(sorted( questionnaire.errors.keys())), text=message.get('text', '')), submission, had_errors ) had_errors = True return ( _('Invalid message: "{text}". Please check and resend!').format( text=message.get('text', '')), submission, had_errors )