mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-12-29 13:48:09 -06:00
Performance: avoid unnecessary filename operations on bulk custom field updates (#11558)
This commit is contained in:
@@ -16,6 +16,7 @@ from django.utils import timezone
|
||||
from documents.file_handling import create_source_path_directory
|
||||
from documents.file_handling import delete_empty_directories
|
||||
from documents.file_handling import generate_filename
|
||||
from documents.file_handling import generate_unique_filename
|
||||
from documents.models import Correspondent
|
||||
from documents.models import CustomField
|
||||
from documents.models import CustomFieldInstance
|
||||
@@ -1632,6 +1633,73 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
|
||||
)
|
||||
|
||||
|
||||
class TestCustomFieldFilenameUpdates(
|
||||
DirectoriesMixin,
|
||||
FileSystemAssertsMixin,
|
||||
TestCase,
|
||||
):
|
||||
def setUp(self):
|
||||
self.cf = CustomField.objects.create(
|
||||
name="flavor",
|
||||
data_type=CustomField.FieldDataType.STRING,
|
||||
)
|
||||
self.doc = Document.objects.create(
|
||||
title="document",
|
||||
mime_type="application/pdf",
|
||||
checksum="abc123",
|
||||
)
|
||||
self.cfi = CustomFieldInstance.objects.create(
|
||||
field=self.cf,
|
||||
document=self.doc,
|
||||
value_text="initial",
|
||||
)
|
||||
return super().setUp()
|
||||
|
||||
@override_settings(FILENAME_FORMAT=None)
|
||||
def test_custom_field_not_in_template_skips_filename_work(self):
|
||||
storage_path = StoragePath.objects.create(path="{{created}}/{{ title }}")
|
||||
self.doc.storage_path = storage_path
|
||||
self.doc.save()
|
||||
initial_filename = generate_filename(self.doc)
|
||||
Document.objects.filter(pk=self.doc.pk).update(filename=str(initial_filename))
|
||||
self.doc.refresh_from_db()
|
||||
Path(self.doc.source_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
Path(self.doc.source_path).touch()
|
||||
|
||||
with mock.patch("documents.signals.handlers.generate_unique_filename") as m:
|
||||
m.side_effect = generate_unique_filename
|
||||
self.cfi.value_text = "updated"
|
||||
self.cfi.save()
|
||||
|
||||
self.doc.refresh_from_db()
|
||||
self.assertEqual(Path(self.doc.filename), initial_filename)
|
||||
self.assertEqual(m.call_count, 0)
|
||||
|
||||
@override_settings(FILENAME_FORMAT=None)
|
||||
def test_custom_field_in_template_triggers_filename_update(self):
|
||||
storage_path = StoragePath.objects.create(
|
||||
path="{{ custom_fields|get_cf_value('flavor') }}/{{ title }}",
|
||||
)
|
||||
self.doc.storage_path = storage_path
|
||||
self.doc.save()
|
||||
initial_filename = generate_filename(self.doc)
|
||||
Document.objects.filter(pk=self.doc.pk).update(filename=str(initial_filename))
|
||||
self.doc.refresh_from_db()
|
||||
Path(self.doc.source_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
Path(self.doc.source_path).touch()
|
||||
|
||||
with mock.patch("documents.signals.handlers.generate_unique_filename") as m:
|
||||
m.side_effect = generate_unique_filename
|
||||
self.cfi.value_text = "updated"
|
||||
self.cfi.save()
|
||||
|
||||
self.doc.refresh_from_db()
|
||||
expected_filename = Path("updated/document.pdf")
|
||||
self.assertEqual(Path(self.doc.filename), expected_filename)
|
||||
self.assertTrue(Path(self.doc.source_path).is_file())
|
||||
self.assertLessEqual(m.call_count, 1)
|
||||
|
||||
|
||||
class TestPathDateLocalization:
|
||||
"""
|
||||
Groups all tests related to the `localize_date` function.
|
||||
|
||||
Reference in New Issue
Block a user