Fallback to title if title assignment invalid

This commit is contained in:
shamoon 2024-01-06 22:13:00 -08:00
parent 416ad13aaf
commit 22cc82641c
4 changed files with 85 additions and 18 deletions

View File

@ -726,12 +726,17 @@ class Consumer(LoggingMixin):
storage_type = Document.STORAGE_TYPE_UNENCRYPTED
title = file_info.title[:127]
if self.override_title is not None:
try:
title = self._parse_title_placeholders(self.override_title)
except Exception:
self.log.error(
f"Error occurred parsing title override '{self.override_title}', falling back to original",
)
document = Document.objects.create(
title=(
self._parse_title_placeholders(self.override_title)
if self.override_title is not None
else file_info.title
)[:127],
title=title,
content=text,
mime_type=mime_type,
checksum=hashlib.md5(self.working_copy.read_bytes()).hexdigest(),

View File

@ -570,6 +570,7 @@ def run_workflow(
document.owner = action.assign_owner
if action.assign_title is not None:
try:
document.title = parse_doc_title_w_placeholders(
action.assign_title,
document.correspondent.name
@ -578,10 +579,17 @@ def run_workflow(
document.document_type.name
if document.document_type is not None
else "",
document.owner.username if document.owner is not None else "",
timezone.localtime(document.added),
document.owner.username
if document.owner is not None
else "",
document.added,
document.original_filename,
timezone.localtime(document.created),
document.created,
)
except Exception:
logger.error(
f"Error occurred parsing title assignment '{action.assign_title}', falling back to original",
extra={"group": logging_group},
)
if (

View File

@ -423,6 +423,16 @@ class TestConsumer(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertEqual(document.title, "Override Title")
self._assert_first_last_send_progress()
def testOverrideTitleInvalidPlaceholders(self):
with self.assertLogs("paperless.consumer", level="ERROR") as cm:
document = self.consumer.try_consume_file(
self.get_test_file(),
override_title="Override {correspondent]",
)
self.assertEqual(document.title, "sample")
expected_str = "Error occurred parsing title override 'Override {correspondent]', falling back to original"
self.assertIn(expected_str, cm.output[0])
def testOverrideCorrespondent(self):
c = Correspondent.objects.create(name="test")

View File

@ -966,6 +966,50 @@ class TestWorkflows(DirectoriesMixin, FileSystemAssertsMixin, APITestCase):
expected_str = f"Document correspondent {doc.correspondent} does not match {trigger.filter_has_correspondent}"
self.assertIn(expected_str, cm.output[1])
def test_document_added_invalid_title_placeholders(self):
"""
GIVEN:
- Existing workflow with added trigger type
- Assign title field has an error
WHEN:
- File that matches is added
THEN:
- Title is not updated, error is output
"""
trigger = WorkflowTrigger.objects.create(
type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_ADDED,
filter_filename="*sample*",
)
action = WorkflowAction.objects.create(
assign_title="Doc {created_year]",
)
w = Workflow.objects.create(
name="Workflow 1",
order=0,
)
w.triggers.add(trigger)
w.actions.add(action)
w.save()
now = timezone.localtime(timezone.now())
created = now - timedelta(weeks=520)
doc = Document.objects.create(
original_filename="sample.pdf",
title="sample test",
content="Hello world bar",
created=created,
)
with self.assertLogs("paperless.handlers", level="ERROR") as cm:
document_consumption_finished.send(
sender=self.__class__,
document=doc,
)
expected_str = f"Error occurred parsing title assignment '{action.assign_title}', falling back to original"
self.assertIn(expected_str, cm.output[0])
self.assertEqual(doc.title, "sample test")
def test_document_updated_workflow(self):
trigger = WorkflowTrigger.objects.create(
type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_UPDATED,