This repository has been archived by the owner on Jun 6, 2021. It is now read-only.
/
routes.py
65 lines (53 loc) · 2.42 KB
/
routes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask, flash, redirect, render_template, request, url_for
from app import app
from question import Question, NewQuestionForm, AnswerForm
@app.route('/')
def index():
return render_template('index.html')
@app.route('/randomQuestion')
def random_question():
if Question.random_id():
return redirect(url_for('question', id=Question.random_id()))
else:
flash('No questions found! Please create a new one.', 'negative')
return new_question()
@app.route('/question/<id>', methods=['GET', 'POST'])
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')
@app.route('/questions/new', methods=['GET'])
def new_question():
form = NewQuestionForm(request.form)
if request.method == 'GET':
return render_template('question_new.html', form=form, amount=Question.size())
else:
return render_template('invalid_request.html')
@app.route('/questions', methods=['POST'])
def questions():
form = NewQuestionForm(request.form)
if request.method == 'POST' and form.validate():
question = Question(form.category.data, form.question.data,
form.option_a.data, form.option_b.data,
form.option_c.data, form.option_d.data,
form.answer.data)
question.save()
flash('Question successfully created!', 'positive')
return render_template('question_created.html', question=question, amount=Question.size())
elif request.method == 'POST' and not form.validate():
flash('Oops, your submitted question appears to be invalid.', 'negative')
return render_template('question_new.html', form=form, amount=Question.size())
else:
return render_template('invalid_request.html')