From 0af1a32c482bd02ae325447235033a060f1b2b51 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 3 Oct 2024 13:43:17 -0700 Subject: [PATCH] Return select field option text not ID --- src/documents/file_handling.py | 9 ++++++++ src/documents/tests/test_file_handling.py | 26 +++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/documents/file_handling.py b/src/documents/file_handling.py index 1c3bbe69d..bbe91081f 100644 --- a/src/documents/file_handling.py +++ b/src/documents/file_handling.py @@ -261,6 +261,15 @@ def get_custom_fields_context( field_instance.value, replacement_text="-", ) + elif ( + field_instance.field.data_type == CustomField.FieldDataType.SELECT + and field_instance.field.extra_data["select_options"] is not None + ): + options = field_instance.field.extra_data["select_options"] + value = pathvalidate.sanitize_filename( + options[int(field_instance.value)], + replacement_text="-", + ) else: value = field_instance.value field_data["custom_fields"][ diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py index 5bb593c0e..a497dab40 100644 --- a/src/documents/tests/test_file_handling.py +++ b/src/documents/tests/test_file_handling.py @@ -1266,6 +1266,18 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase): value_int=1234, ) + cf2 = CustomField.objects.create( + name="Select Field", + data_type=CustomField.FieldDataType.SELECT, + extra_data={"select_options": ["ChoiceOne", "ChoiceTwo"]}, + ) + + CustomFieldInstance.objects.create( + document=doc_a, + field=cf2, + value_select=0, + ) + with override_settings( FILENAME_FORMAT=""" {% if "Invoice" in custom_fields %} @@ -1280,6 +1292,20 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase): "invoices/1234.pdf", ) + with override_settings( + FILENAME_FORMAT=""" + {% if "Select Field" in custom_fields %} + {{ title }}_{{ custom_fields|get_cf_value:'Select Field' }} + {% else %} + {{ title }} + {% endif %} + """, + ): + self.assertEqual( + generate_filename(doc_a), + "Some Title_ChoiceOne.pdf", + ) + cf.name = "Invoice Number" cfi.value_int = 4567 cfi.save()