def edit_question(self): self.response['category'] = self.request.matchdict['category'] Categories.by(self.response['category'], user=self.request.user, permission=ACL.EDIT, strict=True).first() # security question = self.request.matchdict['id'] self.response['option'] = question if question == 'new': if 'form.submit' in self.request.params or 'form.submit.next' in self.request.params: qs = QuestionSets(category_id=int(self.response['category'])) DBSession.add(qs) DBSession.flush() qs = QuestionSets.by(None, sort='id desc').first() id = qs.id self._transaction(qs, self.request.params) if 'form.submit.next' in self.request.params: return HTTPFound(location=self.request.application_url + self.request.path) return HTTPFound(location=self.request.application_url + self.request.path + '/../' + str(id)) else: qs = QuestionSets.by(question).first() q = Questions.by({'question_sets_id':qs.id}).all() wa = Answers.by({'question_sets_id':qs.id,'is_correct':False}, sort='position asc').all() ca = Answers.by({'question_sets_id':qs.id,'is_correct':True}).first() self.response['question_sets'] = qs self.response['questions'] = q self.response['wrong_answers'] = wa self.response['correct_answer'] = ca if 'form.submit' in self.request.params or 'form.submit.next' in self.request.params: self.notify('Changes saved!') self._transaction(qs, self.request.params) if 'form.submit.next' in self.request.params: return HTTPFound(location=self.request.application_url + self.request.path + '/../new') return HTTPFound(location=self.request.application_url + self.request.path) return self.template('/edit-question.pt', theme='AdminPanel')
def export_category(self): category_id = int(self.request.matchdict['category']) category = Result2Dict(Categories.by(category_id).first()) sa_qs = QuestionSets.by({'category_id':category_id}).all() question_sets = [] for qs in sa_qs: questions = [] answers = [] # Get QuestionSet Transform d_qs = Result2Dict(qs) # Get All Questions in QuestionSet and Transform sa_q = Questions.by({'question_sets_id':qs.id}).all() for q in sa_q: questions.append(Result2Dict(q)) d_qs['questions'] = questions # Get All Answers in QuestionSet and Transform sa_a = Answers.by({'question_sets_id':qs.id}).all() for a in sa_a: answers.append(Result2Dict(a)) d_qs['answers'] = answers question_sets.append(d_qs) return self._export_zip(json.dumps({'category' : category, 'question_sets' : question_sets}, sort_keys=True, indent=4, separators=(',', ': ') ))
def edit_questions(self): category = self.request.matchdict['category'] self.response['active_category'] = Categories.by(category, user=self.request.user, permission=ACL.EDIT, strict=True).first() question_sets = QuestionSets.by({'category_id':category}).all() self.response['question_sets'] = [] i = 1 for qs in question_sets: self.response['question_sets'].append({ 'rank' : i, 'id' : qs.id, 'answer_help' : qs.answer_help, 'questions' : Questions.by({'question_sets_id':qs.id}).all(), 'wrong_answers' : Answers.by({'question_sets_id':qs.id,'is_correct':False}).all(), 'correct_answer' : Answers.by({'question_sets_id':qs.id,'is_correct':True}).first(), }) i += 1 return self.template('/edit-questions.pt', theme='AdminPanel')
def _transaction(self, question_set, fields): for key,v in fields.iteritems(): if Validate.sanatize(v) != '': parts = key.split('_') if parts[0] == 'answerhelp': question_set.answer_help = v if parts[0] == 'correctanswer' and not key.endswith('_index'): if parts[1] == 'old': a = Answers.by(parts[2]).first() a.answer = v a.position=fields[key + '_index'] else: a = Answers(question_sets_id=question_set.id, answer=v, is_correct=True, position=fields[key + '_index']) DBSession.add(a) if parts[0] == 'wronganswer' and not key.endswith('_index'): if parts[1] == 'old': a = Answers.by(parts[2]).first() a.answer = v a.position = fields[key + '_index'] else: a = Answers(question_sets_id=question_set.id, answer=v, is_correct=False, position=fields[key + '_index']) DBSession.add(a) if parts[0] == 'question': if parts[1] == 'old': a = Questions.by(parts[2]).first() a.question = v else: a = Questions(question=v, question_sets_id=question_set.id) DBSession.add(a) DBSession.flush() transaction.commit()
def _get_question_variation(self, questionset, last_test): if last_test == None: # First test has no past variations so just randomized return self._random_question(questionset) else: # Use next variation and wrap back to beginning when at end past_question = TestsResults.by({'tests_id':last_test.id, 'question_sets_id':questionset.id}).first() questions = Questions.by({'question_sets_id':questionset.id}).all() for i, question in enumerate(questions): if past_question == None: return self._random_question(questionset) if past_question.question == question.question: if (i+1) >= len(questions): return questions[0].question return questions[i+1].question # purely defensive, should never hit this... return self._random_question(questionset)
def _random_question(self,questionset): questions = Questions.by({'question_sets_id':questionset.id}).all() random.shuffle(questions) return questions[0].question