From ef27ab104e35211740402fc6473c94a0007f0392 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Tue, 12 Nov 2024 23:12:49 -0800 Subject: [PATCH] Fix filtering --- ...ustom-fields-query-dropdown.component.html | 4 +++ .../custom-field-edit-dialog.component.html | 2 +- src/documents/filters.py | 26 ++++++++++--------- .../tests/test_api_filter_by_custom_fields.py | 22 ++++++++++------ 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.html b/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.html index 9cc095d7d..768a79af5 100644 --- a/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.html +++ b/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.html @@ -44,6 +44,8 @@ @for (option of objectForm.controls.extra_data.controls.select_options.controls; track option; let i = $index) {
- +
} diff --git a/src/documents/filters.py b/src/documents/filters.py index e8065c472..246f1ee35 100644 --- a/src/documents/filters.py +++ b/src/documents/filters.py @@ -176,9 +176,9 @@ class CustomFieldsFilter(Filter): if fields_with_matching_selects.count() > 0: for field in fields_with_matching_selects: options = field.extra_data.get("select_options", []) - for index, option in enumerate(options): - if option.lower().find(value.lower()) != -1: - option_ids.extend([index]) + for _, option in enumerate(options): + if option.get("label").lower().find(value.lower()) != -1: + option_ids.extend([option.get("id")]) return ( qs.filter(custom_fields__field__name__icontains=value) | qs.filter(custom_fields__value_text__icontains=value) @@ -195,19 +195,21 @@ class CustomFieldsFilter(Filter): return qs -class SelectField(serializers.IntegerField): +class SelectField(serializers.CharField): def __init__(self, custom_field: CustomField): self._options = custom_field.extra_data["select_options"] - super().__init__(min_value=0, max_value=len(self._options)) + super().__init__(max_length=128) def to_internal_value(self, data): - if not isinstance(data, int): - # If the supplied value is not an integer, - # we will try to map it to an option index. - try: - data = self._options.index(data) - except ValueError: - pass + # Test if the supplied value is not an id + try: + data = next( + option.get("id") + for option in self._options + if option.get("label") == data + ) + except StopIteration: + pass return super().to_internal_value(data) diff --git a/src/documents/tests/test_api_filter_by_custom_fields.py b/src/documents/tests/test_api_filter_by_custom_fields.py index 4cba29152..d0d95d0c9 100644 --- a/src/documents/tests/test_api_filter_by_custom_fields.py +++ b/src/documents/tests/test_api_filter_by_custom_fields.py @@ -46,7 +46,13 @@ class TestCustomFieldsSearch(DirectoriesMixin, APITestCase): # Add some options to the select_field select = self.custom_fields["select_field"] - select.extra_data = {"select_options": ["A", "B", "C"]} + select.extra_data = { + "select_options": [ + {"label": "A", "id": "abc-123"}, + {"label": "B", "id": "def-456"}, + {"label": "C", "id": "ghi-789"}, + ], + } select.save() # Now we will create some test documents @@ -122,9 +128,9 @@ class TestCustomFieldsSearch(DirectoriesMixin, APITestCase): # CustomField.FieldDataType.SELECT self._create_document(select_field=None) - self._create_document(select_field=0) - self._create_document(select_field=1) - self._create_document(select_field=2) + self._create_document(select_field="abc-123") + self._create_document(select_field="def-456") + self._create_document(select_field="ghi-789") def _create_document(self, **kwargs): title = str(kwargs) @@ -296,18 +302,18 @@ class TestCustomFieldsSearch(DirectoriesMixin, APITestCase): ) def test_select(self): - # For select fields, you can either specify the index + # For select fields, you can either specify the id of the option # or the name of the option. They function exactly the same. self._assert_query_match_predicate( - ["select_field", "exact", 1], + ["select_field", "exact", "def-456"], lambda document: "select_field" in document - and document["select_field"] == 1, + and document["select_field"] == "def-456", ) # This is the same as: self._assert_query_match_predicate( ["select_field", "exact", "B"], lambda document: "select_field" in document - and document["select_field"] == 1, + and document["select_field"] == "def-456", ) # ==========================================================#