MEGAINFRA-WEBAPP/scripts/pb-add-site-coordinates.py
DEls 08ed89d9c8 feat(dashboard): enhance operations dashboard with rain outlook and coordinate support
Updated the operations dashboard to include a rain outlook feature, providing users with a 7-day forecast based on site coordinates. Added latitude and longitude fields to the site form for better weather data integration. Enhanced the fuel log and site management functionalities to utilize dynamic fuel pricing based on historical data. Improved overall data handling and user experience across various dashboard components.
2026-09-06 19:59:52 +02:00

55 lines
1.8 KiB
Python

#!/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."
)