-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
72 lines (56 loc) · 2.32 KB
/
app.py
File metadata and controls
72 lines (56 loc) · 2.32 KB
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
from flask import Flask, jsonify, request, render_template
from flask_cors import CORS
import mysql.connector
from dotenv import load_dotenv
import os
import json
load_dotenv()
mysql_pass = os.environ.get("MYSQL_PASSWORD")
""" conn = mysql.connector.connect(
host="127.0.0.1",
user="root",
password=mysql_pass,
database="ctf_2025"
) """
app = Flask(__name__)
CORS(app)
@app.route('/')
def index():
return render_template("index.html") # {"connected_to_database": conn.is_connected()}
@app.route('/data')
def get_data():
with open("database/queried_challenges.json") as f:
data = json.load(f)
return jsonify(data)
@app.route('/tables')
def get_tables():
cursor = conn.cursor()
cursor.execute("SHOW TABLES")
tables = [table[0] for table in cursor.fetchall()]
cursor.close()
return jsonify(tables)
@app.route('/query/<table_name>')
def get_table_data(table_name):
# default query:
# query = f"SELECT * FROM {table_name} ORDER BY Category, FIELD(Difficulty, 'Easy', 'Medium', 'Hard');"
query = f"SELECT Challenge_Name, Category, Difficulty, Points, Attempts_Successful, Attempts_Fail, (Attempts_Successful + Attempts_Fail) AS Attempts, IFNULL(ROUND(Attempts_Successful/(Attempts_Successful + Attempts_Fail) * 100.0, 2), 0) AS Success_Rate FROM {table_name} ORDER BY FIELD(Difficulty, 'Easy', 'Medium', 'Hard'), Success_Rate DESC, Attempts DESC;"
# query = f"SELECT * FROM {table_name} ORDER BY FIELD(Difficulty, 'Easy', 'Medium', 'Hard'), Category;"
""" isAscending = request.args.get("isAscending")
print(f"{isAscending}\n")
query_list = list(query)
if isAscending:
query_list.insert(-1, " DESC")
else:
query_list.insert(-1, " ASC")
full_query = "".join(query_list)
print(full_query) """
cursor = conn.cursor(dictionary=True)
#cursor.execute(f"SELECT * FROM {table_name}")
cursor.execute(query)
# cursor.execute(f"SELECT * FROM {table_name} WHERE Difficulty = 'Easy';SELECT * FROM {table_name} WHERE Difficulty = 'Medium';")
# cursor.execute(f"SELECT uuid, Challenge_Name, Category, Difficulty, Points, Attempts_Successful, Attempts_Fail FROM {table_name} ORDER BY Category, points;")
rows = cursor.fetchall()
cursor.close()
return jsonify(rows)
if __name__ == '__main__':
app.run(debug=True)