]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Refresh the instance from the database before doing any file handling with it
authorTrenton H <holmes.trenton@gmail.com>
Mon, 31 Oct 2022 22:30:56 +0000 (15:30 -0700)
committerTrenton H <797416+stumpylog@users.noreply.github.com>
Thu, 3 Nov 2022 18:32:27 +0000 (11:32 -0700)
src/documents/consumer.py
src/documents/signals/handlers.py
src/documents/tests/test_consumer.py

index 609ebed8324d28157dc2bbd48b14e13d0bf9465c..f542a1d98ab36aba74eeefbafc0f9cce59dfb6aa 100644 (file)
@@ -405,6 +405,7 @@ class Consumer(LoggingMixin):
 
                 # Don't save with the lock active. Saving will cause the file
                 # renaming logic to acquire the lock as well.
+                # This triggers things like file renaming
                 document.save()
 
                 # Delete the file only if it was successfully consumed
@@ -438,6 +439,9 @@ class Consumer(LoggingMixin):
 
         self._send_progress(100, 100, "SUCCESS", MESSAGE_FINISHED, document.id)
 
+        # Return the most up to date fields
+        document.refresh_from_db()
+
         return document
 
     def _store(self, text, date, mime_type) -> Document:
index 1b180626a4f8d7cccf57cea8c886e29581efff92..865c83935e9055393b6ac54aedcc2218b647fa17 100644 (file)
@@ -400,6 +400,13 @@ def update_filename_and_move_files(sender, instance, **kwargs):
 
     with FileLock(settings.MEDIA_LOCK):
         try:
+
+            # If this was waiting for the lock, the filename or archive_filename
+            # of this document may have been updated.  This happens if multiple updates
+            # get queued from the UI for the same document
+            # So freshen up the data before doing anything
+            instance.refresh_from_db()
+
             old_filename = instance.filename
             old_source_path = instance.source_path
 
index 48f19590371b047c869811ba62951b355bb9535c..3b94b889b85db91a06c9ec4d1af3708ea4a37cdd 100644 (file)
@@ -14,6 +14,7 @@ except ImportError:
     import backports.zoneinfo as zoneinfo
 
 from django.conf import settings
+from django.utils import timezone
 from django.test import override_settings
 from django.test import TestCase
 
@@ -326,6 +327,12 @@ class TestConsumer(DirectoriesMixin, TestCase):
     def testNormalOperation(self):
 
         filename = self.get_test_file()
+
+        # Get the local time, as an aware datetime
+        # Roughly equal to file modification time
+        rough_create_date_local = timezone.localtime(timezone.now())
+
+        # Consume the file
         document = self.consumer.try_consume_file(filename)
 
         self.assertEqual(document.content, "The Text")
@@ -351,7 +358,20 @@ class TestConsumer(DirectoriesMixin, TestCase):
 
         self._assert_first_last_send_progress()
 
-        self.assertEqual(document.created.tzinfo, zoneinfo.ZoneInfo("America/Chicago"))
+        # Convert UTC time from DB to local time
+        document_date_local = timezone.localtime(document.created)
+
+        self.assertEqual(
+            document_date_local.tzinfo,
+            zoneinfo.ZoneInfo("America/Chicago"),
+        )
+        self.assertEqual(document_date_local.tzinfo, rough_create_date_local.tzinfo)
+        self.assertEqual(document_date_local.year, rough_create_date_local.year)
+        self.assertEqual(document_date_local.month, rough_create_date_local.month)
+        self.assertEqual(document_date_local.day, rough_create_date_local.day)
+        self.assertEqual(document_date_local.hour, rough_create_date_local.hour)
+        self.assertEqual(document_date_local.minute, rough_create_date_local.minute)
+        # Skipping seconds and more precise
 
     @override_settings(FILENAME_FORMAT=None)
     def testDeleteMacFiles(self):