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 __call__(self, environ, start_response): """Invoke the Controller""" # WSGIController.__call__ dispatches to the Controller method # the request is routed to. This routing information is # available in environ['pylons.routes_dict'] try: return WSGIController.__call__(self, environ, start_response) finally: Session.remove()
def edit_question(self, id, testsuite_id): question = Session.query(Question).get(int(id)) testsuite = Session.query(TestSuite).get(int(testsuite_id)) if question and testsuite: c.max_name_length = 50 c.question = question return render('/admin/tests/edit_question.html') elif testsuite: 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_password(self): new_password = request.params.get('new_password') repeat_password = request.params.get('repeat_password') if new_password == repeat_password: identity = request.environ.get('repoze.who.identity') user = identity['user'] if user: user._set_password(new_password) Session.commit() redirect(url(controller='tests', action='index')) else: redirect(url(controller='account', action='change_password'))
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 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 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()
def show(self, id): attempt = Session.query(Attempt).get(int(id)) if attempt and attempt.is_attempted: c.attempt = attempt return render('/admin/results/show.html') else: redirect(url(controller='results', action='index'))
def test(self, id): attempt = Session.query(Attempt).get(int(id)) if attempt and not attempt.is_attempted: c.attempt_id = attempt.id c.test = json.loads(attempt.test) return render('/attempt/test.html') else: redirect(url(controller='attempt', action='index'))
def edit_test(self, id): testsuite = Session.query(TestSuite).get(int(id)) if testsuite: c.questions = testsuite.questions c.testsuite = testsuite return render('/admin/tests/edit_test.html') else: 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 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 check(self, id): attempt = Session.query(Attempt).get(int(id)) if attempt: if not attempt.is_attempted: test = json.loads(attempt.test) correct_answers = [] for question_id in test['questions']: for answer_id in test['questions'][unicode(question_id)]['answers']: if test['questions'][unicode(question_id)]['answers'][unicode(answer_id)]['is_correct']: correct_answers.append(answer_id) correct = 0 wrong = 0 for param in request.params: if param in correct_answers: correct += 1 else: wrong += 1 attempt.result = json.dumps([param for param in request.params]) attempt.date = datetime.now() attempt.is_attempted = True mistakes_num = wrong + len(correct_answers) - correct attempt.is_attempted_correct = (mistakes_num <= int(config['max_mistakes_num'])) Session.commit() if attempt.is_attempted_correct: message = u"Вы успешно прошли этот тест" else: message = u"Вам не удалось пройти тест" redirect(url(controller='attempt', action='index', message=message)) elif attempt.is_attempted_correct: message = u"Вы уже успешно прошли этот тест" redirect(url(controller='attempt', action='index', message=message)) else: redirect(url(controller='attempt', action='index')) else: redirect(url(controller='attempt', 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_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 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'))
def init_model(engine): """Call me before using any of the tables or classes in the model""" Session.configure(bind=engine)
def index(self): c.message = request.params.get('message') if not hasattr(c, 'message'): c.message = u"" c.tests = Session.query(TestSuite).all() return render('/attempt/index.html')
def index(self): c.tests = Session.query(TestSuite).order_by(asc(TestSuite.id)).all() return render('/admin/tests/index.html')
def index(self): c.attempts = Session.query(Attempt).order_by(desc(Attempt.date)).all() return render('/admin/results/index.html')