def record_answer(self, scenario_id, answer_id): """Record this answer in the session; do any necessary updates.""" # get the correct answer answer = Answer.get_by_key_name(answer_id or '--none--') # if the lookup failed then this is not a valid answer if not answer: raise InvalidAnswerException('bad answer ID') # verify that this answer is for this scenario if not answer.scenario.key().name() == scenario_id: raise InvalidAnswerException('answer and scenario do not match') # record the answer ID self.session.setdefault(scenario_id, []) if answer_id not in self.session[scenario_id]: self.session[scenario_id] += [answer_id] # update session['completed'] if the answer is correct if answer.is_correct: self.session.setdefault('completed', {}) self.session['completed'][scenario_id] = datetime.now().isoformat()
def get(self, scenario_id): state = LearnerState(self.session) answer_id = state.last_answer_id(scenario_id) or "--none--" answer = Answer.get_by_key_name(answer_id) if answer is None: self.redirect("/%s/question" % scenario_id) return if state.is_completed(scenario_id): link_next = "/%s/disc1" % scenario_id else: link_next = "/%s/question" % scenario_id context = dict(next=self.next(), previous=self.previous(), state=state.as_string(), show_prevnext=False, correct=state.is_completed(scenario_id), response=answer.response, link_next=link_next) jt = self.jinja_environment().get_template('response.djt') self.response.write(jt.render(context))