From cf5ac596ed450d339a8337c4db742f2f2867ad4b Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Wed, 19 Nov 2025 07:21:33 -0800 Subject: [PATCH] Performance: make move files after select custom field change async (#11391) --- src/documents/signals/handlers.py | 3 ++- src/documents/tests/test_api_custom_fields.py | 2 ++ src/documents/tests/test_file_handling.py | 6 +++--- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py index bce376f76..0e53c9357 100644 --- a/src/documents/signals/handlers.py +++ b/src/documents/signals/handlers.py @@ -396,6 +396,7 @@ class CannotMoveFilesException(Exception): @receiver(models.signals.post_save, sender=CustomFieldInstance, weak=False) @receiver(models.signals.m2m_changed, sender=Document.tags.through, weak=False) @receiver(models.signals.post_save, sender=Document, weak=False) +@shared_task def update_filename_and_move_files( sender, instance: Document | CustomFieldInstance, @@ -559,7 +560,7 @@ def check_paths_and_prune_custom_fields(sender, instance: CustomField, **kwargs) cf_instance.save(update_fields=["value_select"]) # Update the filename and move files if necessary - update_filename_and_move_files(sender, cf_instance) + update_filename_and_move_files.delay(sender, cf_instance) @receiver(models.signals.post_delete, sender=CustomField) diff --git a/src/documents/tests/test_api_custom_fields.py b/src/documents/tests/test_api_custom_fields.py index 31dd14b88..b6e6c1342 100644 --- a/src/documents/tests/test_api_custom_fields.py +++ b/src/documents/tests/test_api_custom_fields.py @@ -4,6 +4,7 @@ from unittest.mock import ANY from django.contrib.auth.models import Permission from django.contrib.auth.models import User +from django.test import override_settings from rest_framework import status from rest_framework.test import APITestCase @@ -211,6 +212,7 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase): ], ) + @override_settings(CELERY_TASK_ALWAYS_EAGER=True) def test_custom_field_select_options_pruned(self): """ GIVEN: diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py index c0070aa81..117a964ba 100644 --- a/src/documents/tests/test_file_handling.py +++ b/src/documents/tests/test_file_handling.py @@ -569,7 +569,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase): self.assertEqual(generate_filename(doc), Path("document_apple.pdf")) # handler should not have been called - self.assertEqual(m.call_count, 0) + self.assertEqual(m.delay.call_count, 0) cf.extra_data = { "select_options": [ {"label": "aubergine", "id": "abc123"}, @@ -579,8 +579,8 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase): } cf.save() self.assertEqual(generate_filename(doc), Path("document_aubergine.pdf")) - # handler should have been called - self.assertEqual(m.call_count, 1) + # handler should have been called via delay + self.assertEqual(m.delay.call_count, 1) class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, TestCase):