-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
428 lines (359 loc) · 14.7 KB
/
Copy pathapp.py
File metadata and controls
428 lines (359 loc) · 14.7 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""Pick-up basketball coordinator — local prototype.
Run: python3 app.py then open http://localhost:8000
"""
import os
import re
import urllib.parse
from datetime import date
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import db
import templates as T
import services as S
PORT = int(os.environ.get("PORT", 8000))
# ---- small routing helper -------------------------------------------------
ROUTES = [] # (method, compiled_regex, handler_name)
_DB_READY = False # schema is created lazily on the first request (see _dispatch)
def route(method, pattern):
def deco(fn):
ROUTES.append((method, re.compile(f"^{pattern}$"), fn))
return fn
return deco
class Handler(BaseHTTPRequestHandler):
# --- response helpers ---
def _html(self, html, status=200):
data = html.encode("utf-8")
self.send_response(status)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
def _redirect(self, location):
self.send_response(303)
self.send_header("Location", location)
self.end_headers()
def _no_content(self):
self.send_response(204)
self.end_headers()
def _form(self):
length = int(self.headers.get("Content-Length", 0))
raw = self.rfile.read(length).decode("utf-8") if length else ""
return {k: v[0] for k, v in urllib.parse.parse_qs(raw).items()}
def _query(self):
qs = urllib.parse.urlparse(self.path).query
return {k: v[0] for k, v in urllib.parse.parse_qs(qs).items()}
def _dispatch(self, method):
global _DB_READY
if not _DB_READY:
db.init_db() # idempotent; runs once per (cold) process
_DB_READY = True
path = urllib.parse.urlparse(self.path).path
# On Vercel all paths are rewritten to this function; if the rewrite
# target leaks through as the path, recover the original route.
if path.startswith("/api/index"):
path = path[len("/api/index"):] or "/"
for m, rx, fn in ROUTES:
if m != method:
continue
match = rx.match(path)
if match:
conn = db.get_conn()
try:
fn(self, conn, *match.groups())
finally:
conn.close()
return
self._html(T.simple("Not found", "No such page."), status=404)
def do_GET(self):
self._dispatch("GET")
def do_POST(self):
self._dispatch("POST")
def log_message(self, fmt, *args): # quieter console
return
# ---- lookups --------------------------------------------------------------
def _player_by_token(conn, token):
return conn.execute("SELECT * FROM players WHERE token = ?", (token,)).fetchone()
def _organizer_by_token(conn, token):
p = _player_by_token(conn, token)
return p if (p and p["is_organizer"]) else None
def _upcoming_game(conn):
"""The next game that hasn't happened yet, else the most recent one."""
today = date.today().isoformat() # pass as a param — portable across backends
return conn.execute(
"SELECT * FROM games WHERE game_date >= ? ORDER BY game_date, start_time LIMIT 1",
(today,),
).fetchone() or conn.execute(
"SELECT * FROM games ORDER BY game_date DESC, start_time DESC LIMIT 1"
).fetchone()
# ---- routes: public / dev -------------------------------------------------
@route("GET", "/")
def index(h, conn):
game = _upcoming_game(conn)
# The organizer dashboard is openly linked from the landing board for
# everyone (a casual, fully-open model — anyone can manage games/roster).
org = conn.execute(
"SELECT token FROM players WHERE is_organizer = 1 ORDER BY id LIMIT 1"
).fetchone()
org_token = org["token"] if org else None
if game:
rows = conn.execute(
"""SELECT p.*, a.status AS av
FROM players p
LEFT JOIN availability a ON a.player_id = p.id AND a.game_id = ?
ORDER BY p.is_organizer DESC, p.name""",
(game["id"],),
).fetchall()
players_with_status = [(r, r["av"]) for r in rows]
else:
rows = conn.execute(
"SELECT * FROM players ORDER BY is_organizer DESC, name"
).fetchall()
players_with_status = [(r, None) for r in rows]
h._html(T.landing(game, players_with_status, org_token))
def _setup_allowed(conn, given_key):
"""Setup is open when the DB has no organizer yet (first-run bootstrap), or
when SETUP_KEY is configured and matches. Returns (allowed, key_for_forms)."""
env_key = os.environ.get("SETUP_KEY")
if env_key and given_key == env_key:
return True, env_key
no_org = conn.execute(
"SELECT 1 FROM players WHERE is_organizer = 1 LIMIT 1"
).fetchone() is None
return no_org, (env_key or "")
def _organizers(conn):
return conn.execute(
"SELECT name, email, token FROM players WHERE is_organizer = 1 ORDER BY id"
).fetchall()
@route("GET", r"/setup")
def setup(h, conn):
"""Create / retrieve organizer access (see _setup_allowed for the gate)."""
allowed, key = _setup_allowed(conn, h._query().get("key", ""))
if not allowed:
return h._html(T.simple("Setup", "Setup is not available."), status=404)
h._html(T.setup_page(_organizers(conn), key))
@route("POST", r"/setup/create")
def setup_create(h, conn):
form = h._form()
allowed, key = _setup_allowed(conn, form.get("key", ""))
if not allowed:
return h._html(T.simple("Setup", "Setup is not available."), status=404)
name = form.get("name", "").strip()
email = form.get("email", "").strip().lower()
if name and email and not conn.execute(
"SELECT 1 FROM players WHERE email = ?", (email,)
).fetchone():
conn.execute(
"INSERT INTO players (name, email, token, is_organizer) VALUES (?, ?, ?, 1)",
(name, email, db.new_token()),
)
conn.commit()
# Render directly (not redirect) so the new admin link is shown even when
# creating the very first organizer in open (no-key) mode.
h._html(T.setup_page(_organizers(conn), key))
# ---- routes: player -------------------------------------------------------
@route("GET", r"/p/([\w-]+)")
def player_home(h, conn, token):
player = _player_by_token(conn, token)
if not player:
return h._html(T.simple("Unknown link", "This link isn't valid."), status=404)
game = _upcoming_game(conn)
status = my_team = None
locked = False
if game:
a = conn.execute(
"SELECT status FROM availability WHERE game_id = ? AND player_id = ?",
(game["id"], player["id"]),
).fetchone()
status = a["status"] if a else None
t = conn.execute(
"SELECT team FROM assignments WHERE game_id = ? AND player_id = ?",
(game["id"], player["id"]),
).fetchone()
my_team = t["team"] if t else None
locked = bool(game["teams_locked"])
h._html(T.player_home(player, game, status, my_team, locked))
@route("POST", r"/p/([\w-]+)/availability")
def set_availability(h, conn, token):
player = _player_by_token(conn, token)
if not player:
return h._html(T.simple("Unknown link", "This link isn't valid."), status=404)
form = h._form()
game_id, status = form.get("game_id"), form.get("status")
if status in ("in", "out") and game_id:
conn.execute(
"""INSERT INTO availability (game_id, player_id, status) VALUES (?, ?, ?)
ON CONFLICT(game_id, player_id) DO UPDATE SET status = excluded.status""",
(game_id, player["id"], status),
)
conn.commit()
# Return to wherever the request came from (landing board or personal page).
dest = form.get("return_to", "")
if not (dest.startswith("/") and not dest.startswith("//")):
dest = f"/p/{token}"
h._redirect(dest)
# ---- routes: organizer ----------------------------------------------------
def _require_org(h, conn, token):
org = _organizer_by_token(conn, token)
if not org:
h._html(T.simple("Not authorized", "Organizer link required."), status=403)
return None
return org
@route("GET", r"/admin/([\w-]+)")
def admin_dashboard(h, conn, token):
org = _require_org(h, conn, token)
if not org:
return
players = conn.execute("SELECT * FROM players ORDER BY is_organizer DESC, name").fetchall()
games = conn.execute("SELECT * FROM games ORDER BY game_date DESC, start_time DESC").fetchall()
h._html(T.admin_dashboard(org, players, games))
@route("POST", r"/admin/([\w-]+)/players")
def add_player(h, conn, token):
org = _require_org(h, conn, token)
if not org:
return
form = h._form()
name, email = form.get("name", "").strip(), form.get("email", "").strip().lower()
is_org = 1 if form.get("is_organizer") else 0
if name and email:
try:
conn.execute(
"INSERT INTO players (name, email, token, is_organizer) VALUES (?, ?, ?, ?)",
(name, email, db.new_token(), is_org),
)
conn.commit()
except Exception:
pass # duplicate email — ignore for the prototype
h._redirect(f"/admin/{token}")
@route("POST", r"/admin/([\w-]+)/players/(\d+)/edit")
def edit_player(h, conn, token, player_id):
if not _require_org(h, conn, token):
return
form = h._form()
name, email = form.get("name", "").strip(), form.get("email", "").strip().lower()
is_org = 1 if form.get("is_organizer") else 0
if name and email:
try:
conn.execute(
"UPDATE players SET name = ?, email = ?, is_organizer = ? WHERE id = ?",
(name, email, is_org, player_id),
)
conn.commit()
except Exception:
pass # email collides with another player — ignore for the prototype
h._redirect(f"/admin/{token}")
@route("POST", r"/admin/([\w-]+)/players/(\d+)/delete")
def delete_player(h, conn, token, player_id):
org = _require_org(h, conn, token)
if not org:
return
# Don't let the acting organizer delete themselves out of their own session.
if str(org["id"]) != player_id:
conn.execute("DELETE FROM players WHERE id = ?", (player_id,)) # cascades availability + assignments
conn.commit()
h._redirect(f"/admin/{token}")
@route("POST", r"/admin/([\w-]+)/games")
def add_game(h, conn, token):
org = _require_org(h, conn, token)
if not org:
return
form = h._form()
date, time = form.get("game_date"), form.get("start_time", "07:00")
if date:
conn.execute(
"INSERT INTO games (game_date, start_time) VALUES (?, ?)", (date, time)
)
conn.commit()
h._redirect(f"/admin/{token}")
@route("POST", r"/admin/([\w-]+)/games/(\d+)/edit")
def edit_game(h, conn, token, game_id):
if not _require_org(h, conn, token):
return
form = h._form()
date, time = form.get("game_date"), form.get("start_time", "07:00")
if date:
conn.execute(
"UPDATE games SET game_date = ?, start_time = ? WHERE id = ?",
(date, time, game_id),
)
conn.commit()
h._redirect(f"/admin/{token}")
@route("POST", r"/admin/([\w-]+)/games/(\d+)/delete")
def delete_game(h, conn, token, game_id):
if not _require_org(h, conn, token):
return
conn.execute("DELETE FROM games WHERE id = ?", (game_id,)) # cascades availability + assignments
conn.commit()
h._redirect(f"/admin/{token}")
@route("GET", r"/admin/([\w-]+)/games/(\d+)")
def admin_game(h, conn, token, game_id):
org = _require_org(h, conn, token)
if not org:
return
game = conn.execute("SELECT * FROM games WHERE id = ?", (game_id,)).fetchone()
if not game:
return h._html(T.simple("Not found", "No such game.", back=f"/admin/{token}"), status=404)
rows = conn.execute(
"""SELECT p.*, a.status AS av_status, asg.team AS team
FROM players p
LEFT JOIN availability a ON a.player_id = p.id AND a.game_id = ?
LEFT JOIN assignments asg ON asg.player_id = p.id AND asg.game_id = ?
ORDER BY p.name""",
(game_id, game_id),
).fetchall()
light = [r for r in rows if r["team"] == "light"]
dark = [r for r in rows if r["team"] == "dark"]
unassigned = [r for r in rows if r["av_status"] == "in" and r["team"] is None]
not_playing = [r for r in rows if r["av_status"] == "out"]
summary = S.team_summary_text(conn, game)
h._html(T.admin_game(org, game, unassigned, light, dark, not_playing, summary))
def _locked_guard(h, conn, token, game_id):
"""Return the game row, or None if missing (404 already sent)."""
game = conn.execute("SELECT * FROM games WHERE id = ?", (game_id,)).fetchone()
if not game:
h._html(T.simple("Not found", "No such game.", back=f"/admin/{token}"), status=404)
return None
return game
@route("POST", r"/admin/([\w-]+)/games/(\d+)/suggest")
def suggest(h, conn, token, game_id):
if not _require_org(h, conn, token):
return
game = _locked_guard(h, conn, token, game_id)
if game and not game["teams_locked"]:
S.suggest_teams(conn, game_id)
h._redirect(f"/admin/{token}/games/{game_id}")
@route("POST", r"/admin/([\w-]+)/games/(\d+)/assign")
def assign(h, conn, token, game_id):
if not _require_org(h, conn, token):
return
game = _locked_guard(h, conn, token, game_id)
if game is None:
return # 404 already sent
form = h._form()
if not game["teams_locked"] and form.get("player_id"):
S.set_assignment(conn, game_id, form["player_id"], form.get("team", "bench"))
if form.get("ajax"):
return h._no_content()
h._redirect(f"/admin/{token}/games/{game_id}")
@route("POST", r"/admin/([\w-]+)/games/(\d+)/lock")
def lock(h, conn, token, game_id):
if not _require_org(h, conn, token):
return
conn.execute("UPDATE games SET teams_locked = 1 WHERE id = ?", (game_id,))
conn.commit()
h._redirect(f"/admin/{token}/games/{game_id}")
@route("POST", r"/admin/([\w-]+)/games/(\d+)/unlock")
def unlock(h, conn, token, game_id):
if not _require_org(h, conn, token):
return
conn.execute("UPDATE games SET teams_locked = 0 WHERE id = ?", (game_id,))
conn.commit()
h._redirect(f"/admin/{token}/games/{game_id}")
def main():
db.init_db()
server = ThreadingHTTPServer(("0.0.0.0", PORT), Handler)
print(f"\n🏀 Pick-up Hoops running at http://localhost:{PORT}\n", flush=True)
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nbye")
if __name__ == "__main__":
main()