-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
128 lines (100 loc) · 3.55 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
from functools import lru_cache
from os import environ, getenv
from os.path import dirname, join
from dotenv import load_dotenv
from firebase_admin import credentials, db, initialize_app, storage
from flask import Flask, json, redirect, render_template
# Initialize Flask app
app = Flask(__name__)
# Read environment variables set at ./.env
if not load_dotenv(f"{dirname(__file__)}/.env"):
raise RuntimeError(
f"Could not load env file. Make sure it is located at {dirname(__file__)}/.env"
)
def check_if_required_env_variables_are_present():
required_env_variables = {
"START_ACT",
"END_ACT",
"CURRENT_ACT_YEAR",
"FIREBASE_STORAGE",
}
if not all(env in environ for env in required_env_variables):
raise RuntimeError(
f"The following required environmental variables have not been set - {(x for x in required_env_variables if x not in environ)}"
)
check_if_required_env_variables_are_present()
# Initialize Firebase app
creds = credentials.Certificate("firebase.json")
initialize_app(
creds,
{
"databaseURL": getenv("FIREBASE_DB"),
"storageBucket": getenv("FIREBASE_STORAGE"),
},
)
all_leaderboards_ref = db.reference("vitcc").child("owasp")
def fetch_data(act: int | str) -> list[dict]:
"""Return a list of all members in the act. Sorted by points"""
data_of_act: dict = all_leaderboards_ref.child(f"leaderboard-act{act}").get()
users_of_act = list(data_of_act.values())
users_of_act.sort(key=lambda x: x["Rating"], reverse=True)
return users_of_act
class Act:
def __init__(self, num: int, name: str):
self.num: int = num
self.name: str = name
self.data: list
# Fetch data from leaderboard
self.refresh_leaderboard_data()
self.cabinet = list(
filter(
lambda member: (member["Rating"] >= 5000)
and member["Name"].casefold() != "testing",
self.data,
)
)
self.members = list(
filter(
lambda member: (
member["Rating"] < 5000 and member["Name"].casefold() != "testing"
),
self.data,
)
)
self.rank_members()
def refresh_leaderboard_data(self):
print("fetched latest points")
self.data = fetch_data(self.num)
def rank_members(self):
for i, member in enumerate(self.members):
if i < 1:
member["Image"] = "platinum-3"
elif i < 3:
member["Image"] = "platinum-2"
elif i < 8:
member["Image"] = "gold-3"
elif i < 12:
member["Image"] = "gold-2"
elif i <= 25:
member["Image"] = "silver-3"
elif i <= 40:
member["Image"] = "silver-2"
else:
member["Image"] = "bronze-1"
if member["Rating"] == 0:
member["Image"] = "unranked"
CURRENT_ACT_YEAR: int = int(getenv("CURRENT_ACT_YEAR"))
END_ACT: int = int(getenv("END_ACT"))
START_ACT: int = int(getenv("START_ACT"))
@app.route("/", methods=["GET"])
def index():
return redirect("/leaderboard")
@app.route("/leaderboard", methods=["GET"])
def leaderboard():
all_acts = [
Act(i, f"ACT {i} - {CURRENT_ACT_YEAR - END_ACT + i}")
for i in range(END_ACT, START_ACT, -1)
]
return render_template("leaderboard.html", all_acts=all_acts, enumerate=enumerate)
if __name__ == "__main__":
app.run(port=5000, debug=True)