Update handlers.py

This commit is contained in:
shamoon
2026-01-25 22:46:25 -08:00
parent 444ff6951e
commit b36cfab43e

View File

@@ -19,6 +19,7 @@ from django.db import DatabaseError
from django.db import close_old_connections from django.db import close_old_connections
from django.db import connections from django.db import connections
from django.db import models from django.db import models
from django.db import transaction
from django.db.models import Q from django.db.models import Q
from django.dispatch import receiver from django.dispatch import receiver
from django.utils import timezone from django.utils import timezone
@@ -453,8 +454,17 @@ def update_filename_and_move_files(
# This will in turn cause this logic to move the file where it belongs. # This will in turn cause this logic to move the file where it belongs.
return return
with FileLock(settings.MEDIA_LOCK): move_original = False
move_archive = False
old_filename = None
old_archive_filename = None
old_source_path = None
old_archive_path = None
try: try:
with transaction.atomic():
Document.global_objects.select_for_update().get(pk=instance.pk)
with FileLock(settings.MEDIA_LOCK):
# If this was waiting for the lock, the filename or archive_filename # If this was waiting for the lock, the filename or archive_filename
# of this document may have been updated. This happens if multiple updates # of this document may have been updated. This happens if multiple updates
# get queued from the UI for the same document # get queued from the UI for the same document
@@ -487,7 +497,10 @@ def update_filename_and_move_files(
old_archive_path = instance.archive_path old_archive_path = instance.archive_path
if instance.has_archive_version: if instance.has_archive_version:
archive_candidate = generate_filename(instance, archive_filename=True) archive_candidate = generate_filename(
instance,
archive_filename=True,
)
archive_candidate_path = ( archive_candidate_path = (
settings.ARCHIVE_DIR / archive_candidate settings.ARCHIVE_DIR / archive_candidate
).resolve() ).resolve()
@@ -510,13 +523,6 @@ def update_filename_and_move_files(
else: else:
move_archive = False move_archive = False
if not move_original and not move_archive:
# Just update modified. Also, don't save() here to prevent infinite recursion.
Document.objects.filter(pk=instance.pk).update(
modified=timezone.now(),
)
return
if move_original: if move_original:
validate_move( validate_move(
instance, instance,
@@ -537,6 +543,13 @@ def update_filename_and_move_files(
create_source_path_directory(instance.archive_path) create_source_path_directory(instance.archive_path)
shutil.move(old_archive_path, instance.archive_path) shutil.move(old_archive_path, instance.archive_path)
if not move_original and not move_archive:
# Just update modified. Also, don't save() here to prevent infinite recursion.
Document.objects.filter(pk=instance.pk).update(
modified=timezone.now(),
)
return
# Don't save() here to prevent infinite recursion. # Don't save() here to prevent infinite recursion.
Document.global_objects.filter(pk=instance.pk).update( Document.global_objects.filter(pk=instance.pk).update(
filename=instance.filename, filename=instance.filename,
@@ -553,8 +566,10 @@ def update_filename_and_move_files(
# - saving to the database failed due to database errors # - saving to the database failed due to database errors
# In both cases, we need to revert to the original state. # In both cases, we need to revert to the original state.
if move_original or move_archive:
# Try to move files to their original location. # Try to move files to their original location.
try: try:
with FileLock(settings.MEDIA_LOCK):
if move_original and instance.source_path.is_file(): if move_original and instance.source_path.is_file():
logger.info("Restoring previous original path") logger.info("Restoring previous original path")
shutil.move(instance.source_path, old_source_path) shutil.move(instance.source_path, old_source_path)
@@ -575,18 +590,24 @@ def update_filename_and_move_files(
pass pass
# restore old values on the instance # restore old values on the instance
if old_filename is not None:
instance.filename = old_filename instance.filename = old_filename
if old_archive_filename is not None:
instance.archive_filename = old_archive_filename instance.archive_filename = old_archive_filename
# finally, remove any empty sub folders. This will do nothing if # finally, remove any empty sub folders. This will do nothing if
# something has failed above. # something has failed above.
if not old_source_path.is_file(): if old_source_path and not old_source_path.is_file():
delete_empty_directories( delete_empty_directories(
Path(old_source_path).parent, Path(old_source_path).parent,
root=settings.ORIGINALS_DIR, root=settings.ORIGINALS_DIR,
) )
if instance.has_archive_version and not old_archive_path.is_file(): if (
instance.has_archive_version
and old_archive_path
and not old_archive_path.is_file()
):
delete_empty_directories( delete_empty_directories(
Path(old_archive_path).parent, Path(old_archive_path).parent,
root=settings.ARCHIVE_DIR, root=settings.ARCHIVE_DIR,