diff --git a/General-Script-Examples.md b/General-Script-Examples.md new file mode 100644 index 0000000..5b1bde6 --- /dev/null +++ b/General-Script-Examples.md @@ -0,0 +1,106 @@ +This wiki page is a repository of example scripts contributed by the community. As always, you should exercise caution when using a script and make sure you understand the code before using a script from the internet. + +## updating document permissions based on the permissions of the documents storage path + +```python +#!/usr/bin/env python3 + +import requests +import json +import logging +import time + +# this script updates document permissions based on the permissions of the documents storage path. +# runtime of this script could be improved by only checking documents that have beed added or updated since the last run + + +def _set_auth_tokens(paperless_url: str, timeout: float, session: requests.Session): + # TODO: You fill these in or otherwise provide them + credentials = {"username": "xxxxxx", "password": "xxxxxxxxx"} + + response = sess.get(paperless_url, timeout=timeout) + response.raise_for_status() + + csrf_token = response.cookies["csrftoken"] + + response = sess.post( + paperless_url + "/api/token/", + data=credentials, + headers={"X-CSRFToken": csrf_token}, + timeout=timeout, + ) + response.raise_for_status() + + api_token = response.json()["token"] + + session.headers.update( + {"Authorization": f"Token {api_token}", f"X-CSRFToken": csrf_token} + ) + + +# TODO: Update this as needed +paperless_url = "http://localhost:8000" +timeout = 5.0 + +starttime = round(time.time() * 1000) +logging.basicConfig( + filename="permission_update_deamon.log", + encoding="utf-8", + level=logging.INFO, + format="%(asctime)s %(message)s", +) + + +with requests.Session() as sess: + # Set tokens for the appropriate header auth + _set_auth_tokens(paperless_url, timeout, sess) + + # Query the API for the document info + doc_info_resp = sess.get( + paperless_url + f"/api/documents/?full_perms=true", timeout=timeout + ) + doc_info_resp.raise_for_status() + doc_info = doc_info_resp.json() + + # Query the API for the storage_path info + path_info_resp = sess.get( + paperless_url + f"/api/storage_paths/?full_perms=true", timeout=timeout + ) + path_info_resp.raise_for_status() + path_info = path_info_resp.json() + + path_dict = {} + + for path in path_info["results"]: + path_dict[path["id"]] = path + + updated_documents = [] + + for document in doc_info["results"]: + cur_path = document["storage_path"] + # check if document permissions equal path permissions + if cur_path is not None and not ( + document["owner"] == path_dict[cur_path]["owner"] + and document["permissions"] == path_dict[cur_path]["permissions"] + ): + # Update the document + resp = sess.patch( + paperless_url + f"/api/documents/{document['id']}/?full_perms=true/", + headers={"charset": "utf-8", "Content-type": "application/json"}, + data=json.dumps( + { + "owner": path_dict[cur_path]["owner"], + "set_permissions": path_dict[cur_path]["permissions"], + } + ), + timeout=timeout, + ) + resp.raise_for_status() + updated_documents.append(document["id"]) + +endtime = round(time.time() * 1000) +logging.info( + f"finished in {endtime-starttime}ms updated documents: {updated_documents}" +) + +``` \ No newline at end of file