class ChoiceQuestion (Question):
+ _state_attributes = Question._state_attributes + [
+ 'display_choices',
+ ]
+
+ def __setstate__(self, state):
+ if 'display_choices' not in state:
+ state['display_choices'] = False
+ super(ChoiceQuestion, self).__setstate__(state)
+
def check(self, answer):
correct = answer in self.answer
details = None
print(e)
from .. import error as _error
+from .. import question as _question
from . import UserInterface as _UserInterface
def _set_ps1(self):
"Pose a question and prompt"
if self.question:
- self.prompt = '\n{}\n{}'.format(
+ lines = [
+ '',
_colorize(
self.ui.colors['question'], self.question.format_prompt()),
- _colorize(self.ui.colors['prompt'], self._prompt))
+ ]
+ lines.extend(
+ _colorize(self.ui.colors['prompt'], line)
+ for line in self._extra_ps1_lines())
+ lines.append(_colorize(self.ui.colors['prompt'], self._prompt))
+ self.prompt = '\n'.join(lines)
else:
self.prompt = _colorize(self.ui.colors['prompt'], self._prompt)
"Just prompt (without the question, e.g. for multi-line answers)"
self.prompt = _colorize(self.ui.colors['prompt'], self._prompt)
+ def _extra_ps1_lines(self):
+ if (isinstance(self.question, _question.ChoiceQuestion) and
+ self.question.display_choices):
+ for i,choice in enumerate(self.question.answer):
+ yield '{}) {}'.format(i, choice)
+ return []
+
+ def _process_answer(self, answer):
+ "Back out any mappings suggested by _extra_ps1_lines()"
+ if (isinstance(self.question, _question.ChoiceQuestion) and
+ self.question.display_choices):
+ try:
+ a = int(answer)
+ return self.question.answer[a]
+ except (ValueError, IndexError):
+ pass
+ return answer
+
def default(self, line):
self.answers.append(line)
if self.question.multiline:
kwargs = {}
if self._tempdir:
kwargs['tempdir'] = self._tempdir
+ answer = self._process_answer(answer=answer)
correct,details = self.ui.process_answer(
question=self.question, answer=answer, **kwargs)
if correct:
import urllib.parse as _urllib_parse
import wsgiref.simple_server as _wsgiref_simple_server
+from .. import question as _question
from . import UserInterface as _UserInterface
if question is None:
raise HandlerError(
307, 'Temporary Redirect', headers=[('Location', '/results/')])
- if question.multiline:
+ if (isinstance(question, _question.ChoiceQuestion) and
+ question.display_choices):
+ answer_element = '\n'.join(
+ ('<input type="radio" name="answer" value="{0}"/>{0}<br/>'
+ ).format(answer)
+ for answer in question.answer)
+ elif question.multiline:
answer_element = (
'<textarea rows="5" cols="60" name="answer"></textarea>')
else: