mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-09 09:58:20 -05:00
Removed parameter, added documentation
This commit is contained in:
parent
8f8a99a645
commit
88ee3bdb6d
@ -126,6 +126,7 @@ script can access the following relevant environment variables set:
|
|||||||
| ----------------------- | ------------------------------------------------------------ |
|
| ----------------------- | ------------------------------------------------------------ |
|
||||||
| `DOCUMENT_SOURCE_PATH` | Original path of the consumed document |
|
| `DOCUMENT_SOURCE_PATH` | Original path of the consumed document |
|
||||||
| `DOCUMENT_WORKING_PATH` | Path to a copy of the original that consumption will work on |
|
| `DOCUMENT_WORKING_PATH` | Path to a copy of the original that consumption will work on |
|
||||||
|
| `TASK_ID` | UUID of the task used to process the new document (if any) |
|
||||||
|
|
||||||
!!! note
|
!!! note
|
||||||
|
|
||||||
@ -168,21 +169,22 @@ Executed after the consumer has successfully processed a document and
|
|||||||
has moved it into paperless. It receives the following environment
|
has moved it into paperless. It receives the following environment
|
||||||
variables:
|
variables:
|
||||||
|
|
||||||
| Environment Variable | Description |
|
| Environment Variable | Description |
|
||||||
| ---------------------------- | --------------------------------------------- |
|
| ---------------------------- | ---------------------------------------------- |
|
||||||
| `DOCUMENT_ID` | Database primary key of the document |
|
| `DOCUMENT_ID` | Database primary key of the document |
|
||||||
| `DOCUMENT_FILE_NAME` | Formatted filename, not including paths |
|
| `DOCUMENT_FILE_NAME` | Formatted filename, not including paths |
|
||||||
| `DOCUMENT_CREATED` | Date & time when document created |
|
| `DOCUMENT_CREATED` | Date & time when document created |
|
||||||
| `DOCUMENT_MODIFIED` | Date & time when document was last modified |
|
| `DOCUMENT_MODIFIED` | Date & time when document was last modified |
|
||||||
| `DOCUMENT_ADDED` | Date & time when document was added |
|
| `DOCUMENT_ADDED` | Date & time when document was added |
|
||||||
| `DOCUMENT_SOURCE_PATH` | Path to the original document file |
|
| `DOCUMENT_SOURCE_PATH` | Path to the original document file |
|
||||||
| `DOCUMENT_ARCHIVE_PATH` | Path to the generate archive file (if any) |
|
| `DOCUMENT_ARCHIVE_PATH` | Path to the generate archive file (if any) |
|
||||||
| `DOCUMENT_THUMBNAIL_PATH` | Path to the generated thumbnail |
|
| `DOCUMENT_THUMBNAIL_PATH` | Path to the generated thumbnail |
|
||||||
| `DOCUMENT_DOWNLOAD_URL` | URL for document download |
|
| `DOCUMENT_DOWNLOAD_URL` | URL for document download |
|
||||||
| `DOCUMENT_THUMBNAIL_URL` | URL for the document thumbnail |
|
| `DOCUMENT_THUMBNAIL_URL` | URL for the document thumbnail |
|
||||||
| `DOCUMENT_CORRESPONDENT` | Assigned correspondent (if any) |
|
| `DOCUMENT_CORRESPONDENT` | Assigned correspondent (if any) |
|
||||||
| `DOCUMENT_TAGS` | Comma separated list of tags applied (if any) |
|
| `DOCUMENT_TAGS` | Comma separated list of tags applied (if any) |
|
||||||
| `DOCUMENT_ORIGINAL_FILENAME` | Filename of original document |
|
| `DOCUMENT_ORIGINAL_FILENAME` | Filename of original document |
|
||||||
|
| `TASK_ID` | Task UUID used to import the document (if any) |
|
||||||
|
|
||||||
The script can be in any language, A simple shell script example:
|
The script can be in any language, A simple shell script example:
|
||||||
|
|
||||||
|
@ -186,7 +186,7 @@ class Consumer(LoggingMixin):
|
|||||||
f"Not consuming {self.filename}: Given ASN already exists!",
|
f"Not consuming {self.filename}: Given ASN already exists!",
|
||||||
)
|
)
|
||||||
|
|
||||||
def run_pre_consume_script(self, task_id):
|
def run_pre_consume_script(self):
|
||||||
"""
|
"""
|
||||||
If one is configured and exists, run the pre-consume script and
|
If one is configured and exists, run the pre-consume script and
|
||||||
handle its output and/or errors
|
handle its output and/or errors
|
||||||
@ -209,7 +209,7 @@ class Consumer(LoggingMixin):
|
|||||||
script_env = os.environ.copy()
|
script_env = os.environ.copy()
|
||||||
script_env["DOCUMENT_SOURCE_PATH"] = original_file_path
|
script_env["DOCUMENT_SOURCE_PATH"] = original_file_path
|
||||||
script_env["DOCUMENT_WORKING_PATH"] = working_file_path
|
script_env["DOCUMENT_WORKING_PATH"] = working_file_path
|
||||||
script_env["TASK_ID"] = task_id
|
script_env["TASK_ID"] = self.task_id or ""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
completed_proc = run(
|
completed_proc = run(
|
||||||
@ -234,7 +234,7 @@ class Consumer(LoggingMixin):
|
|||||||
exception=e,
|
exception=e,
|
||||||
)
|
)
|
||||||
|
|
||||||
def run_post_consume_script(self, document: Document, task_id):
|
def run_post_consume_script(self, document: Document):
|
||||||
"""
|
"""
|
||||||
If one is configured and exists, run the pre-consume script and
|
If one is configured and exists, run the pre-consume script and
|
||||||
handle its output and/or errors
|
handle its output and/or errors
|
||||||
@ -280,7 +280,7 @@ class Consumer(LoggingMixin):
|
|||||||
",".join(document.tags.all().values_list("name", flat=True)),
|
",".join(document.tags.all().values_list("name", flat=True)),
|
||||||
)
|
)
|
||||||
script_env["DOCUMENT_ORIGINAL_FILENAME"] = str(document.original_filename)
|
script_env["DOCUMENT_ORIGINAL_FILENAME"] = str(document.original_filename)
|
||||||
script_env["TASK_ID"] = task_id
|
script_env["TASK_ID"] = self.task_id or ""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
completed_proc = run(
|
completed_proc = run(
|
||||||
@ -390,7 +390,7 @@ class Consumer(LoggingMixin):
|
|||||||
logging_group=self.logging_group,
|
logging_group=self.logging_group,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.run_pre_consume_script(task_id=self.task_id)
|
self.run_pre_consume_script()
|
||||||
|
|
||||||
def progress_callback(current_progress, max_progress): # pragma: no cover
|
def progress_callback(current_progress, max_progress): # pragma: no cover
|
||||||
# recalculate progress to be within 20 and 80
|
# recalculate progress to be within 20 and 80
|
||||||
@ -555,7 +555,7 @@ class Consumer(LoggingMixin):
|
|||||||
document_parser.cleanup()
|
document_parser.cleanup()
|
||||||
tempdir.cleanup()
|
tempdir.cleanup()
|
||||||
|
|
||||||
self.run_post_consume_script(document, task_id=self.task_id)
|
self.run_post_consume_script(document)
|
||||||
|
|
||||||
self.log.info(f"Document {document} consumption finished")
|
self.log.info(f"Document {document} consumption finished")
|
||||||
|
|
||||||
|
@ -803,7 +803,7 @@ class PreConsumeTestCase(TestCase):
|
|||||||
def test_no_pre_consume_script(self, m):
|
def test_no_pre_consume_script(self, m):
|
||||||
c = Consumer()
|
c = Consumer()
|
||||||
c.path = "path-to-file"
|
c.path = "path-to-file"
|
||||||
c.run_pre_consume_script(str(uuid.uuid4()))
|
c.run_pre_consume_script()
|
||||||
m.assert_not_called()
|
m.assert_not_called()
|
||||||
|
|
||||||
@mock.patch("documents.consumer.run")
|
@mock.patch("documents.consumer.run")
|
||||||
@ -813,7 +813,7 @@ class PreConsumeTestCase(TestCase):
|
|||||||
c = Consumer()
|
c = Consumer()
|
||||||
c.filename = "somefile.pdf"
|
c.filename = "somefile.pdf"
|
||||||
c.path = "path-to-file"
|
c.path = "path-to-file"
|
||||||
self.assertRaises(ConsumerError, c.run_pre_consume_script, str(uuid.uuid4()))
|
self.assertRaises(ConsumerError, c.run_pre_consume_script)
|
||||||
|
|
||||||
@mock.patch("documents.consumer.run")
|
@mock.patch("documents.consumer.run")
|
||||||
def test_pre_consume_script(self, m):
|
def test_pre_consume_script(self, m):
|
||||||
@ -822,8 +822,8 @@ class PreConsumeTestCase(TestCase):
|
|||||||
c = Consumer()
|
c = Consumer()
|
||||||
c.original_path = "path-to-file"
|
c.original_path = "path-to-file"
|
||||||
c.path = "/tmp/somewhere/path-to-file"
|
c.path = "/tmp/somewhere/path-to-file"
|
||||||
task_id = str(uuid.uuid4())
|
c.task_id = str(uuid.uuid4())
|
||||||
c.run_pre_consume_script(task_id)
|
c.run_pre_consume_script()
|
||||||
|
|
||||||
m.assert_called_once()
|
m.assert_called_once()
|
||||||
|
|
||||||
@ -838,7 +838,7 @@ class PreConsumeTestCase(TestCase):
|
|||||||
subset = {
|
subset = {
|
||||||
"DOCUMENT_SOURCE_PATH": c.original_path,
|
"DOCUMENT_SOURCE_PATH": c.original_path,
|
||||||
"DOCUMENT_WORKING_PATH": c.path,
|
"DOCUMENT_WORKING_PATH": c.path,
|
||||||
"TASK_ID": task_id,
|
"TASK_ID": c.task_id,
|
||||||
}
|
}
|
||||||
self.assertDictEqual(environment, {**environment, **subset})
|
self.assertDictEqual(environment, {**environment, **subset})
|
||||||
|
|
||||||
@ -867,7 +867,7 @@ class PreConsumeTestCase(TestCase):
|
|||||||
c = Consumer()
|
c = Consumer()
|
||||||
c.path = "path-to-file"
|
c.path = "path-to-file"
|
||||||
|
|
||||||
c.run_pre_consume_script(str(uuid.uuid4()))
|
c.run_pre_consume_script()
|
||||||
self.assertIn(
|
self.assertIn(
|
||||||
"INFO:paperless.consumer:This message goes to stdout",
|
"INFO:paperless.consumer:This message goes to stdout",
|
||||||
cm.output,
|
cm.output,
|
||||||
@ -902,7 +902,6 @@ class PreConsumeTestCase(TestCase):
|
|||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
ConsumerError,
|
ConsumerError,
|
||||||
c.run_pre_consume_script,
|
c.run_pre_consume_script,
|
||||||
str(uuid.uuid4()),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -924,7 +923,7 @@ class PostConsumeTestCase(TestCase):
|
|||||||
doc.tags.add(tag1)
|
doc.tags.add(tag1)
|
||||||
doc.tags.add(tag2)
|
doc.tags.add(tag2)
|
||||||
|
|
||||||
Consumer().run_post_consume_script(doc, str(uuid.uuid4()))
|
Consumer().run_post_consume_script(doc)
|
||||||
|
|
||||||
m.assert_not_called()
|
m.assert_not_called()
|
||||||
|
|
||||||
@ -938,7 +937,6 @@ class PostConsumeTestCase(TestCase):
|
|||||||
ConsumerError,
|
ConsumerError,
|
||||||
c.run_post_consume_script,
|
c.run_post_consume_script,
|
||||||
doc,
|
doc,
|
||||||
str(uuid.uuid4()),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@mock.patch("documents.consumer.run")
|
@mock.patch("documents.consumer.run")
|
||||||
@ -947,7 +945,7 @@ class PostConsumeTestCase(TestCase):
|
|||||||
with override_settings(POST_CONSUME_SCRIPT=script.name):
|
with override_settings(POST_CONSUME_SCRIPT=script.name):
|
||||||
doc = Document.objects.create(title="Test", mime_type="application/pdf")
|
doc = Document.objects.create(title="Test", mime_type="application/pdf")
|
||||||
|
|
||||||
Consumer().run_post_consume_script(doc, str(uuid.uuid4()))
|
Consumer().run_post_consume_script(doc)
|
||||||
|
|
||||||
m.assert_called_once()
|
m.assert_called_once()
|
||||||
|
|
||||||
@ -965,9 +963,10 @@ class PostConsumeTestCase(TestCase):
|
|||||||
tag2 = Tag.objects.create(name="b")
|
tag2 = Tag.objects.create(name="b")
|
||||||
doc.tags.add(tag1)
|
doc.tags.add(tag1)
|
||||||
doc.tags.add(tag2)
|
doc.tags.add(tag2)
|
||||||
task_id = str(uuid.uuid4())
|
|
||||||
|
|
||||||
Consumer().run_post_consume_script(doc, task_id)
|
consumer = Consumer()
|
||||||
|
consumer.task_id = str(uuid.uuid4())
|
||||||
|
consumer.run_post_consume_script(doc)
|
||||||
|
|
||||||
m.assert_called_once()
|
m.assert_called_once()
|
||||||
|
|
||||||
@ -989,7 +988,7 @@ class PostConsumeTestCase(TestCase):
|
|||||||
"DOCUMENT_THUMBNAIL_URL": f"/api/documents/{doc.pk}/thumb/",
|
"DOCUMENT_THUMBNAIL_URL": f"/api/documents/{doc.pk}/thumb/",
|
||||||
"DOCUMENT_CORRESPONDENT": "my_bank",
|
"DOCUMENT_CORRESPONDENT": "my_bank",
|
||||||
"DOCUMENT_TAGS": "a,b",
|
"DOCUMENT_TAGS": "a,b",
|
||||||
"TASK_ID": task_id,
|
"TASK_ID": consumer.task_id,
|
||||||
}
|
}
|
||||||
|
|
||||||
self.assertDictEqual(environment, {**environment, **subset})
|
self.assertDictEqual(environment, {**environment, **subset})
|
||||||
@ -1018,4 +1017,4 @@ class PostConsumeTestCase(TestCase):
|
|||||||
doc = Document.objects.create(title="Test", mime_type="application/pdf")
|
doc = Document.objects.create(title="Test", mime_type="application/pdf")
|
||||||
c.path = "path-to-file"
|
c.path = "path-to-file"
|
||||||
with self.assertRaises(ConsumerError):
|
with self.assertRaises(ConsumerError):
|
||||||
c.run_post_consume_script(doc, str(uuid.uuid4()))
|
c.run_post_consume_script(doc)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user