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 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 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 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 setup_app(command, conf, vars): """Place any commands to setup bzdtests here""" # Don't reload the app if it was loaded under the testing environment if not pylons.test.pylonsapp: load_environment(conf.global_conf, conf.local_conf) # Create the tables if they don't already exist Base.metadata.create_all(bind=Session.bind) log.info("Adding initial users, groups and permissions...") g = Group() g.name = u'admin' Session.add(g) p = Permission() p.name = u'admin' p.groups.append(g) Session.add(p) u = User() u.username = u'admin' u.fullname = u'admin' u._set_password('admin') u.email = u'*****@*****.**' u.groups.append(g) Session.add(u) Session.commit()