Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Replace usages of os.rename with shutil.move to properly handle cases where the source and dest arent't on the same filesystem

Trenton Holmesb7577038a08a
diff --git a/src/documents/management/commands/document_thumbnails.py b/src/documents/management/commands/document_thumbnails.py
index 0f35dba42..982634c5e 100644
--- a/src/documents/management/commands/document_thumbnails.py
+++ b/src/documents/management/commands/document_thumbnails.py
@@ -10,8 +10,8 @@ from documents.models import Document
from documents.parsers import get_parser_class_for_mime_type
-def _process_document(doc_in):
- document: Document = Document.objects.get(id=doc_in)
+def _process_document(doc_id):
+ document: Document = Document.objects.get(id=doc_id)
parser_class = get_parser_class_for_mime_type(document.mime_type)
if parser_class:
diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py
index b41712a6f..40765443b 100644
--- a/src/documents/signals/handlers.py
+++ b/src/documents/signals/handlers.py
@@ -429,12 +429,12 @@ def update_filename_and_move_files(sender, instance: Document, **kwargs):
if move_original:
validate_move(instance, old_source_path, instance.source_path)
create_source_path_directory(instance.source_path)
- os.rename(old_source_path, instance.source_path)
+ shutil.move(old_source_path, instance.source_path)
if move_archive:
validate_move(instance, old_archive_path, instance.archive_path)
create_source_path_directory(instance.archive_path)
- os.rename(old_archive_path, instance.archive_path)
+ shutil.move(old_archive_path, instance.archive_path)
# Don't save() here to prevent infinite recursion.
Document.objects.filter(pk=instance.pk).update(
@@ -453,11 +453,11 @@ def update_filename_and_move_files(sender, instance: Document, **kwargs):
try:
if move_original and os.path.isfile(instance.source_path):
logger.info("Restoring previous original path")
- os.rename(instance.source_path, old_source_path)
+ shutil.move(instance.source_path, old_source_path)
if move_archive and os.path.isfile(instance.archive_path):
logger.info("Restoring previous archive path")
- os.rename(instance.archive_path, old_archive_path)
+ shutil.move(instance.archive_path, old_archive_path)
except Exception:
# This is fine, since:
diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py
index e4d4d0673..b021f8aaf 100644
--- a/src/documents/tests/test_file_handling.py
+++ b/src/documents/tests/test_file_handling.py
@@ -697,7 +697,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
self.assertIsFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
- @mock.patch("documents.signals.handlers.os.rename")
+ @mock.patch("documents.signals.handlers.shutil.move")
def test_move_archive_error(self, m):
def fake_rename(src, dst):
if "archive" in str(src):
@@ -748,7 +748,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
self.assertIsFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
- @mock.patch("documents.signals.handlers.os.rename")
+ @mock.patch("documents.signals.handlers.shutil.move")
def test_move_file_error(self, m):
def fake_rename(src, dst):
if "original" in str(src):
diff --git a/src/documents/tests/test_management_thumbnails.py b/src/documents/tests/test_management_thumbnails.py
index 6e4e18ea3..1b9d9f664 100644
--- a/src/documents/tests/test_management_thumbnails.py
+++ b/src/documents/tests/test_management_thumbnails.py
@@ -47,12 +47,16 @@ class TestMakeThumbnails(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertIsFile(self.d1.thumbnail_path)
@mock.patch("documents.management.commands.document_thumbnails.shutil.move")
- def test_process_document_invalid_mime_type(self, m):
+ def test_process_document_invalid_mime_type(self, m: mock.Mock):
self.d1.mime_type = "asdasdasd"
self.d1.save()
+ # .save() triggers filename handling
+ m.reset_mock()
+
_process_document(self.d1.id)
+ # Not called during processing of document
m.assert_not_called()
def test_command(self):