def question(id): form = AnswerForm(request.form) question = Question.find(id) if not question: return random_question() if request.method == 'GET' or (request.method == 'POST' and not form.validate()): return render_template('question.html', question=question, form=form, amount=Question.size()) elif request.method == 'POST' and form.validate(): if request.form['answer'] == question.answer: flash('Correct!', 'positive') return random_question() else: flash('Wrong Answer!', 'negative') return render_template('question.html', question=question, form=form, amount=Question.size()) else: return render_template('invalid_request.html')
def test_delete_is_working(self): with DB() as database: question = Question().create() question.id = database.execute('''SELECT id FROM questions WHERE question = ?''', \ (question.question,)).fetchone()[0] with DB() as database: question.delete() self.assertEqual(Question.find(question.id), None)
def test_update_is_working(self): with DB() as database: question = Question().create() question.id = database.execute('''SELECT id FROM questions WHERE question = ?''', \ (question.question,)).fetchone()[0] with DB() as database: question.update(["NewQuestion", question.correct_answer], question.answers).edit() self.assertEqual(Question.find(question.id), question) # find Question with id question id
def edit_test(): if request.method == 'GET': return render_template('edit_test.html', tests=Test.get_all(), questions=Question.get_all()) elif request.method == 'POST': test = Test.find(int(request.form['tests'])) values = (request.form['title']) questions = [ Question.find(request.form['question_one']), Question.find(request.form['question_two']), Question.find(request.form['question_three']) ] test.update(values, questions).edit() app.logger.info("Test %s was succesfully edited", request.form['title']) return redirect('/homepage')
def delete_question(): if request.method == 'GET': return render_template('delete_question.html', questions=Question.get_all()) elif request.method == 'POST': # id is an int, which is why conversion is necessary question = Question.find(int(request.form['questions'])) # delete tests containing the deleted question Test.delete_tests_w_deleted_question(question) question.delete() app.logger.info("Question %s was successfully deleted", request.form['question']) return redirect('/homepage')
def new_test(): if request.method == 'GET': return render_template('new_test.html', questions=Question.get_all()) elif request.method == 'POST': # create a tuple containing all of the needed information given from new_test # id is none because it is autoincrement Test( None, [ Question.find(int( request.form['question_one'])), # 99% sure to bug out?? # request.form[] returns the value of select element (id) Question.find(int(request.form['question_two'])), Question.find(int(request.form['question_three'])) ], # we use find() with the id to get the object we need request.form['title']).create() app.logger.info( 'Test "%s" with questions "%s", "%s", "%s" created successfully', request.form['title'], request.form['question_one'], request.form['question_two'], request.form['question_three']) return redirect('/tests')
def edit_question(): if request.method == 'GET': return render_template('edit_question.html', questions=Question.get_all()) elif request.method == 'POST': question = Question.find(int(request.form['questions'])) values = ( request.form['question'], request.form['correct_answer'], ) answers = [ request.form['answer_one'], request.form['answer_two'], request.form['answer_three'], ] question.update(values, answers).edit() app.logger.info("Question %s was edited successfully", request.form['question']) return redirect('/homepage')