-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
301 lines (244 loc) · 9.65 KB
/
main.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from flask import Flask, request, url_for, flash, g
from flask import render_template
from markupsafe import Markup
from werkzeug.utils import redirect
from flask_login import LoginManager, login_required, logout_user, login_user, current_user
from badge_system import get_badges, get_badge_names
from leaderboard import get_top_ten_score, get_top_ten_accuracy
from db_interaction import *
from feedback import *
import settings
app = Flask(__name__)
app.secret_key = settings.SECRET_KEY
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'home'
login_manager.login_message = 'Please log in to view this'
login_manager.login_message_category = 'error'
@app.before_request
def before_request():
g.user = current_user
@app.context_processor
def moderator():
def mod(user):
if 'AnonymousUserMixin' in (str)(user):
return False
if user is None:
return False
if not user:
return False
if user.is_authenticated():
return is_moderator(user)
return False
return dict(is_moderator=mod)
@login_manager.user_loader
def load_user(uid):
return get_user_by_uid(uid)
@app.route('/', methods=['GET'])
def home():
if current_user.is_active:
return redirect(url_for('dashboard'))
return render_template('homepage.html')
@app.route('/signup', methods=['POST'])
def signup():
error = ''
if request.method == 'POST':
username = [request.form['uname']][0].lower()
fullname = [request.form['fname']][0]
password = [request.form['pwd']][0]
if is_taken(username):
error = 'Username is already taken, You\'ll have to pick a new one'
elif (len(username) < 4) or (' ' in username) or len(username) > 30:
error = 'Usernames must be at least 4 characters in length, contain no spaces, and a maximum of 30 characters'
elif (len(password) < 4):
error = 'Password must be at least 4 characters'
else:
create_user(username, fullname, password)
message = 'Account Successfully Created!'
flash(message, 'success')
return redirect(url_for('home'))
flash(error, 'error')
return render_template('homepage.html')
else:
return render_template('homepage.html', error=error)
@app.route('/signin', methods=['POST', 'GET'])
def signin():
error = ''
if request.method == 'POST':
username = [request.form['uname']][0].lower()
password = [request.form['pwd']][0]
if not is_taken(username):
error = '%s does not exist' % username
flash(error, 'error')
return redirect(url_for('home'))
if not is_valid(username, password):
error = 'Incorrect Username/Password'
flash(error, 'error')
return redirect(url_for('home'))
else:
u = get_user_by_username(username)
login_user(u)
flash("Successfully Logged In!")
return redirect(url_for('dashboard'))
return redirect(url_for('home'))
@app.route('/dashboard', methods=['GET'])
@login_required
def dashboard():
rank = compute_rank(current_user)
data = correct_and_total_num(current_user.username)
return render_template('dashboard.html', number_correct=data[0], number_attempted=data[1], rank=rank,
overtime_pts=get_overtime_pts(current_user.uid))
@app.route('/logout', methods=['GET'])
def logout():
logout_user()
return redirect(url_for('home'))
@app.route('/search', methods=['GET', 'POST'])
@login_required
def search():
users_found = []
if request.method == 'POST':
uname = [request.form['uname']][0]
users_found = search_by_username(uname)
if users_found.count() > 0:
flash(str(users_found.count()) + ' users were found', 'success')
else:
flash(Markup('<strong>0</strong> users were found'), 'error')
return render_template("search.html", users_found=users_found)
else:
return render_template("search.html")
@app.route('/user/<username>', methods=['GET'])
@login_required
def profile(username):
user_prof = get_user_by_username(username)
rank = compute_rank(user_prof)
return render_template('profile.html', user=user_prof, rank=rank, badges=get_badges(user_prof.uid),
badgenames=get_badge_names())
@app.route('/leaderboard', methods=['GET'])
@login_required
def leaderboard():
top_ten = get_top_ten_score()
accurate = get_top_ten_accuracy()
return render_template('leaderboard.html', top_ten=top_ten, accurate=accurate)
@app.route('/feedback', methods=['GET', 'POST'])
def feedback():
if request.method == 'GET':
return render_template('feedback.html', user=current_user)
if request.method == 'POST':
fullname = current_user.fullname
username = current_user.username
message = [request.form['message']][0]
new_feedback(fullname, username, message)
flash('Thanks for the Feedback!', 'success')
return redirect(url_for('dashboard'))
# Computer Science Methods:
@app.route('/cs', methods=['GET'])
@login_required
def cs():
return render_template('cs.html')
@app.route('/cs/new', methods=['GET', 'POST'])
@login_required
def cs_question():
question = get_random_question('cs')
return redirect('/cs/question/' + (str)(question.qid))
@app.route('/cs/question/<qid>', methods=['GET', 'POST'])
def cs_question_specific(qid):
question = get_question_by_qid(qid)
question.explanation = (str)(question.explanation)
return render_template('question.html', question=question, subject='cs')
# Submit API
@app.route('/submit/', methods=['POST'])
@login_required
def submit():
isCor = [request.form['isCor']][0]
qid = [request.form['qid']][0]
if get_last_question(current_user) == int(qid):
return 'Question not accounted for - Previously answered'
elif isCor == 'true':
correct(current_user, qid)
return 'Question accounted as correct'
elif isCor == 'false':
incorrect(current_user, qid)
return 'Question accounted as incorrect'
return 'Question not accounted for'
@app.route('/math', methods=['GET'])
@login_required
def math():
return render_template('math.html')
@app.route('/math/new', methods=['GET', 'POST'])
@login_required
def math_question():
question = get_random_question('math')
return redirect('/math/question/' + (str)(question.qid))
@app.route('/math/question/<qid>', methods=['GET', 'POST'])
def math_question_specific(qid):
question = get_question_by_qid(qid)
question.explanation = (str)(question.explanation)
return render_template('question.html', question=question, subject='math')
@app.route('/admin', methods=['GET', 'POST'])
@login_required
def admin():
if not current_user.is_authenticated or not is_moderator(current_user):
flash('You are not allowed here', 'error')
return redirect(url_for('home'))
if request.method == 'POST':
questionheader = [request.form['questionheader']][0]
questiontext = [request.form['questiontext']][0]
answerchoices = [request.form['answerchoices']][0]
correctanswer = [request.form['correctanswer']][0]
explanation = [request.form['explanation']][0]
questionid = [request.form['questionid']][0]
subject = [request.form['subject']][0]
if questionid == '':
add_question(questionheader, questiontext, answerchoices, (int)(correctanswer), (str)(explanation),
(str)(subject))
else:
update_question(questionid, questionheader, questiontext, answerchoices, (int)(correctanswer),
(str)(explanation), (str)(subject))
flash('Added New Question!', 'success')
return render_template('admin.html', database_stats=get_table_amts(), flagranks=get_top_flagged(),
feedback=get_feedback())
else:
return render_template('admin.html', database_stats=get_table_amts(), flagranks=get_top_flagged(),
feedback=get_feedback())
@app.route('/admin/clearflags', methods=['POST', 'GET'])
@login_required
def admin_clearflags():
if not current_user.is_authenticated or not is_moderator(current_user):
flash('You are not allowed here', 'error')
return redirect(url_for('home'))
if request.method == 'POST':
clear_flags()
flash('Cleared Flags', 'success')
return redirect(url_for('admin'))
else:
flash('Please Use the clear button instead of navigating here', 'success')
return redirect(url_for('admin'))
@app.route('/admin/clearfeedback', methods=['POST', 'GET'])
@login_required
def admin_clearfeedback():
if not current_user.is_authenticated or not is_moderator(current_user):
flash('You are not allowed here', 'error')
return redirect(url_for('home'))
if request.method == 'POST':
clear_feedback()
flash('Cleared Feedback', 'success')
return redirect(url_for('admin'))
else:
flash('Please Use the clear button instead of navigating here', 'success')
return redirect(url_for('admin'))
# flag API
@app.route('/flag/', methods=['POST'])
def flag():
qid = [request.form['qid']][0]
flag_question(qid)
return 'Thanks for the flag on question ' + qid
# errors
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.errorhandler(405)
def page_not_found(e):
return render_template('404.html'), 405
if __name__ == '__main__':
app.debug = settings.DEBUG
app.run(use_reloader=settings.USE_RELOADER)