Skip to content

Question utils

question.utils:

question_by_key(key, questions, is_skippable=None, drop_choices=[])

Return question by given key

Source code in question/utils.py
19
20
21
22
23
24
25
26
27
28
29
30
31
def question_by_key(key, questions, is_skippable=None, drop_choices=[]):
    """Return question by given key"""
    for question in questions:
        if question.key == key:
            q = deepcopy(question)
            # Question is_skippable
            if is_skippable is not None:
                q.is_skippable = is_skippable
            if hasattr(question, 'choices') and len(drop_choices):
                for choice in drop_choices:
                    q.choices.pop(choice, None)
            return q
    return None

total_unanswered_questions(participant, questions)

Return how many questions have not been answered yet by the participant

Source code in question/utils.py
13
14
15
16
def total_unanswered_questions(participant, questions):
    """ Return how many questions have not been answered yet by the participant"""
    profile_questions = participant.profile().values_list('question_key', flat=True)
    return len([question for question in questions if question.key not in profile_questions])

unanswered_questions(participant, questions, randomize=False, cutoff_index=None)

Generator to give next unasked profile question and prepare its result - participant: participant who will be checked for unanswered questions - questions: list of questions from which to select an unanswered question - optionally, randomize order of questions

Source code in question/utils.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def unanswered_questions(participant, questions, randomize=False, cutoff_index=None):
    """Generator to give next unasked profile question and prepare its result
    - participant: participant who will be checked for unanswered questions
    - questions: list of questions from which to select an unanswered question
    - optionally, randomize order of questions
    """
    if randomize:
        random.shuffle(questions)
    for question in questions[:cutoff_index]:
        profile_result = prepare_profile_result(question.key, participant)
        if profile_result.given_response is None:
            q = deepcopy(question)
            q.result_id = profile_result.pk
            yield q