Beispiel #1
0
    def test_parse_comment_message(self):
        faker = Generic()

        # test with comments in the locales below
        # we're using Farsi instead of Arabic
        # as mimesis doesn't support Arabic yet ):
        for locale in ['EN', 'ES', 'FA', 'FR', 'RU']:
            current_locale = getattr(locales, locale)
            test_participant_id = faker.person.identifier('######')

            with self.subTest(current_locale=current_locale):
                with faker.text.override_locale(current_locale):
                    test_comment = faker.text.sentence()
                    sample_text = 'XA{}AA1AB2@{}'.format(
                        test_participant_id, test_comment)

                    result = parse_text(sample_text)
                    (prefix, participant_id, exclamation, form_serial,
                     responses, comment) = result

                    self.assertEqual(prefix, 'XA')
                    self.assertEqual(participant_id, test_participant_id)
                    self.assertFalse(exclamation)
                    self.assertEqual(responses, 'AA1AB2')
                    self.assertEqual(comment, test_comment)
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)
Beispiel #3
0
    def test_parse_survey_message(self):
        sample_text = 'TC111111X321AA2'
        prefix, participant_id, exclamation, form_serial, responses, comment = \
            parse_text(sample_text)

        self.assertEqual(prefix, 'TC')
        self.assertEqual(participant_id, '111111')
        self.assertFalse(exclamation)
        self.assertEqual(form_serial, '321')
        self.assertEqual(responses, 'AA2')
        self.assertIsNone(comment)
Beispiel #4
0
    def test_parse_invalid_message(self):
        sample_text = '2014'
        prefix, participant_id, exclamation, form_serial, responses, comment = \
            parse_text(sample_text)

        self.assertIsNone(prefix)
        self.assertIsNone(participant_id, sample_text)
        self.assertIsNone(exclamation)
        self.assertIsNone(form_serial)
        self.assertIsNone(responses)
        self.assertIsNone(comment)
Beispiel #5
0
def lookup_participant(msg, event=None):
    participant = None
    unused, participant_id, unused, unused, unused = parse_text(msg.text)

    evt = getattr(g, 'event', None) or event

    if participant_id:
        participant = services.participants.get(event=evt,
                                                participant_id=participant_id)

    if not participant:
        try:
            clean_number = msg.sender.replace('+', '')
            participant = services.participants.get(
                event=evt, phones__number__contains=clean_number)
        except services.participants.__model__.MultipleObjectsReturned:
            participant = None

    return participant
Beispiel #6
0
def lookup_participant(msg, event=None):
    participant = None
    unused, participant_id, unused, unused, unused = parse_text(msg.text)

    evt = getattr(g, 'event', None) or event

    if participant_id:
        participant = services.participants.get(
            event=evt,
            participant_id=participant_id
        )

    if not participant:
        try:
            clean_number = msg.sender.replace('+', '')
            participant = services.participants.get(
                event=evt,
                phones__number__contains=clean_number
            )
        except services.participants.__model__.MultipleObjectsReturned:
            participant = None

    return participant
Beispiel #7
0
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
    )
Beispiel #8
0
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)
Beispiel #9
0
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
    )