-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
122 lines (107 loc) · 4.54 KB
/
app.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from flask import Flask, render_template, g, request, jsonify, redirect, flash
import sqlite3
from random import randint
import datetime
from flask_wtf import FlaskForm
from wtforms import StringField, RadioField, TextAreaField, SelectField
from wtforms.validators import DataRequired
app = Flask(__name__)
app.config["SECRET_KEY"] = "secretkey"
database = "database.db"
class AddQuestionForm(FlaskForm):
subject = SelectField("Subject", choices=["Computing", "Math", "Physics", "Chemistry", "Economics", "English"], validators=[DataRequired()])
question = TextAreaField("Question", validators=[DataRequired()])
option1 = StringField("Option 1", validators=[DataRequired()])
option2 = StringField("Option 2", validators=[DataRequired()])
option3 = StringField("Option 3", validators=[DataRequired()])
option4 = StringField("Option 4", validators=[DataRequired()])
correct_option = RadioField("Correct option", choices=["Option 1", "Option 2", "Option 3", "Option 4"], validators=[DataRequired()])
def get_db():
db = getattr(g, "_database", None)
if db is None:
db = g._database = sqlite3.connect(database)
db.row_factory = sqlite3.Row
return db
def query_db(query, args=(), one=False):
cur = get_db().execute(query, args)
rv = cur.fetchall()
cur.close()
return (rv[0] if rv else None) if one else rv
def insert_db(query, args=()):
conn = get_db()
conn.execute(query, args)
conn.commit()
conn.close()
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, "_database", None)
if db is not None:
db.close()
def split_list(lst, n):
new_lst = []
for i in range(0, len(lst)-1, n):
new_lst.append(lst[i:i+n])
return new_lst
@app.route("/", methods=["GET", "POST"])
def index():
return render_template("index.html")
@app.route("/get_posts", methods=["POST"])
def get_posts():
page_num = request.json.get('pageNum', 0)
print(page_num)
posts = query_db("SELECT * FROM posts ORDER BY created_utc DESC;")
posts = [
{
**dict(post),
'created_utc': datetime.datetime.fromtimestamp(post["created_utc"])
}
for post in posts
]
posts_list = split_list(posts, 5)
posts = posts_list[page_num % len(posts_list)]
return jsonify(posts)
@app.route("/questions")
def questions():
questions = query_db("SELECT * FROM questions;")
questions = [dict(question) for question in questions]
questions_dict = {}
for question in questions:
subject = question["subject"]
question_dict = {"subject": question["subject"], "question": question["question"], "options": [question["option1"], question["option2"], question["option3"], question["option4"]], "correct": question["correct_option"]}
if subject not in questions_dict:
questions_dict[subject] = []
questions_dict[subject].append(question_dict)
questions_list = list(questions_dict.items())
print(questions_list)
return render_template("questions.html", questions_list=questions_list)
@app.route("/add_questions", methods=["GET", "POST"])
def add_questions():
form = AddQuestionForm()
if form.validate_on_submit():
subject = request.form.get("subject")
question = request.form.get("question")
option1 = request.form.get("option1")
option2 = request.form.get("option2")
option3 = request.form.get("option3")
option4 = request.form.get("option4")
correct_option = request.form.get("correct_option")
try:
correct_option = "1234".index(correct_option[-1])+1
insert_db("INSERT INTO questions (subject, question, option1, option2, option3, option4, correct_option) VALUES (?, ?, ?, ?, ?, ?, ?);", (subject, question, option1, option2, option3, option4, correct_option))
flash("Question submitted!")
except ValueError:
pass
return redirect("/add_questions")
return render_template("add_questions.html", form=form)
@app.route("/get_question")
def get_question():
questions = query_db("SELECT * FROM questions;")
questions = [dict(question) for question in questions]
idx = randint(0, len(questions)-1)
question = questions[idx]
question_dict = {"subject": question["subject"], "question": question["question"], "options": [question["option1"], question["option2"], question["option3"], question["option4"]], "correct": question["correct_option"]}
return jsonify(question_dict)
if __name__ == "__main__":
app.run(debug=True)
def create_app():
return app