#!/usr/bin/env python3 """Add `latitude` / `longitude` to the SITES collection. Needed by the rain outlook card on the Overview page — Open-Meteo is queried per site coordinate. Sites without coordinates are listed as "no location set"; nothing else in the app reads these fields. Idempotent: fields already present are left alone. Existing fields and data are preserved — this PATCHes the collection's field list rather than replacing it. Run `pb-backup-collection.py SITES` first if you want a schema snapshot to fall back on. Credentials come from .env.local (or the environment): python3 scripts/pb-add-site-coordinates.py """ import sys from pb_env import http, superuser_token TOKEN = superuser_token() code, sites = http("GET", "/api/collections/SITES", token=TOKEN) if code != 200: sys.exit(f"could not read SITES: {code} {sites}") fields = sites.get("fields", []) existing = {f.get("name") for f in fields} NEW = [ {"name": "latitude", "type": "number", "required": False, "min": -90, "max": 90}, {"name": "longitude", "type": "number", "required": False, "min": -180, "max": 180}, ] added = [f for f in NEW if f["name"] not in existing] if not added: print(" • latitude / longitude already present — nothing to do") sys.exit(0) code, resp = http( "PATCH", f"/api/collections/{sites['id']}", {"fields": fields + added}, TOKEN, ) if code != 200: sys.exit(f" ✗ PATCH FAILED: {code} {resp}") print(f" ✓ added {', '.join(f['name'] for f in added)} to SITES") print(f" fields now: {', '.join(f['name'] for f in resp.get('fields', []))}") print( "\nNext: set each site's coordinates via Edit on the site, or in the\n" "PocketBase admin UI. Right-click a site in Google Maps and the first\n" "menu entry is the 'latitude, longitude' pair." )