MEGAINFRA-WEBAPP/scripts/pb-add-dn-open-pages.py

49 lines
1.5 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
"""
Adds dn_batches.open_pages.
A page belonging to no document used to fail the pages_accounted check,
which put the whole batch in needs_attention. Batch 5qzcv6tvgb6cqks
promoted 3 of 3 notes correctly and still read as a failure because its
fourth page was a tax invoice cover sheet.
Open pages are questions, not failures: they are listed, counted, and
dismissable, while the requirement that every DETECTED note promotes is
unchanged.
Idempotent.
"""
import sys
from pb_env import PB, http, superuser_token
WANTED = {
"dn_batches": [
{"name": "open_pages", "type": "json", "required": False, "maxSize": 100000},
],
}
def main():
token = superuser_token()
print(f"\n→ {PB}")
for name, wanted in WANTED.items():
code, coll = http("GET", f"/api/collections/{name}", token=token)
if code != 200:
sys.exit(f"GET {name} failed: {code} {coll}")
fields = list(coll.get("fields", []))
have = {f.get("name") for f in fields}
add = [w for w in wanted if w["name"] not in have]
if not add:
print(f" • {name}: nothing to add")
continue
code, resp = http("PATCH", f"/api/collections/{coll['id']}", {"fields": fields + add}, token)
if code != 200:
sys.exit(f"PATCH {name} failed: {code} {resp}")
for a in add:
print(f" ✓ {name}: added {a['name']}")
if __name__ == "__main__":
main()
print("\nDone.")