]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Adds the storage paths to the re-tagger command 1446/head
authorTrenton Holmes <holmes.trenton@gmail.com>
Mon, 22 Aug 2022 00:54:05 +0000 (17:54 -0700)
committerTrenton Holmes <holmes.trenton@gmail.com>
Mon, 22 Aug 2022 01:01:19 +0000 (18:01 -0700)
docs/administration.rst
src/documents/management/commands/document_retagger.py
src/documents/signals/handlers.py
src/documents/tests/test_management_retagger.py

index dfb88ff8086e629dec9fc14180b3cab85a0c9698..d8f41dd1607c7d6891d8fbb94e27b5474196ff3a 100644 (file)
@@ -310,6 +310,7 @@ there are tools for it.
     -c, --correspondent
     -T, --tags
     -t, --document_type
+    -s, --storage_path
     -i, --inbox-only
     --use-first
     -f, --overwrite
@@ -318,7 +319,7 @@ Run this after changing or adding matching rules. It'll loop over all
 of the documents in your database and attempt to match documents
 according to the new rules.
 
-Specify any combination of ``-c``, ``-T`` and ``-t`` to have the
+Specify any combination of ``-c``, ``-T``, ``-t`` and ``-s`` to have the
 retagger perform matching of the specified metadata type. If you don't
 specify any of these options, the document retagger won't do anything.
 
index 5ecf7f8ce32f61c9c2aa744aca6d67d415207082..c42357eb50b46579eabcae5adae5fa0914314171 100644 (file)
@@ -7,6 +7,7 @@ from documents.models import Document
 
 from ...signals.handlers import set_correspondent
 from ...signals.handlers import set_document_type
+from ...signals.handlers import set_storage_path
 from ...signals.handlers import set_tags
 
 
@@ -29,6 +30,7 @@ class Command(BaseCommand):
         parser.add_argument("-c", "--correspondent", default=False, action="store_true")
         parser.add_argument("-T", "--tags", default=False, action="store_true")
         parser.add_argument("-t", "--document_type", default=False, action="store_true")
+        parser.add_argument("-s", "--storage_path", default=False, action="store_true")
         parser.add_argument("-i", "--inbox-only", default=False, action="store_true")
         parser.add_argument(
             "--use-first",
@@ -112,3 +114,14 @@ class Command(BaseCommand):
                     base_url=options["base_url"],
                     color=color,
                 )
+            if options["storage_path"]:
+                set_storage_path(
+                    sender=None,
+                    document=document,
+                    classifier=classifier,
+                    replace=options["overwrite"],
+                    use_first=options["use_first"],
+                    suggest=options["suggest"],
+                    base_url=options["base_url"],
+                    color=color,
+                )
index cf6bcc0ae1475a7923e03469551e9bcfb8a7352d..2e7c2369ce6b59461f8227fd4209a4b3cf290824 100644 (file)
@@ -291,7 +291,7 @@ def set_storage_path(
                     )
                     + f" [{document.pk}]",
                 )
-            print(f"Sugest storage directory {selected}")
+            print(f"Suggest storage directory {selected}")
         else:
             logger.info(
                 f"Assigning storage path {selected} to {document}",
index c3146285503cb12086eb6d78ec23e8579cf1de00..2b7aae6494c5c83040ecfbe69840a8abff8d8a7c 100644 (file)
@@ -3,12 +3,34 @@ from django.test import TestCase
 from documents.models import Correspondent
 from documents.models import Document
 from documents.models import DocumentType
+from documents.models import StoragePath
 from documents.models import Tag
 from documents.tests.utils import DirectoriesMixin
 
 
 class TestRetagger(DirectoriesMixin, TestCase):
     def make_models(self):
+
+        self.sp1 = StoragePath.objects.create(
+            name="dummy a",
+            path="{created_data}/{title}",
+            match="auto document",
+            matching_algorithm=StoragePath.MATCH_LITERAL,
+        )
+        self.sp2 = StoragePath.objects.create(
+            name="dummy b",
+            path="{title}",
+            match="^first|^unrelated",
+            matching_algorithm=StoragePath.MATCH_REGEX,
+        )
+
+        self.sp3 = StoragePath.objects.create(
+            name="dummy c",
+            path="{title}",
+            match="^blah",
+            matching_algorithm=StoragePath.MATCH_REGEX,
+        )
+
         self.d1 = Document.objects.create(
             checksum="A",
             title="A",
@@ -23,6 +45,7 @@ class TestRetagger(DirectoriesMixin, TestCase):
             checksum="C",
             title="C",
             content="unrelated document",
+            storage_path=self.sp3,
         )
         self.d4 = Document.objects.create(
             checksum="D",
@@ -146,15 +169,15 @@ class TestRetagger(DirectoriesMixin, TestCase):
         call_command("document_retagger", "--document_type", "--suggest")
         d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()
 
-        self.assertEqual(d_first.document_type, None)
-        self.assertEqual(d_second.document_type, None)
+        self.assertIsNone(d_first.document_type)
+        self.assertIsNone(d_second.document_type)
 
     def test_add_correspondent_suggest(self):
         call_command("document_retagger", "--correspondent", "--suggest")
         d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()
 
-        self.assertEqual(d_first.correspondent, None)
-        self.assertEqual(d_second.correspondent, None)
+        self.assertIsNone(d_first.correspondent)
+        self.assertIsNone(d_second.correspondent)
 
     def test_add_tags_suggest_url(self):
         call_command(
@@ -178,8 +201,8 @@ class TestRetagger(DirectoriesMixin, TestCase):
         )
         d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()
 
-        self.assertEqual(d_first.document_type, None)
-        self.assertEqual(d_second.document_type, None)
+        self.assertIsNone(d_first.document_type)
+        self.assertIsNone(d_second.document_type)
 
     def test_add_correspondent_suggest_url(self):
         call_command(
@@ -190,5 +213,48 @@ class TestRetagger(DirectoriesMixin, TestCase):
         )
         d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()
 
-        self.assertEqual(d_first.correspondent, None)
-        self.assertEqual(d_second.correspondent, None)
+        self.assertIsNone(d_first.correspondent)
+        self.assertIsNone(d_second.correspondent)
+
+    def test_add_storage_path(self):
+        """
+        GIVEN:
+            - 2 storage paths with documents which match them
+            - 1 document which matches but has a storage path
+        WHEN:
+            - document retagger is called
+        THEN:
+            - Matching document's storage paths updated
+            - Non-matching documents have no storage path
+            - Existing storage patch left unchanged
+        """
+        call_command(
+            "document_retagger",
+            "--storage_path",
+        )
+        d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()
+
+        self.assertEqual(d_first.storage_path, self.sp2)
+        self.assertEqual(d_auto.storage_path, self.sp1)
+        self.assertIsNone(d_second.storage_path)
+        self.assertEqual(d_unrelated.storage_path, self.sp3)
+
+    def test_overwrite_storage_path(self):
+        """
+        GIVEN:
+            - 2 storage paths with documents which match them
+            - 1 document which matches but has a storage path
+        WHEN:
+            - document retagger is called with overwrite
+        THEN:
+            - Matching document's storage paths updated
+            - Non-matching documents have no storage path
+            - Existing storage patch overwritten
+        """
+        call_command("document_retagger", "--storage_path", "--overwrite")
+        d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()
+
+        self.assertEqual(d_first.storage_path, self.sp2)
+        self.assertEqual(d_auto.storage_path, self.sp1)
+        self.assertIsNone(d_second.storage_path)
+        self.assertEqual(d_unrelated.storage_path, self.sp2)