-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app.py
152 lines (113 loc) · 4.88 KB
/
flask_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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# A very simple Flask Hello World app for you to get started with...
from flask import Flask, render_template
from flask import request
import mysql.mysql as sql
import json
app = Flask(__name__)
@app.route('/', methods=['POST', 'GET'])
def index():
return "Welcome to HSE API"
def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")
# ======= LEADER BOARD ===========
@app.route('/leader_board_only_table', methods=['GET'])
def leader_board_only_table():
result = sql.get_leaderboard()
return render_template('leaderboard-table.html', users=result, title="Right answers")
@app.route('/looser_board_only_table', methods=['GET'])
def looser_board_only_table():
result = sql.get_looserboard()
return render_template('leaderboard-table.html', users=result, title="Wrong answers")
@app.route('/ratio_board_only_table', methods=['GET'])
def ratio_board_only_table():
result = sql.get_ratioboard()
return render_template('leaderboard-table.html', users=result, title="Ratio right answers")
@app.route('/leader_board', methods=['GET'])
def leader_board():
result = sql.get_leaderboard()
return render_template('leaderboard.html', users=result, title="Right answers", url="leader_board_only_table")
@app.route('/looser_board', methods=['GET'])
def looser_board():
result = sql.get_looserboard()
return render_template('leaderboard.html', users=result, title="Wrong answers", url="looser_board_only_table")
@app.route('/ratio_board', methods=['GET'])
def ratio_board():
result = sql.get_ratioboard()
return render_template('leaderboard.html', users=result, title="Ratio right answers", url="ratio_board_only_table")
@app.route('/stat', methods=['GET'])
def stat():
return render_template('stat.html')
# ======= SHOW DATABASE ===========
@app.route('/all_users', methods=['GET'])
def all_users():
result = sql.get_all_rows('USERS')
return render_template('all_rows.html', users=result)
@app.route('/all_history', methods=['GET'])
def all_history():
result = sql.get_all_rows('HISTORY')
return render_template('all_rows.html', users=result)
@app.route('/all_questions', methods=['GET'])
def all_questions():
result = sql.get_all_rows('QUESTIONS')
return render_template('all_rows.html', users=result)
@app.route('/all_songs', methods=['GET'])
def all_songs():
result = sql.get_all_rows('SONG')
return render_template('all_rows.html', users=result)
# ===================================
# ============= ADD DATA ============
@app.route('/register', methods=['POST', 'GET'])
def register():
if request.method == 'POST':
login = request.form['login']
password = request.form['password']
if(sql.check_user(login)):
return json.dumps({'status' : '0', 'error' : 'User already exists'})
if(sql.add_user(login, password)):
return json.dumps({'status' : '1'})
return 'Get requests isn\'t allowed'
@app.route('/add_history', methods=['POST', 'GET'])
def add_history_handler():
if request.method == 'POST':
params = ['user', 'song_name', 'song_artist', 'answer_right',
'op1', 'op2', 'op3', 'genre', 'user_answer', 'result']
for i in params:
if (request.form.get(i, None) == None):
return json.dumps({'status' : '0', 'error' : 'Need parameter: ' + i})
user = request.form['user']
song_name = request.form['song_name']
song_artist = request.form['song_artist']
answer_right = request.form['answer_right']
op1 = request.form['op1']
op2 = request.form['op2']
op3 = request.form['op3']
genre = request.form['genre']
user_answer = request.form['user_answer']
result = str2bool(request.form['result'])
res = sql.add_history(user, song_name, song_artist, answer_right, op1, op2, op3, genre, user_answer, result)
return json.dumps({'status' : '1', 'result' : str(res)})
return 'Get requests isn\'t allowed'
@app.route('/count_game', methods=['POST', 'GET'])
def count_game():
if request.method == 'POST':
login = request.form['login']
return str(sql.game_count(login))
return 'Get requests isn\'t allowed'
@app.route('/count_game_increment', methods=['POST', 'GET'])
def count_game_increment():
if request.method == 'POST':
login = request.form['login']
return str(sql.game_count_increment(login))
return 'Get requests isn\'t allowed'
# ===================================
@app.route('/auth', methods=['POST', 'GET'])
def auth():
if request.method == 'POST':
result = sql.auth(request.form['login'], request.form['password'])
if result:
return json.dumps({'status' : '1', 'result' : result})
else:
return json.dumps({'status' : '0', 'error' : 'Invalid username/password'})
return 'Get requests isn\'t allowed'