def new(self): try: testsuite_id = int(h.escape(request.params.get('testsuite_id'))) except: redirect(url(controller='attempt', action='index')) return testsuite = Session.query(TestSuite).get(testsuite_id) if testsuite and testsuite.questions_per_test <= len(testsuite.questions): first_name = h.escape(request.params.get('first_name')) middle_name = h.escape(request.params.get('middle_name')) last_name = h.escape(request.params.get('last_name')) group = h.escape(request.params.get('group')) if len(first_name) and len(middle_name) and len(last_name) and len(group): attempt = Session.query(Attempt).filter((Attempt.first_name==first_name) & (Attempt.middle_name==middle_name) & (Attempt.last_name==last_name) & (Attempt.group==group) & (Attempt.testsuite_id==testsuite_id)).order_by(desc(Attempt.date)).first() attempt_delay = timedelta(seconds=int(config['attempt_delay'])) delta = attempt_delay if attempt: delta = datetime.now() - attempt.date if attempt is None or delta >= attempt_delay: questions_q = Session.query(Question).filter(Question.testsuite_id==testsuite.id).order_by(random()).limit(testsuite.questions_per_test) question_encoder = QuestionEncoder() test_dict = {'name': testsuite.name, 'questions': {question.id: question_encoder.default(question) for question in questions_q}} test = json.dumps(test_dict) new_attempt = Attempt(first_name=first_name, middle_name=middle_name, last_name=last_name, group=group, testsuite_id=testsuite.id, test=test) Session.add(new_attempt) Session.commit() redirect(url(controller='attempt', action='test', id=new_attempt.id)) return elif not attempt.is_attempted: redirect(url(controller='attempt', action='test', id=attempt.id)) return else: if attempt.is_attempted_correct: message = u"Вы уже успешно прошли этот тест" else: before_repeat = attempt_delay - delta before_repeat = timedelta(days=before_repeat.days, seconds=before_repeat.seconds) message = u"Вы сможете повтороно пройти тест через " + unicode(before_repeat) redirect(url(controller='attempt', action='index', message=message)) else: redirect(url(controller='attempt', action='index')) else: redirect(url(controller='attempt', action='index'))
def set_test_params(self, id): new_name = h.escape(request.params.get('name').strip()) new_number = h.escape(request.params.get('number').strip()) testsuite = Session.query(TestSuite).get(int(id)) if testsuite: if len(new_name): testsuite.name = new_name if new_number >= 0: testsuite.questions_per_test = new_number Session.commit() redirect(url(controller='tests', action='edit_test', id=testsuite.id)) else: redirect(url(controller='tests', action='index'))
def remove_test(self): id = int(h.escape(request.params.get('id'))) testsuite = Session.query(TestSuite).get(id) if testsuite: Session.delete(testsuite) Session.commit() redirect(url(controller='tests', action='index'))
def add_test(self): name = h.escape(request.params.get('name').strip()) if len(name): testsuite = TestSuite(name=name) Session.add(testsuite) Session.commit() redirect(url(controller='tests', action='index'))
def add_answer(self, id, testsuite_id): name = h.escape(request.params.get('name').strip()) is_correct = bool(h.escape(request.params.get('is_correct'))) question = Session.query(Question).get(int(id)) testsuite = Session.query(TestSuite).get(int(testsuite_id)) if question and testsuite and len(name): answer = Answer() answer.name = name answer.is_correct = is_correct answer.question_id = question.id Session.add(answer) Session.commit() redirect(url(controller='tests', action='edit_question', id=question.id, testsuite_id=testsuite.id)) elif testsuite: redirect(url(controller='tests', action='edit_test', id=testsuite.id)) else: redirect(url(controller='tests', action='index'))
def remove_question(self, id): question_id = h.escape(request.params.get('id')) question = Session.query(Question).get(int(question_id)) testsuite = Session.query(TestSuite).get(int(id)) if question and testsuite: Session.delete(question) Session.commit() redirect(url(controller='tests', action='edit_test', id=testsuite.id)) else: redirect(url(controller='tests', action='index'))
def add_question(self, id): testsuite = Session.query(TestSuite).get(int(id)) if testsuite: name = h.escape(request.params.get('name').strip()) if len(name): question = Question(name=name, testsuite_id=id) Session.add(question) Session.commit() redirect(url(controller='tests', action='edit_test', id=id)) else: redirect(url(controller='tests', action='index'))
def set_question_params(self, id, testsuite_id): new_name = h.escape(request.params.get('name').strip()) question = Session.query(Question).get(int(id)) testsuite = Session.query(TestSuite).get(int(testsuite_id)) if question and testsuite and len(new_name): question.name = new_name Session.commit() redirect(url(controller='tests', action='edit_question', id=question.id, testsuite_id=testsuite.id)) elif testsuite: redirect(url(controller='tests', action='edit_test', id=testsuite.id)) else: redirect(url(controller='tests', action='index'))
def remove_answer(self, id, testsuite_id): answer_id = h.escape(request.params.get('id')) answer = Session.query(Answer).get(int(answer_id)) question = Session.query(Question).get(int(id)) testsuite = Session.query(TestSuite).get(int(testsuite_id)) if answer and question and testsuite: Session.delete(answer) Session.commit() redirect(url(controller='tests', action='edit_question', id=question.id, testsuite_id=testsuite.id)) elif testsuite: redirect(url(controller='tests', action='edit_test', id=testsuite.id)) else: redirect(url(controller='tests', action='index'))
def save_answers(self, id, testsuite_id): question = Session.query(Question).get(int(id)) testsuite = Session.query(TestSuite).get(int(testsuite_id)) if question and testsuite: for param in request.params: if param.startswith('name'): answer_id = int(param[4:]) answer = Session.query(Answer).get(int(answer_id)) if answer: new_name = h.escape(request.params.get(param)) answer.name = new_name elif param.startswith('is_correct'): answer_id = int(param[10:]) answer = Session.query(Answer).get(int(answer_id)) if answer: new_is_correct = bool(h.escape(request.params.get(param))) answer.is_correct = new_is_correct Session.commit() redirect(url(controller='tests', action='edit_question', id=question.id, testsuite_id=testsuite.id)) elif testsuite: redirect(url(controller='tests', action='edit_test', id=testsuite.id)) else: redirect(url(controller='tests', action='index'))