refactor
This commit is contained in:
94
main.py
94
main.py
@@ -79,11 +79,6 @@ def initialize_database() -> None:
|
|||||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS app_meta (
|
|
||||||
key TEXT PRIMARY KEY,
|
|
||||||
value TEXT NOT NULL
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS members (
|
CREATE TABLE IF NOT EXISTS members (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
first_name TEXT NOT NULL,
|
first_name TEXT NOT NULL,
|
||||||
@@ -103,60 +98,6 @@ def initialize_database() -> None:
|
|||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
# Seed demo data once to make first run usable.
|
|
||||||
total_rows = conn.execute("SELECT COUNT(*) AS count FROM members;").fetchone()["count"]
|
|
||||||
if total_rows == 0:
|
|
||||||
conn.executemany(
|
|
||||||
"INSERT INTO families (family_name, primary_phone) VALUES (?, ?);",
|
|
||||||
[
|
|
||||||
("Garcia", "555-201-0141"),
|
|
||||||
("Miller", "555-201-0187"),
|
|
||||||
("Nguyen", "555-201-0179"),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
family_map = {
|
|
||||||
row["family_name"]: row["id"]
|
|
||||||
for row in conn.execute("SELECT id, family_name FROM families;").fetchall()
|
|
||||||
}
|
|
||||||
|
|
||||||
conn.executemany(
|
|
||||||
"""
|
|
||||||
INSERT INTO members
|
|
||||||
(first_name, last_name, age, family_id, is_checked_in, swim_test_passed, can_use_deep_end)
|
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?);
|
|
||||||
""",
|
|
||||||
[
|
|
||||||
("Avery", "Wilson", 34, None, 0, 0, 1),
|
|
||||||
("Noah", "Patel", 42, None, 0, 0, 1),
|
|
||||||
("Emma", "James", 29, None, 0, 0, 1),
|
|
||||||
("Luis", "Garcia", 41, family_map["Garcia"], 0, 0, 1),
|
|
||||||
("Sofia", "Garcia", 38, family_map["Garcia"], 0, 0, 1),
|
|
||||||
("Mateo", "Garcia", 11, family_map["Garcia"], 0, 0, 0),
|
|
||||||
("Helen", "Miller", 37, family_map["Miller"], 0, 0, 1),
|
|
||||||
("Kai", "Miller", 9, family_map["Miller"], 0, 0, 0),
|
|
||||||
("Trang", "Nguyen", 35, family_map["Nguyen"], 0, 0, 1),
|
|
||||||
("Liam", "Nguyen", 8, family_map["Nguyen"], 0, 0, 0),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
schema_version = conn.execute(
|
|
||||||
"SELECT value FROM app_meta WHERE key = 'schema_version';"
|
|
||||||
).fetchone()
|
|
||||||
current_schema_version = 0
|
|
||||||
if schema_version is not None:
|
|
||||||
try:
|
|
||||||
current_schema_version = int(schema_version["value"])
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
current_schema_version = 0
|
|
||||||
|
|
||||||
# One-time migration for older local databases created before defaults were reset.
|
|
||||||
if current_schema_version < 2:
|
|
||||||
conn.execute("UPDATE members SET is_checked_in = 0, swim_test_passed = 0;")
|
|
||||||
conn.execute(
|
|
||||||
"UPDATE members SET can_use_deep_end = CASE WHEN age >= 13 THEN 1 ELSE 0 END;"
|
|
||||||
)
|
|
||||||
|
|
||||||
family_columns = {
|
family_columns = {
|
||||||
row["name"]
|
row["name"]
|
||||||
for row in conn.execute("PRAGMA table_info(families);").fetchall()
|
for row in conn.execute("PRAGMA table_info(families);").fetchall()
|
||||||
@@ -167,26 +108,6 @@ def initialize_database() -> None:
|
|||||||
"ADD COLUMN guest_passes INTEGER NOT NULL DEFAULT 0 CHECK(guest_passes >= 0);"
|
"ADD COLUMN guest_passes INTEGER NOT NULL DEFAULT 0 CHECK(guest_passes >= 0);"
|
||||||
)
|
)
|
||||||
|
|
||||||
if current_schema_version < 3:
|
|
||||||
conn.execute(
|
|
||||||
"INSERT INTO app_meta (key, value) VALUES ('schema_version', '3') "
|
|
||||||
"ON CONFLICT(key) DO UPDATE SET value = excluded.value;"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Normalize older short phone records to full numbers when possible.
|
|
||||||
old_phones = conn.execute(
|
|
||||||
"SELECT id, primary_phone FROM families WHERE primary_phone IS NOT NULL;"
|
|
||||||
).fetchall()
|
|
||||||
for row in old_phones:
|
|
||||||
phone = row["primary_phone"]
|
|
||||||
digits = re.sub(r"\D", "", phone)
|
|
||||||
if len(digits) == 7:
|
|
||||||
normalized = normalize_phone_for_storage(f"555{digits}")
|
|
||||||
conn.execute(
|
|
||||||
"UPDATE families SET primary_phone = ? WHERE id = ?;",
|
|
||||||
(normalized, row["id"]),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def to_member_payload(row: dict, min_swim_test_age: int) -> dict:
|
def to_member_payload(row: dict, min_swim_test_age: int) -> dict:
|
||||||
requires_swim_test = row["age"] < min_swim_test_age
|
requires_swim_test = row["age"] < min_swim_test_age
|
||||||
@@ -203,21 +124,6 @@ def to_member_payload(row: dict, min_swim_test_age: int) -> dict:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def recalculate_deep_end(conn: sqlite3.Connection, member_id: int) -> None:
|
|
||||||
member = conn.execute(
|
|
||||||
"SELECT age, swim_test_passed FROM members WHERE id = ?;",
|
|
||||||
(member_id,),
|
|
||||||
).fetchone()
|
|
||||||
if member is None:
|
|
||||||
return
|
|
||||||
|
|
||||||
can_use = int(member["age"] >= 13 or bool(member["swim_test_passed"]))
|
|
||||||
conn.execute(
|
|
||||||
"UPDATE members SET can_use_deep_end = ? WHERE id = ?;",
|
|
||||||
(can_use, member_id),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def homepage():
|
def homepage():
|
||||||
return send_file(BASE_DIR / "index.html")
|
return send_file(BASE_DIR / "index.html")
|
||||||
|
|||||||
Reference in New Issue
Block a user