[birds] Add much better bird importing
Some checks failed
build & deploy / test (push) Failing after 1m19s
build & deploy / build-and-deploy (push) Has been skipped

This commit is contained in:
2026-05-15 12:10:20 -04:00
parent b967c526f1
commit f160f5a7b8
15 changed files with 550 additions and 27 deletions

View File

@ -46,6 +46,21 @@ def parse_timestamp(date_str, time_str):
return None
def parse_bool(value):
if not value:
return None
return value.strip().lower() in ("true", "yes", "1")
def parse_int(value):
if not value:
return None
try:
return int(value.strip())
except (ValueError, TypeError):
return None
def import_birding_csv(file_path, user_id):
user = User.objects.get(id=user_id)
new_scrobbles = []
@ -72,7 +87,9 @@ def import_birding_csv(file_path, user_id):
if not timestamp:
continue
location_title = LOCATION_COORDS_RE.sub("", location_str).strip().rstrip(",").strip()
location_title = (
LOCATION_COORDS_RE.sub("", location_str).strip().rstrip(",").strip()
)
if not location_title:
location_title = location_str
@ -90,19 +107,13 @@ def import_birding_csv(file_path, user_id):
location.save(update_fields=["geo_location"])
first_row = sighting_rows[0]
duration_minutes = parse_duration(first_row.get("Duration", ""))
party_size = first_row.get("Party Size", "").strip()
birds_data = []
for row in sighting_rows:
species = row.get("Species", "").strip()
if not species:
continue
count_str = row.get("Count", "1").strip()
try:
count = int(count_str) if count_str else 1
except ValueError:
count = 1
count = parse_int(row.get("Count")) or 1
details = row.get("Details", "").strip()
bird = Bird.find_or_create(species)
@ -113,19 +124,15 @@ def import_birding_csv(file_path, user_id):
logdata = BirdSightingLogData(
birds=birds_data,
duration_minutes=duration_minutes,
duration_minutes=parse_duration(first_row.get("Duration", "")),
observation_type=first_row.get("Observation Type", "").strip() or None,
distance=first_row.get("Distance", "").strip() or None,
area=first_row.get("Area", "").strip() or None,
party_size=parse_int(first_row.get("Party Size")),
complete_checklist=parse_bool(first_row.get("Complete Checklist")),
)
notes = []
if party_size:
notes.append(f"Party size: {party_size}")
checklist_flag = first_row.get("Complete Checklist", "").strip()
if checklist_flag:
notes.append(f"Complete checklist: {checklist_flag}")
log_dict = logdata.asdict
if notes:
log_dict["notes"] = notes
scrobble = Scrobble(
user=user,