-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
348 lines (321 loc) · 11.4 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# import the necessary modules
from flask import Flask, render_template, request, redirect, url_for, session, g, jsonify
import sqlite3
from random import choice
from time import sleep
from os import environ, urandom
from functools import wraps
# start a Flask app
app = Flask(__name__)
app.secret_key = urandom(24)
# Declare global variables
firstUserData = []
animals = ["beach.png","mouse.png","dolphine.png","crab.png","bee.png","rabbit.png","tiger.png","owl.png","elephant.png","panda.png","snail.png","frog.png","fish.png","reindeer.png","lion.png","bear.png","spider.png","butterfly.png","spider2.png","animals1.png","animals2.png","animals3.png","animals4.png","animal6.png","animal7.png",]
# The makedata function handles new user registration on any page.
def makedata():
with sqlite3.connect("tags.db") as connection:
c = connection.cursor()
# pull name data from history table
c.execute("SELECT name FROM history")
histdata = c.fetchall()
# this code flattens the list
histdata = [h[0] for h in histdata]
inputan = choice(animals)
inputname = request.form["discordtag"]
# declare a match var that will tell if the for loop finds a match or not
match = False
# the default batch
batch = [(inputan, inputname, 0, 0, 0)]
for hist in histdata:
if inputname == hist:
c.execute("SELECT * FROM history WHERE name=?", [(inputname)])
actualTimes = c.fetchone()
match = True
batch = [actualTimes]
c.executemany("INSERT INTO current VALUES(?,?,?,?,?)", batch)
if not match:
c.executemany("INSERT INTO history VALUES(?,?,?,?,?)", batch)
c.executemany("INSERT INTO permanent VALUES(?,?,?,?,?)", batch)
# login required decorator
def login_required(f):
@wraps(f)
def wrap(*args, **kwargs):
try:
if not session['logged_in']:
return render_template('error.html')
except KeyError:
return render_template('error.html')
return f(*args, **kwargs)
return wrap
# the default route decorator
@app.route("/")
def index():
with sqlite3.connect("tags.db") as connection:
c = connection.cursor()
c.execute("SELECT * FROM current")
myUsers = c.fetchall()
# if the length of users is more than 0, we need to pull the first name
if len(myUsers) > 0:
firstUserData = myUsers.pop(-0)
else:
firstUserData = None
return render_template("public.html", users=myUsers, first=firstUserData)
@app.route("/refresh", methods=['POST'])
def refresh():
with sqlite3.connect("tags.db") as connection:
c = connection.cursor()
c.execute("SELECT * FROM current")
myUsers = c.fetchall()
# if the length of users is more than 0, we need to pull the first name
if len(myUsers) > 0:
firstUserData = myUsers.pop(-0)
else:
firstUserData = None
return jsonify({"firstUserData": firstUserData, "myUsers": myUsers})
@app.route('/av', methods=["GET", "POST"])
@login_required
def adminview():
if request.method == "POST":
makedata()
with sqlite3.connect("tags.db") as connection:
c = connection.cursor()
c.execute("SELECT * FROM current")
myUsers = c.fetchall()
global firstUserData
if len(myUsers) > 0:
firstUserData = myUsers.pop(-0)
else:
firstUserData = None
return render_template("index.html", users=myUsers, first=firstUserData)
with sqlite3.connect("tags.db") as connection:
c = connection.cursor()
c.execute("SELECT * FROM current")
myUsers = c.fetchall()
# if the length of users is more than 0, we need to pull the first name
if len(myUsers) > 0:
firstUserData = myUsers.pop(-0)
else:
firstUserData = None
return render_template("index.html", users=myUsers, first=firstUserData)
@app.route("/adminlogin", methods=["GET", "POST"])
def login():
error = None
if request.method == "POST":
if request.form["username"] == "org123" and request.form["password"] == "#L0v3sh4ck":
session['logged_in'] = True
return redirect(url_for('adminview'))
else:
error = True
return render_template('login.html', error=error)
return render_template("login.html", error=error)
# decorator for the 'next' button
@app.route('/av/next', methods=["GET", "POST"])
def nextSinger():
if request.method == "POST":
makedata()
with sqlite3.connect("tags.db") as connection:
c = connection.cursor()
c.execute("SELECT * FROM current")
myUsers = c.fetchall()
global firstUserData
if len(myUsers) > 0:
firstUserData = myUsers.pop(-0)
else:
firstUserData = None
return render_template("index.html", users=myUsers, first=firstUserData)
with sqlite3.connect("tags.db") as connection:
c = connection.cursor()
# when next is clicked, we need to increment the times he/she has sang by 1.
# as well as remove the user from the 'current' queue.
userLikes = (firstUserData[3])
userDisLikes = (firstUserData[4])
singer = (firstUserData[1])
c.execute("UPDATE history SET singTimes = singTimes + 1 WHERE name=?", [singer])
c.execute("UPDATE permanent SET singTimes = singTimes + 1 WHERE name=?", [singer])
c.execute("UPDATE history SET likes = ? WHERE name=?", [userLikes, singer])
c.execute("UPDATE history SET dislikes = ? WHERE name=?", [userDisLikes, singer])
c.execute("UPDATE permanent SET dislikes = ? WHERE name=?", [userDisLikes, singer])
c.execute("UPDATE permanent SET likes = ? WHERE name=?", [userLikes, singer])
c.execute("DELETE FROM current WHERE name=?", [singer])
c.execute("SELECT * FROM current")
myUsers = c.fetchall()
if len(myUsers) > 0:
firstUserData = myUsers.pop(-0)
else:
firstUserData = None
return redirect(url_for("adminview"))
# decorator for the 'skip' button
@app.route('/av/skip', methods=["GET", "POST"])
def skipSinger():
if request.method == "POST":
makedata()
with sqlite3.connect("tags.db") as connection:
c = connection.cursor()
c.execute("SELECT * FROM current")
myUsers = c.fetchall()
global firstUserData
if len(myUsers) > 0:
firstUserData = myUsers.pop(-0)
else:
firstUserData = None
return render_template("index.html", users=myUsers, first=firstUserData)
with sqlite3.connect("tags.db") as connection:
c = connection.cursor()
singer = (firstUserData[1])
# here, we don't need to increment. Just delete the record in current.
c.execute("DELETE FROM current WHERE name=?", [singer])
c.execute("SELECT * FROM current")
myUsers = c.fetchall()
if len(myUsers) > 0:
firstUserData = myUsers.pop(-0)
else:
firstUserData = None
return redirect(url_for('adminview'))
# decorator for resetting the history data
@app.route('/resetd', methods=["GET", "POST"])
@login_required
def resetHistory():
if request.method == "POST":
makedata()
with sqlite3.connect("tags.db") as connection:
c = connection.cursor()
c.execute("SELECT * FROM current")
myUsers = c.fetchall()
global firstUserData
if len(myUsers) > 0:
firstUserData = myUsers.pop(-0)
else:
firstUserData = None
return render_template("index.html", users=myUsers, first=firstUserData)
with sqlite3.connect('tags.db') as connection:
c = connection.cursor()
c.execute("DELETE FROM history")
sleep(2)
c.execute("SELECT * FROM current")
myUsers = c.fetchall()
if len(myUsers) > 0:
firstUserData = myUsers.pop(-0)
else:
firstUserData = None
return redirect(url_for('adminview'))
@app.route("/delete", methods=["GET", "POST"])
@login_required
def delete():
editType = "del"
if request.method == "POST":
deleteUser = (request.form['slct'])
with sqlite3.connect('tags.db') as connection:
c = connection.cursor()
c.execute("DELETE FROM current WHERE name=?", [deleteUser])
return redirect(url_for('adminview'))
with sqlite3.connect('tags.db') as connection:
c = connection.cursor()
c.execute("SELECT name FROM current")
allNames = c.fetchall()[1:]
allNames = [a[0] for a in allNames]
return render_template("edit.html", edit=editType, names=allNames)
@app.route("/rename", methods=["GET", "POST"])
@login_required
def rename():
editType = "ren"
if request.method == "POST":
oldUser = request.form['slct']
newUser = request.form['newname']
changeUser = (newUser, oldUser)
with sqlite3.connect('tags.db') as connection:
c = connection.cursor()
c.execute("UPDATE current SET name=? WHERE name=?", changeUser)
c.execute("DELETE FROM history WHERE name=?", [oldUser])
c.execute("DELETE FROM permanent WHERE name=?", [oldUser])
c.execute("SELECT name FROM history")
match = False
histNames = c.fetchall()
for h in histNames:
if newUser == h:
match = True
if not match:
c.execute("INSERT INTO history VALUES(?,?,?,?,?)", (choice(animals), newUser, 0, 0, 0))
c.execute("INSERT INTO permanent VALUES(?,?,?,?,?)", (choice(animals), newUser, 0, 0, 0))
return redirect(url_for('adminview'))
with sqlite3.connect('tags.db') as connection:
c = connection.cursor()
c.execute("SELECT name FROM current")
allNames = c.fetchall()[1:]
allNames = [a[0] for a in allNames]
return render_template("edit.html", edit=editType, names=allNames)
@app.route('/log')
def log():
with sqlite3.connect("tags.db") as connection:
c = connection.cursor()
c.execute("SELECT * FROM current")
myUsers = c.fetchall()
if len(myUsers) > 0:
firstUserData = myUsers.pop(-0)
else:
firstUserData = None
return render_template('log.html', users=myUsers, first=firstUserData)
@app.route('/logfull')
def fullLog():
return render_template('fulllog.html')
@app.route('/stats')
@login_required
def stats():
with sqlite3.connect('tags.db') as connection:
c = connection.cursor()
c.execute("SELECT singTimes from permanent")
allTimes = c.fetchall()
realTimes = [a[0] for a in allTimes]
totalTimes = sum(realTimes)
percentages = []
for r in realTimes:
pctvalue = int(round((r * 1.00 / totalTimes) * 100))
percentages.append(pctvalue)
c.execute("SELECT name from permanent")
allNames = c.fetchall()
realNames = [a[0] for a in allNames]
statData = zip(realNames, percentages)
c.execute("SELECT name, likes from permanent")
allLikes = c.fetchall()
allLikes = sorted(allLikes, key=lambda d: d[1], reverse=True)
try:
mostLiked = allLikes[0][0]
except:
mostLiked = ""
c.execute("SELECT name, dislikes from permanent")
allDislikes = c.fetchall()
allDislikes = sorted(allDislikes, key=lambda d: d[1], reverse=True)
try:
mostDisliked = allDislikes[0][0]
except:
mostDisliked = ""
return render_template('stats.html', stats=statData, msliked=mostLiked, msdsliked=mostDisliked)
@app.route("/l", methods=["POST"])
def votelike():
with sqlite3.connect("tags.db") as connection:
c = connection.cursor()
c.execute("SELECT * from current")
currentNameData = c.fetchone()
nameDataName = currentNameData[1]
c.execute("UPDATE current SET likes = likes + 1 WHERE name=?", [nameDataName])
c.execute("SELECT * from current")
currentNameData = c.fetchone()
return jsonify({"status": "OK", "likes": currentNameData[3]})
@app.route("/dl", methods=["POST"])
def votedislike():
with sqlite3.connect("tags.db") as connection:
c = connection.cursor()
c.execute("SELECT * from current")
currentNameData = c.fetchone()
nameDataName = currentNameData[1]
c.execute("UPDATE current SET dislikes = dislikes + 1 WHERE name=?", [nameDataName])
c.execute("SELECT * FROM current")
currentNameData = c.fetchone()
return jsonify({"status": "OK", "dislikes": currentNameData[4]})
@app.route('/featured')
def featured():
return render_template('feature.html')
if __name__ == "__main__":
# convention to run on Heroku
port = int(environ.get("PORT", 5000))
# run the app available anywhere on the network, on debug mode
app.run(host="0.0.0.0", port=port, debug=True)