]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Fix: Handling of Nones when using custom fields in filepath templating (#7933)
authorTrenton H <797416+stumpylog@users.noreply.github.com>
Tue, 15 Oct 2024 17:54:15 +0000 (10:54 -0700)
committerGitHub <noreply@github.com>
Tue, 15 Oct 2024 17:54:15 +0000 (17:54 +0000)
src/documents/templating/filepath.py
src/documents/tests/test_file_handling.py

index 54ceb30a8168c0d320bfc3ab4746fb74260b8905..afb43ff4d1c73e6f6da8e6e64d8a7fd11e2595f7 100644 (file)
@@ -82,7 +82,7 @@ def get_cf_value(
     name: str,
     default: str | None = None,
 ) -> str | None:
-    if name in custom_field_data:
+    if name in custom_field_data and custom_field_data[name]["value"] is not None:
         return custom_field_data[name]["value"]
     elif default is not None:
         return default
@@ -235,8 +235,10 @@ def get_custom_fields_context(
             field_instance.field.data_type,
             replacement_text="-",
         )
+        if field_instance.value is None:
+            value = None
         # String types need to be sanitized
-        if field_instance.field.data_type in {
+        elif field_instance.field.data_type in {
             CustomField.FieldDataType.MONETARY,
             CustomField.FieldDataType.STRING,
             CustomField.FieldDataType.URL,
index 3a0700e1ca9d914b5fcfa4585a4eb6ea1bc441be..fbe945ae07341112dac68e38257e57121dafaa47 100644 (file)
@@ -1322,7 +1322,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
             extra_data={"select_options": ["ChoiceOne", "ChoiceTwo"]},
         )
 
-        CustomFieldInstance.objects.create(
+        cfi1 = CustomFieldInstance.objects.create(
             document=doc_a,
             field=cf2,
             value_select=0,
@@ -1351,7 +1351,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
         with override_settings(
             FILENAME_FORMAT="""
                  {% if "Select Field" in custom_fields %}
-                   {{ title }}_{{ custom_fields | get_cf_value('Select Field') }}
+                   {{ title }}_{{ custom_fields | get_cf_value('Select Field', 'Default Value') }}
                  {% else %}
                    {{ title }}
                  {% endif %}
@@ -1362,6 +1362,15 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
                 "Some Title_ChoiceOne.pdf",
             )
 
+            # Check for handling Nones well
+            cfi1.value_select = None
+            cfi1.save()
+
+            self.assertEqual(
+                generate_filename(doc_a),
+                "Some Title_Default Value.pdf",
+            )
+
         cf.name = "Invoice Number"
         cfi.value_int = 4567
         cfi.save()