fix: robust AIDA CSV parsing (handle ~~ vs ~ prefix, comma separator)

CSV format is '~~00341~~,~~0~~' or '~00341~~,~~0~~'.
Strip tildes and compare bare numbers instead of relying on
startswith/endswith which breaks with inconsistent tilde counts.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Christian Mueller 2026-04-23 22:51:56 +02:00
parent 5e9af4e268
commit a7efb12d5c

View File

@ -24,21 +24,40 @@ def read_aida_csv(csv_path):
return []
def _extract_nr(value):
"""Extract bare number from ~~00341~~ -> 00341."""
return value.strip().strip("~")
def determine_aida_status(lines, personal_nr, settings):
"""Determine AIDA status for a user based on their personal number."""
if not personal_nr or not lines:
return None
matching = [l for l in lines if l.startswith(personal_nr.lower())]
nr = _extract_nr(personal_nr)
if not nr:
return None
# Find line containing this personal number
matching = [l for l in lines if nr in l]
if not matching:
return None
line = matching[0]
if line.endswith(settings.get("aida_status_abwesend", "~~0~~")):
# Check status part (after comma or at end)
status_abwesend = _extract_nr(settings.get("aida_status_abwesend", "~~0~~"))
status_anwesend = _extract_nr(settings.get("aida_status_anwesend", "~~1~~"))
status_homeoffice = _extract_nr(settings.get("aida_status_homeoffice", "~~2~~"))
# Get the status value (last part after comma, stripped of ~)
parts = line.split(",")
status_val = _extract_nr(parts[-1]) if len(parts) > 1 else ""
if status_val == status_abwesend:
return "abwesend"
if line.endswith(settings.get("aida_status_anwesend", "~~1~~")):
if status_val == status_anwesend:
return "anwesend"
if line.endswith(settings.get("aida_status_homeoffice", "~~2~~")):
if status_val == status_homeoffice:
return "homeoffice"
return None