Beispiel #1
0
def get_responses_from_db_grouped_by_steps(bceid_user, hide_failed_conditionals=False):
    """
    Group questions and responses by steps to which they belong

    `hide_failed_conditionals` goes through the responses after grouping and
    tests their conditionality.  If they fail, the response is blanked (this is
    to hide conditional responses that are no longer applicable but haven't been
    erased, mainly for the question review page).
    """
    married, married_questions, responses = __get_data(bceid_user)
    responses_dict = {}

    for step, questions in question_step_mapping.items():

        lst = []
        step_responses = responses.filter(question_id__in=questions).exclude(
            value__in=['', '[]', '[["",""]]']).order_by('question')

        for answer in step_responses:
            if not married and answer.question_id in married_questions:
                value = ''
            else:
                value = answer.value

            lst += [{'question__conditional_target': answer.question.conditional_target,
                     'question__reveal_response': answer.question.reveal_response,
                     'value': value,
                     'question__name': answer.question.name,
                     'question__required': answer.question.required,
                     'question_id': answer.question.pk}]

        # This was added for DIV-514, where the user entered a name change for
        # their spouse but then said 'no', they won't be changing their name.
        # Since we don't blank related answers, we need to hide it dynamically.
        # This only works for questions in the same step.
        if hide_failed_conditionals:
            values = {q['question_id']: q['value'] for q in lst}
            for q in lst:
                if q['question__required'] != 'Conditional':
                    continue
                target = q['question__conditional_target']
                if target not in values:
                    continue
                numeric_condition = evaluate_numeric_condition(values[target], q['question__reveal_response'])
                if numeric_condition is None:
                    if q['question__reveal_response'] and q['question__reveal_response'] != values[target]:
                        q['value'] = ''
                elif numeric_condition is False:
                    q['value'] = ''

        responses_dict[step] = lst

    return responses_dict
Beispiel #2
0
def _condition_met(target_response, reveal_response):
    # check whether using a numeric condition
    numeric_condition_met = evaluate_numeric_condition(target_response, reveal_response)
    if numeric_condition_met is None:
        # handle special negation options. ex) '!NO' matches anything but 'NO'
        if reveal_response.startswith('!'):
            if target_response == "" or target_response.lower() == reveal_response[1:].lower():
                return False
        elif str(target_response).lower() != reveal_response.lower():
            return False
    elif numeric_condition_met is False:
        return False
    return True