95 lines
2.9 KiB
Python
95 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Grant view permissions on non-personal Paperless documents to family users."""
|
|
|
|
import os
|
|
import sys
|
|
|
|
import httpx
|
|
|
|
PAPERLESS_URL = os.environ.get("PAPERLESS_URL", "https://docs.loogle.it").rstrip("/")
|
|
ADMIN_TOKEN = os.environ.get("PAPERLESS_API_TOKEN_DANIELE") or os.environ.get("PAPERLESS_API_TOKEN", "")
|
|
FAMILY_USER_IDS = [4, 5, 6] # lucia, davide, luca
|
|
PERSONAL_TAG_NAMES = {"personal", "privato", "private"}
|
|
BATCH_SIZE = 50
|
|
|
|
|
|
def main() -> int:
|
|
if not ADMIN_TOKEN:
|
|
print("Missing PAPERLESS_API_TOKEN_DANIELE", file=sys.stderr)
|
|
return 1
|
|
headers = {"Authorization": f"Token {ADMIN_TOKEN}", "Content-Type": "application/json"}
|
|
|
|
tags_resp = httpx.get(f"{PAPERLESS_URL}/api/tags/", headers=headers, timeout=60)
|
|
tags_resp.raise_for_status()
|
|
personal_tag_ids = {
|
|
t["id"]
|
|
for t in tags_resp.json().get("results", [])
|
|
if (t.get("name") or "").lower() in PERSONAL_TAG_NAMES
|
|
}
|
|
|
|
doc_ids: list[int] = []
|
|
page = 1
|
|
while True:
|
|
resp = httpx.get(
|
|
f"{PAPERLESS_URL}/api/documents/",
|
|
headers=headers,
|
|
params={"page": page, "page_size": 100},
|
|
timeout=60,
|
|
)
|
|
resp.raise_for_status()
|
|
data = resp.json()
|
|
for doc in data.get("results", []):
|
|
if set(doc.get("tags") or []) & personal_tag_ids:
|
|
continue
|
|
doc_ids.append(doc["id"])
|
|
if not data.get("next"):
|
|
break
|
|
page += 1
|
|
|
|
print(f"Documenti da condividere (senza tag personali): {len(doc_ids)}")
|
|
|
|
updated = 0
|
|
for i in range(0, len(doc_ids), BATCH_SIZE):
|
|
chunk = doc_ids[i : i + BATCH_SIZE]
|
|
payload = {
|
|
"documents": chunk,
|
|
"method": "set_permissions",
|
|
"parameters": {
|
|
"set_permissions": {
|
|
"view": {"users": FAMILY_USER_IDS, "groups": []},
|
|
"change": {"users": [], "groups": []},
|
|
},
|
|
"merge": True,
|
|
},
|
|
}
|
|
resp = httpx.post(
|
|
f"{PAPERLESS_URL}/api/documents/bulk_edit/",
|
|
headers=headers,
|
|
json=payload,
|
|
timeout=120,
|
|
)
|
|
resp.raise_for_status()
|
|
updated += len(chunk)
|
|
print(f" Aggiornati {updated}/{len(doc_ids)}")
|
|
|
|
for name, env_key in [
|
|
("lucia", "PAPERLESS_API_TOKEN_LUCIA"),
|
|
("davide", "PAPERLESS_API_TOKEN_DAVIDE"),
|
|
("luca", "PAPERLESS_API_TOKEN_LUCA"),
|
|
]:
|
|
token = os.environ.get(env_key, "")
|
|
if not token:
|
|
continue
|
|
count = httpx.get(
|
|
f"{PAPERLESS_URL}/api/documents/?page_size=1",
|
|
headers={"Authorization": f"Token {token}"},
|
|
timeout=30,
|
|
).json().get("count", 0)
|
|
print(f"{name}: documenti visibili = {count}")
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|