Spaces:
Sleeping
Sleeping
Add application file
Browse files- app/routes.py +3 -57
app/routes.py
CHANGED
|
@@ -1,66 +1,17 @@
|
|
|
|
|
| 1 |
from app import app
|
| 2 |
-
from
|
| 3 |
-
from werkzeug.urls import url_parse
|
| 4 |
-
from app.forms import LoginForm, RegistrationForm, QuestionForm
|
| 5 |
-
from app.models import User, Questions
|
| 6 |
-
from app import db
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
@app.before_request
|
| 10 |
-
def before_request():
|
| 11 |
-
g.user = None
|
| 12 |
-
|
| 13 |
-
if 'user_id' in session:
|
| 14 |
-
user = User.query.filter_by(id=session['user_id']).first()
|
| 15 |
-
g.user = user
|
| 16 |
|
| 17 |
@app.route('/')
|
| 18 |
def home():
|
| 19 |
return render_template('index.html', title='Home')
|
| 20 |
|
| 21 |
-
@app.route('/login', methods=['GET', 'POST'])
|
| 22 |
-
def login():
|
| 23 |
-
form = LoginForm()
|
| 24 |
-
if form.validate_on_submit():
|
| 25 |
-
user = User.query.filter_by(username=form.username.data).first()
|
| 26 |
-
if user is None or not user.check_password(form.password.data):
|
| 27 |
-
return redirect(url_for('login'))
|
| 28 |
-
session['user_id'] = user.id
|
| 29 |
-
session['marks'] = 0
|
| 30 |
-
next_page = request.args.get('next')
|
| 31 |
-
if not next_page or url_parse(next_page).netloc != '':
|
| 32 |
-
next_page = url_for('home')
|
| 33 |
-
return redirect(next_page)
|
| 34 |
-
return redirect(url_for('home'))
|
| 35 |
-
if g.user:
|
| 36 |
-
return redirect(url_for('home'))
|
| 37 |
-
return render_template('login.html', form=form, title='Login')
|
| 38 |
-
|
| 39 |
-
@app.route('/register', methods=['GET', 'POST'])
|
| 40 |
-
def register():
|
| 41 |
-
form = RegistrationForm()
|
| 42 |
-
if form.validate_on_submit():
|
| 43 |
-
user = User(username=form.username.data, email=form.password.data)
|
| 44 |
-
user.set_password(form.password.data)
|
| 45 |
-
db.session.add(user)
|
| 46 |
-
db.session.commit()
|
| 47 |
-
session['user_id'] = user.id
|
| 48 |
-
session['marks'] = 0
|
| 49 |
-
return redirect(url_for('home'))
|
| 50 |
-
if g.user:
|
| 51 |
-
return redirect(url_for('home'))
|
| 52 |
-
return render_template('register.html', title='Register', form=form)
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
@app.route('/question/<int:id>', methods=['GET', 'POST'])
|
| 57 |
def question(id):
|
| 58 |
form = QuestionForm()
|
| 59 |
q = Questions.query.filter_by(q_id=id).first()
|
| 60 |
if not q:
|
| 61 |
return redirect(url_for('score'))
|
| 62 |
-
if not g.user:
|
| 63 |
-
return redirect(url_for('login'))
|
| 64 |
if request.method == 'POST':
|
| 65 |
option = request.form['options']
|
| 66 |
if option == q.ans:
|
|
@@ -69,19 +20,14 @@ def question(id):
|
|
| 69 |
form.options.choices = [(q.a, q.a), (q.b, q.b), (q.c, q.c), (q.d, q.d)]
|
| 70 |
return render_template('question.html', form=form, q=q, title='Question {}'.format(id))
|
| 71 |
|
| 72 |
-
|
| 73 |
@app.route('/score')
|
| 74 |
def score():
|
| 75 |
-
if not g.user:
|
| 76 |
-
return redirect(url_for('login'))
|
| 77 |
g.user.marks = session['marks']
|
| 78 |
# db.session.commit()
|
| 79 |
return render_template('score.html', title='Final Score')
|
| 80 |
|
| 81 |
@app.route('/logout')
|
| 82 |
def logout():
|
| 83 |
-
if not g.user:
|
| 84 |
-
return redirect(url_for('login'))
|
| 85 |
session.pop('user_id', None)
|
| 86 |
session.pop('marks', None)
|
| 87 |
-
return redirect(url_for('home'))
|
|
|
|
| 1 |
+
from flask import render_template, request, redirect, url_for, session, g
|
| 2 |
from app import app
|
| 3 |
+
from app.models import Questions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
@app.route('/')
|
| 6 |
def home():
|
| 7 |
return render_template('index.html', title='Home')
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
@app.route('/question/<int:id>', methods=['GET', 'POST'])
|
| 10 |
def question(id):
|
| 11 |
form = QuestionForm()
|
| 12 |
q = Questions.query.filter_by(q_id=id).first()
|
| 13 |
if not q:
|
| 14 |
return redirect(url_for('score'))
|
|
|
|
|
|
|
| 15 |
if request.method == 'POST':
|
| 16 |
option = request.form['options']
|
| 17 |
if option == q.ans:
|
|
|
|
| 20 |
form.options.choices = [(q.a, q.a), (q.b, q.b), (q.c, q.c), (q.d, q.d)]
|
| 21 |
return render_template('question.html', form=form, q=q, title='Question {}'.format(id))
|
| 22 |
|
|
|
|
| 23 |
@app.route('/score')
|
| 24 |
def score():
|
|
|
|
|
|
|
| 25 |
g.user.marks = session['marks']
|
| 26 |
# db.session.commit()
|
| 27 |
return render_template('score.html', title='Final Score')
|
| 28 |
|
| 29 |
@app.route('/logout')
|
| 30 |
def logout():
|
|
|
|
|
|
|
| 31 |
session.pop('user_id', None)
|
| 32 |
session.pop('marks', None)
|
| 33 |
+
return redirect(url_for('home'))
|