-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
284 lines (228 loc) · 8.81 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
from flask import Flask, render_template, redirect, jsonify
import redis
import random
import logging
import os
import datetime
import qrcode
import io
import base64
# Configure logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
# Load configuration
config = {
"port": int(os.getenv("APP_PORT", 5000)),
"reroll_count": int(os.getenv("REROLL_COUNT", 0)),
"admin_user": os.getenv("ADMIN_USER", "admin"),
"names": os.getenv("VALID_NAMES", "").split(","),
"year": os.getenv("YEAR", datetime.datetime.now().year),
"budget": os.getenv("BUDGET", "10"),
"url": os.getenv("URL", "http://localhost:5000"),
"rules": os.getenv("RULES", "").split(","),
"qr_toggle_url": os.getenv("QR_TOGGLE_URL", "qr-toggle"),
"giving_day": int(os.getenv("GIVING_DAY", 25)),
"giving_month": int(os.getenv("GIVING_MONTH", 12)),
"use_songs": os.getenv("USE_SONGS", "false") == "true",
"use_qr_checks": os.getenv("DISABLE_QR_CHECKS", "false") == "false",
}
app = Flask(__name__)
# Connect to Redis
def connect_redis():
try:
return redis.StrictRedis(
host=os.getenv("REDIS_HOST", "redis"),
port=int(os.getenv("REDIS_PORT", 6379)),
db=0,
decode_responses=True,
)
except redis.ConnectionError as e:
logging.error(f"Redis connection failed: {e}")
raise
redis_client = connect_redis()
@app.route("/")
def main():
return render_template("index.html")
@app.route("/qrcodes")
def qrcodes():
people = config["names"]
people_dict = []
for person in people:
person_base64_reversed = person.encode("utf-8").hex()[::-1]
qr = qrcode.make(f"{config['url']}/qrscan/{person_base64_reversed}", border=1)
buffer = io.BytesIO()
qr.save(buffer, format="PNG")
qr_img_bytes = base64.b64encode(buffer.getvalue()).decode()
buffer.close()
people_dict.append(
{
"name": person.capitalize(),
"qr": qr_img_bytes,
}
)
return render_template(
"qrcodes.html",
people_dict=people_dict,
config=config,
active=int(redis_client.get("qr-active")) == 1,
)
@app.route(f"/{config['qr_toggle_url']}")
def qr_toggle():
if int(redis_client.get("qr-active")) == 1:
redis_client.set("qr-active", 0)
else:
redis_client.set("qr-active", 1)
return redirect(f"/qrcodes")
@app.route("/qrscan/<person_base64>")
def qrscan(person_base64):
if int(redis_client.get("qr-active")) == 0 and config["use_qr_checks"]:
return redirect("https://cdn.mtdv.me/video/last_rickmas.mp4")
if (
datetime.datetime.now().month != config["giving_month"]
or datetime.datetime.now().day != config["giving_day"]
) and config["use_qr_checks"]:
return redirect("https://cdn.mtdv.me/video/feliz_navidad.mp4")
person = bytes.fromhex(person_base64[::-1]).decode("utf-8")
recipient = redis_client.get(f"recipient:{person.lower()}")
if recipient:
recipient = recipient.capitalize()
else:
recipient = "Not assigned yet"
names = [name.capitalize() for name in config["names"]]
return render_template(
"qr.html",
recipient=recipient.capitalize(),
names=names,
config=config,
song=config["use_songs"]
and recipient != "Not assigned yet"
and os.path.isfile(f"static/songs/{recipient.lower()}.mp3"),
)
@app.route("/qrscan-test/<recipient>")
def qrscantest(recipient):
if recipient:
recipient = recipient.capitalize()
else:
recipient = "Not assigned yet"
names = [name.capitalize() for name in config["names"]]
return render_template(
"qr.html",
recipient=recipient.capitalize(),
names=names,
config=config,
song=config["use_songs"]
and recipient != "Not assigned yet"
and os.path.isfile(f"static/songs/{recipient.lower()}.mp3"),
)
@app.route("/rules/<firstname>/<recipient>")
def rules(firstname, recipient):
return render_template(
"rules.html",
chosen_recipient_capital=recipient.capitalize(),
config=config,
firstname=firstname,
reroll_limit=config["reroll_count"],
)
@app.route("/name/<firstname>")
def name(firstname: str):
firstname_lower = firstname.lower()
if firstname_lower not in config["names"]:
return render_template(
"error.html",
message=(
"Please make sure that you have spelled your name correctly. "
f"Contact {config['admin_user']} if you believe there is a mistake."
),
)
try:
# Check if the user has already been assigned a recipient
recipient = redis_client.get(f"recipient:{firstname_lower}")
if recipient:
return render_template(
"error.html",
message=f"You have already been assigned a recipient. Contact {config['admin_user']} for assistance.",
)
# Get available recipients
all_names = set(config["names"])
assigned_recipients = set(redis_client.smembers("assigned_recipients"))
available_recipients = list(all_names - assigned_recipients - {firstname_lower})
if not available_recipients:
return render_template(
"error.html",
message="No available recipients left. Contact admin for assistance.",
)
chosen_recipient = random.choice(available_recipients)
# Assign the recipient
redis_client.set(f"recipient:{firstname_lower}", chosen_recipient)
redis_client.sadd("assigned_recipients", chosen_recipient)
return redirect(f"/rules/{firstname_lower}/{chosen_recipient}")
except redis.RedisError as e:
logging.error(f"Redis error: {e}")
return render_template(
"error.html", message="Internal server error. Please try again later."
)
@app.route("/reroll/<firstname>", methods=["POST"])
def reroll(firstname):
if config["reroll_count"] == 0:
return jsonify({"error": "Rerolls are disabled."})
firstname_lower = firstname.lower()
if firstname_lower not in config["names"]:
return jsonify(
{
"error": (
"Please make sure that you have spelled your name correctly. "
f"Contact {config['admin_user']} if you believe there is a mistake."
)
}
)
if int(redis_client.get(f"rerolls:{firstname_lower}")) >= config["reroll_count"]:
return jsonify(
{
"error": f"You have reached the maximum number of rerolls ({config['reroll_count']})."
}
)
try:
# Remove the current assignment
old_recipient = redis_client.get(f"recipient:{firstname_lower}")
if old_recipient:
redis_client.srem("assigned_recipients", old_recipient)
else:
return jsonify(
{
"error": "You have not been assigned a recipient yet. Contact admin for assistance."
}
)
# Get new available recipients
all_names = set(config["names"])
assigned_recipients = set(redis_client.smembers("assigned_recipients"))
available_recipients = list(all_names - assigned_recipients - {firstname_lower})
if not available_recipients:
return jsonify(
{"error": "No available recipients left. Contact admin for assistance."}
)
chosen_recipient = random.choice(available_recipients)
# Assign the new recipient
redis_client.set(f"recipient:{firstname_lower}", chosen_recipient)
redis_client.sadd("assigned_recipients", chosen_recipient)
redis_client.incr(f"rerolls:{firstname_lower}")
return jsonify({"newName": chosen_recipient.capitalize()})
except redis.RedisError as e:
logging.error(f"Redis error: {e}")
return render_template(
"error.html", message="Internal server error. Please try again later."
)
if __name__ == "__main__":
try:
# Initialize the recipient assignments in Redis if not already done
if not redis_client.exists("assigned_recipients"):
redis_client.delete("assigned_recipients")
for name in config["names"]:
redis_client.set(f"recipient:{name.lower()}", "")
redis_client.set(f"rerolls:{name.lower()}", 0)
redis_client.set("qr-active", 0)
print(f"Loaded {len(config['names'])} names.")
print(f"Loaded songs: {os.listdir('static/songs')}")
app.run(host="0.0.0.0", debug=False, port=config["port"])
except Exception as e:
logging.critical(f"Failed to start the application: {e}")