mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
Updated Post Consume Script Examples (markdown)
parent
9d082a6685
commit
00b1767b9a
@ -1,22 +1,24 @@
|
||||
This wiki page is a repository of example [post-consume scripts](https://docs.paperless-ngx.com/advanced_usage/#post-consume-script) 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.
|
||||
|
||||
## Python + API
|
||||
|
||||
A more advanced post consume script which uses Python and the API to assign some new values to a document which has just been consumed. Make sure the script has execute permissions.
|
||||
|
||||
```python
|
||||
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# TODO: The user can use anything in the standard library, installed for paperless
|
||||
# or use the custom startup scripts to install additional libraries via pip
|
||||
import requests
|
||||
import json
|
||||
import sys
|
||||
from datetime import datetime
|
||||
import os
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
def _set_auth_tokens(paperless_url: str, session: requests.Session) -> Tuple[str, str]:
|
||||
def _set_auth_tokens(paperless_url: str, timeout: float, session: requests.Session):
|
||||
# TODO: You fill these in or otherwise provide them
|
||||
credentials = {"username": "test", "password": "test"}
|
||||
|
||||
response = sess.get(paperless_url)
|
||||
response = sess.get(paperless_url, timeout=timeout)
|
||||
response.raise_for_status()
|
||||
|
||||
csrf_token = response.cookies["csrftoken"]
|
||||
@ -25,29 +27,34 @@ def _set_auth_tokens(paperless_url: str, session: requests.Session) -> Tuple[str
|
||||
paperless_url + "/api/token/",
|
||||
data=credentials,
|
||||
headers={"X-CSRFToken": csrf_token},
|
||||
timeout=timeout,
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
api_token = response.json()["token"]
|
||||
|
||||
sess.headers.update(
|
||||
session.headers.update(
|
||||
{"Authorization": f"Token {api_token}", f"X-CSRFToken": csrf_token}
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Running inside the Docker container
|
||||
paperless_url = "http://192.168.1.12:8180"
|
||||
# TODO: Update this as needed
|
||||
paperless_url = "http://localhost:8000"
|
||||
timeout = 5.0
|
||||
|
||||
with requests.Session() as sess:
|
||||
# Set tokens for the appropriate header auth
|
||||
_set_auth_tokens(paperless_url, sess)
|
||||
_set_auth_tokens(paperless_url, timeout, sess)
|
||||
|
||||
# Get the PK as provided via post-consume
|
||||
doc_pk = int(os.environ["DOCUMENT_ID"])
|
||||
|
||||
# Query the API for the document info
|
||||
doc_info_resp = sess.get(paperless_url + f"/api/documents/{doc_pk}/")
|
||||
doc_info_resp = sess.get(
|
||||
paperless_url + f"/api/documents/{doc_pk}/", timeout=timeout
|
||||
)
|
||||
doc_info_resp.raise_for_status()
|
||||
doc_info = doc_info_resp.json()
|
||||
|
||||
@ -60,14 +67,14 @@ if __name__ == "__main__":
|
||||
# Parse, set, otherwise choose new values
|
||||
# TODO: Up to the user to decide how these new values should be set
|
||||
# Use regex, etc to get the primary key of the new values
|
||||
new_correspondent = 123
|
||||
new_doc_type = 456
|
||||
new_correspondent = 1
|
||||
new_doc_type = 1
|
||||
|
||||
# Update the document
|
||||
resp = sess.patch(
|
||||
paperless_url + f"/api/documents/{doc_pk}/",
|
||||
data={"correspondent": new_correspondent, "document_type": new_doc_type},
|
||||
timeout=timeout,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user