Skip to content

Commit

Permalink
Allow sorting by /donor table keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Latent-Logic committed May 5, 2024
1 parent 314a2cd commit 7766326
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions test_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,14 @@ async def get_events(timezone: Optional[str] = None):


@app.get("/donors", response_class=HTMLResponse)
async def get_donors():
async def get_donors(sort: str = "total"):
donor_keys = {
"name": (lambda x: x["name"].lower(), False),
"total": (lambda x: x["total"], True),
**{k: (lambda x: x[k], True) for k in CSV_TYPES},
}
if sort not in donor_keys:
return f"<html><body><xmp>{sort} not in {tuple(donor_keys.keys())}</xmp></body></html>"
donor_db = {}
with Path(SETTINGS.db.events).open("r", encoding="utf-8") as f:
reader = csv.DictReader(f, delimiter=",")
Expand All @@ -658,9 +665,9 @@ async def get_donors():
user_db["total"] += amount * SETTINGS.get_value(row["type"])

build_table = "<table>\n"
build_table += "<tr>" + "".join(f"<th>{s}</th>" for s in ("name", "total", *CSV_TYPES)) + "</tr>\n"
for row in sorted(donor_db.values(), key=lambda x: x["total"], reverse=True):
build_table += "<tr>" + "".join(f"<td>{row[s]}</td>" for s in ("name", "total", *CSV_TYPES)) + "</tr>\n"
build_table += "<tr>" + "".join(f"<th>{s}</th>" for s in donor_keys) + "</tr>\n"
for row in sorted(donor_db.values(), key=donor_keys[sort][0], reverse=donor_keys[sort][1]):
build_table += "<tr>" + "".join(f"<td>{row[s]}</td>" for s in donor_keys) + "</tr>\n"
build_table += "</table>\n"
style = """table {border: 2px solid rgb(140 140 140);}
th,td {border: 1px solid rgb(160 160 160);}"""
Expand Down

0 comments on commit 7766326

Please sign in to comment.