feat(db): normalize usernames in enrollment_rank for case insensitivity

This commit is contained in:
shay
2026-04-22 12:50:14 -04:00
parent 5102a36238
commit 7f1e53cfad
3 changed files with 25 additions and 3 deletions
+10 -2
View File
@@ -197,9 +197,17 @@ def enrollments_today() -> int:
def enrollment_rank(username: str) -> Optional[int]: def enrollment_rank(username: str) -> Optional[int]:
"""1-based position in overall enrollment order by enrolled_at. Used by """1-based position in overall enrollment order by enrolled_at. Used by
tag rules that reward early users (e.g. the 'founder' tag).""" tag rules that reward early users (e.g. the 'founder' tag).
Usernames are persisted lowercased by `enroll()`, but callers may pass the
GitHub-returned login (which preserves the original casing). Normalize
here so a mixed-case lookup doesn't silently miss the row.
"""
if not username:
return None
key = username.lower()
with _settings_conn() as c: with _settings_conn() as c:
row = c.execute("SELECT enrolled_at FROM users WHERE username=?", (username,)).fetchone() row = c.execute("SELECT enrolled_at FROM users WHERE username=?", (key,)).fetchone()
if row is None: if row is None:
return None return None
return c.execute( return c.execute(
+4 -1
View File
@@ -67,7 +67,10 @@ def render_grade_widget(data: GradeData, theme_name: str = "dark", settings: dic
radius = 36 radius = 36
circumference = 2 * math.pi * radius circumference = 2 * math.pi * radius
offset = circumference * (1 - score / 100) # S-tier grades get a fully closed ring regardless of exact score — the
# letter "S" itself carries the "top of the scale" meaning, so the visual
# should match. S+, S++ share the full circle.
offset = 0 if base == "S" else circumference * (1 - score / 100)
grade_font = 30 if len(data.grade) <= 2 else 22 grade_font = 30 if len(data.grade) <= 2 else 22
# Stats row with icons # Stats row with icons
+11
View File
@@ -45,6 +45,17 @@ def test_enrollments_daily_counter(tmp_dbs):
assert dbmod.enrollments_today() == 2 assert dbmod.enrollments_today() == 2
def test_enrollment_rank_is_case_insensitive(tmp_dbs):
"""GitHub returns logins with original casing ("ShayManor") but the users
table stores them lowercased. Without normalization the founder-tag rule
silently misses everyone whose login has capitals."""
dbmod.enroll("shaymanor", {"theme": "dark"})
# Mixed / upper case must find the same row as the lowercased insert.
assert dbmod.enrollment_rank("ShayManor") is not None
assert dbmod.enrollment_rank("SHAYMANOR") == dbmod.enrollment_rank("shaymanor")
assert dbmod.enrollment_rank("missing") is None
def test_update_settings_changes_hash(tmp_dbs): def test_update_settings_changes_hash(tmp_dbs):
dbmod.enroll("alice", {"theme": "dark"}) dbmod.enroll("alice", {"theme": "dark"})
h_before = dbmod.get_settings("alice")["settings_hash"] h_before = dbmod.get_settings("alice")["settings_hash"]