From fa5763946c3b19a737739878ca7c0b9019c5dfa1 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 14 Mar 2013 07:54:50 -0400 Subject: [PATCH] quizzer: Add ChoiceQuestion.open_ended This allows us to ask open-ended multiple-choice questions, like: Pick one: 1) Answer-1 2) Answer-2 or fill in something else quizzer? Answer-3 --- quizzer/question.py | 8 +++++--- quizzer/ui/cli.py | 2 ++ quizzer/ui/wsgi.py | 21 ++++++++++++++++----- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/quizzer/question.py b/quizzer/question.py index a4da655..cc4cfff 100644 --- a/quizzer/question.py +++ b/quizzer/question.py @@ -108,15 +108,17 @@ class NormalizedStringQuestion (Question): class ChoiceQuestion (Question): _state_attributes = Question._state_attributes + [ 'display_choices', + 'open_ended', ] def __setstate__(self, state): - if 'display_choices' not in state: - state['display_choices'] = False + for key in ['display_choices', 'open_ended']: + if key not in state: + state[key] = False super(ChoiceQuestion, self).__setstate__(state) def check(self, answer): - correct = answer in self.answer + correct = answer in self.answer or self.open_ended details = None if not correct: details = 'answer ({}) is not in list of expected values'.format( diff --git a/quizzer/ui/cli.py b/quizzer/ui/cli.py index 7b857b0..e91a0dd 100644 --- a/quizzer/ui/cli.py +++ b/quizzer/ui/cli.py @@ -90,6 +90,8 @@ class QuestionCommandLine (_cmd.Cmd): self.question.display_choices): for i,choice in enumerate(self.question.answer): yield '{}) {}'.format(i, choice) + if self.question.open_ended: + yield 'or fill in something else' return [] def _process_answer(self, answer): diff --git a/quizzer/ui/wsgi.py b/quizzer/ui/wsgi.py index c29323c..de2c595 100644 --- a/quizzer/ui/wsgi.py +++ b/quizzer/ui/wsgi.py @@ -244,10 +244,15 @@ class QuestionApp (WSGI_DataObject): 307, 'Temporary Redirect', headers=[('Location', '/results/')]) if (isinstance(question, _question.ChoiceQuestion) and question.display_choices): - answer_element = '\n'.join( + choices = [ ('{0}
' ).format(answer) - for answer in question.answer) + for answer in question.answer] + if question.open_ended: + choices.extend([ + '', + '']) + answer_element = '\n'.join(choices) elif question.multiline: answer_element = ( '') @@ -293,10 +298,16 @@ class QuestionApp (WSGI_DataObject): question = self.ui.quiz.get(id=question_id) except KeyError as e: raise HandlerError(404, 'Not Found') from e - if question.multiline: + if (isinstance(question, _question.ChoiceQuestion) and + question.display_choices and + question.open_ended and + raw_answer == 'open_ended'): + answer = print_answer = data.get('answer-other', None) + elif question.multiline: answer = raw_answer.splitlines() + print_answer = raw_answer else: - answer = raw_answer + answer = print_answer = raw_answer correct,details = self.ui.process_answer( question=question, answer=answer, user=user) link_target = '../question/' @@ -323,7 +334,7 @@ class QuestionApp (WSGI_DataObject): '

Answer

', '

{}

'.format( question.format_prompt(newline='
')), - '
{}
'.format(raw_answer), + '
{}
'.format(print_answer), '

{}

'.format(correct_msg), details or '', '
'.format( -- 2.26.2