From 04bcd89cfbef0d954ca64ce5fee3c4bb4fd74054 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 7 Dec 2023 22:45:17 -0800 Subject: [PATCH] Destroyed General Script Examples (markdown) --- General-Script-Examples.md | 109 ------------------------------------- 1 file changed, 109 deletions(-) delete mode 100644 General-Script-Examples.md diff --git a/General-Script-Examples.md b/General-Script-Examples.md deleted file mode 100644 index fd2c5d1..0000000 --- a/General-Script-Examples.md +++ /dev/null @@ -1,109 +0,0 @@ -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 storage path - -Our usecase is to interpret the storage path as a clients file and only give view permissions to employees that handle this client. -This script could be used as a post-processing script if you are confident that the storage path is allways set correctly and not changed later. We just run it at regular intervals. - -```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