mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-12-12 01:11:17 -06:00
Avoid unnecessary renaming
This commit is contained in:
@@ -28,6 +28,7 @@ from documents import matching
|
|||||||
from documents.caching import clear_document_caches
|
from documents.caching import clear_document_caches
|
||||||
from documents.file_handling import create_source_path_directory
|
from documents.file_handling import create_source_path_directory
|
||||||
from documents.file_handling import delete_empty_directories
|
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.file_handling import generate_unique_filename
|
||||||
from documents.models import CustomField
|
from documents.models import CustomField
|
||||||
from documents.models import CustomFieldInstance
|
from documents.models import CustomFieldInstance
|
||||||
@@ -452,21 +453,47 @@ def update_filename_and_move_files(
|
|||||||
old_filename = instance.filename
|
old_filename = instance.filename
|
||||||
old_source_path = instance.source_path
|
old_source_path = instance.source_path
|
||||||
|
|
||||||
|
candidate_filename = generate_filename(instance)
|
||||||
|
candidate_source_path = (
|
||||||
|
settings.ORIGINALS_DIR / candidate_filename
|
||||||
|
).resolve()
|
||||||
|
if candidate_filename == Path(old_filename):
|
||||||
|
new_filename = Path(old_filename)
|
||||||
|
elif (
|
||||||
|
candidate_source_path.exists()
|
||||||
|
and candidate_source_path != old_source_path
|
||||||
|
):
|
||||||
|
# Only fall back to unique search when there is an actual conflict
|
||||||
|
new_filename = generate_unique_filename(instance)
|
||||||
|
else:
|
||||||
|
new_filename = candidate_filename
|
||||||
|
|
||||||
# Need to convert to string to be able to save it to the db
|
# Need to convert to string to be able to save it to the db
|
||||||
instance.filename = str(generate_unique_filename(instance))
|
instance.filename = str(new_filename)
|
||||||
move_original = old_filename != instance.filename
|
move_original = old_filename != instance.filename
|
||||||
|
|
||||||
old_archive_filename = instance.archive_filename
|
old_archive_filename = instance.archive_filename
|
||||||
old_archive_path = instance.archive_path
|
old_archive_path = instance.archive_path
|
||||||
|
|
||||||
if instance.has_archive_version:
|
if instance.has_archive_version:
|
||||||
# Need to convert to string to be able to save it to the db
|
archive_candidate = generate_filename(instance, archive_filename=True)
|
||||||
instance.archive_filename = str(
|
archive_candidate_path = (
|
||||||
generate_unique_filename(
|
settings.ARCHIVE_DIR / archive_candidate
|
||||||
|
).resolve()
|
||||||
|
if archive_candidate == Path(old_archive_filename):
|
||||||
|
new_archive_filename = Path(old_archive_filename)
|
||||||
|
elif (
|
||||||
|
archive_candidate_path.exists()
|
||||||
|
and archive_candidate_path != old_archive_path
|
||||||
|
):
|
||||||
|
new_archive_filename = generate_unique_filename(
|
||||||
instance,
|
instance,
|
||||||
archive_filename=True,
|
archive_filename=True,
|
||||||
),
|
)
|
||||||
)
|
else:
|
||||||
|
new_archive_filename = archive_candidate
|
||||||
|
|
||||||
|
instance.archive_filename = str(new_archive_filename)
|
||||||
|
|
||||||
move_archive = old_archive_filename != instance.archive_filename
|
move_archive = old_archive_filename != instance.archive_filename
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -1697,7 +1697,7 @@ class TestCustomFieldFilenameUpdates(
|
|||||||
expected_filename = Path("updated/document.pdf")
|
expected_filename = Path("updated/document.pdf")
|
||||||
self.assertEqual(Path(self.doc.filename), expected_filename)
|
self.assertEqual(Path(self.doc.filename), expected_filename)
|
||||||
self.assertTrue(Path(self.doc.source_path).is_file())
|
self.assertTrue(Path(self.doc.source_path).is_file())
|
||||||
self.assertGreater(m.call_count, 0)
|
self.assertLessEqual(m.call_count, 1)
|
||||||
|
|
||||||
|
|
||||||
class TestPathDateLocalization:
|
class TestPathDateLocalization:
|
||||||
|
|||||||
Reference in New Issue
Block a user