WFDASH-WEBAPP/scripts/utils/_probe_po_state.py

71 lines
2.5 KiB
Python
Raw Permalink Normal View History

import os, json, urllib.request
# Read PB_URL from .env.local
pb_url = None
with open('.env.local') as f:
for line in f:
if line.startswith('NEXT_PUBLIC_POCKETBASE_URL'):
pb_url = line.split('=', 1)[1].strip().strip('"').strip("'").strip()
break
print(f"PB_URL: {pb_url}")
# Auth
req = urllib.request.Request(
pb_url + '/api/collections/_superusers/auth-with-password',
data=json.dumps({"identity": "david.els1@outlook.com", "password": "1234567890"}).encode(),
headers={"Content-Type": "application/json"},
)
with urllib.request.urlopen(req) as r:
token = json.load(r)['token']
print(f"Auth token: {token[:40]}...")
# Schema for purchase_orders
req = urllib.request.Request(
pb_url + '/api/collections/pbc_1342968361',
headers={"Authorization": token},
)
with urllib.request.urlopen(req) as r:
d = json.load(r)
print("\n=== purchase_orders fields ===")
for f in d.get('fields', []):
opts = f.get('options', {}) or {}
max_files = opts.get('maxFiles', '')
max_size = opts.get('maxSize', '')
extras = f" (maxFiles={max_files}, maxSize={max_size})" if 'file' in f.get('type','') else ""
print(f" {f.get('name','?'):30} {f.get('type','?'):15}{extras}")
# Sample record
req = urllib.request.Request(
pb_url + '/api/collections/pbc_1342968361/records?perPage=1',
headers={"Authorization": token},
)
with urllib.request.urlopen(req) as r:
d = json.load(r)
items = d.get('items', [])
if items:
print(f"\n=== Sample record keys ({len(items[0].keys())} keys) ===")
for k in items[0].keys():
v = items[0][k]
if isinstance(v, list):
print(f" {k}: list[{len(v)}]")
elif isinstance(v, str) and len(v) > 60:
print(f" {k}: {v[:60]}...")
else:
print(f" {k}: {v!r}")
else:
print("\nNo records found")
# Check purchase_order_pdfs still exists
req = urllib.request.Request(
pb_url + '/api/collections/pbc_4269390732/records?perPage=2&sort=-created',
headers={"Authorization": token},
)
try:
with urllib.request.urlopen(req) as r:
d = json.load(r)
print(f"\n=== purchase_order_pdfs (still active?) — {d.get('totalItems', 0)} rows ===")
for it in d.get('items', [])[:2]:
print(f" {it.get('id')}: file={it.get('file')}, status={it.get('status')}, created={it.get('created')}")
except Exception as e:
print(f"\npurchase_order_pdfs check failed: {e}")