Fix filtering

This commit is contained in:
shamoon 2024-11-12 23:12:49 -08:00
parent 5be432d135
commit ef27ab104e
No known key found for this signature in database
4 changed files with 33 additions and 21 deletions

View File

@ -44,6 +44,8 @@
<ng-select #fieldSelects <ng-select #fieldSelects
class="paperless-input-select rounded-end" class="paperless-input-select rounded-end"
[items]="getSelectOptionsForField(atom.field)" [items]="getSelectOptionsForField(atom.field)"
bindLabel="label"
bindValue="id"
[(ngModel)]="atom.value" [(ngModel)]="atom.value"
[disabled]="disabled" [disabled]="disabled"
(mousedown)="$event.stopImmediatePropagation()" (mousedown)="$event.stopImmediatePropagation()"
@ -99,6 +101,8 @@
<ng-select <ng-select
class="paperless-input-select rounded-end" class="paperless-input-select rounded-end"
[items]="getSelectOptionsForField(atom.field)" [items]="getSelectOptionsForField(atom.field)"
bindLabel="label"
bindValue="id"
[(ngModel)]="atom.value" [(ngModel)]="atom.value"
[disabled]="disabled" [disabled]="disabled"
[multiple]="true" [multiple]="true"

View File

@ -22,8 +22,8 @@
<div formArrayName="select_options"> <div formArrayName="select_options">
@for (option of objectForm.controls.extra_data.controls.select_options.controls; track option; let i = $index) { @for (option of objectForm.controls.extra_data.controls.select_options.controls; track option; let i = $index) {
<div class="input-group input-group-sm my-2" [formGroup]="objectForm.controls.extra_data.controls.select_options.controls[i]"> <div class="input-group input-group-sm my-2" [formGroup]="objectForm.controls.extra_data.controls.select_options.controls[i]">
<input type="hidden" formControlName="id">
<input #selectOption type="text" class="form-control" formControlName="label" autocomplete="off"> <input #selectOption type="text" class="form-control" formControlName="label" autocomplete="off">
<input type="hidden" formControlName="id">
<button type="button" class="btn btn-outline-danger" (click)="removeSelectOption(i)" i18n>Delete</button> <button type="button" class="btn btn-outline-danger" (click)="removeSelectOption(i)" i18n>Delete</button>
</div> </div>
} }

View File

@ -176,9 +176,9 @@ class CustomFieldsFilter(Filter):
if fields_with_matching_selects.count() > 0: if fields_with_matching_selects.count() > 0:
for field in fields_with_matching_selects: for field in fields_with_matching_selects:
options = field.extra_data.get("select_options", []) options = field.extra_data.get("select_options", [])
for index, option in enumerate(options): for _, option in enumerate(options):
if option.lower().find(value.lower()) != -1: if option.get("label").lower().find(value.lower()) != -1:
option_ids.extend([index]) option_ids.extend([option.get("id")])
return ( return (
qs.filter(custom_fields__field__name__icontains=value) qs.filter(custom_fields__field__name__icontains=value)
| qs.filter(custom_fields__value_text__icontains=value) | qs.filter(custom_fields__value_text__icontains=value)
@ -195,19 +195,21 @@ class CustomFieldsFilter(Filter):
return qs return qs
class SelectField(serializers.IntegerField): class SelectField(serializers.CharField):
def __init__(self, custom_field: CustomField): def __init__(self, custom_field: CustomField):
self._options = custom_field.extra_data["select_options"] 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): def to_internal_value(self, data):
if not isinstance(data, int): # Test if the supplied value is not an id
# If the supplied value is not an integer, try:
# we will try to map it to an option index. data = next(
try: option.get("id")
data = self._options.index(data) for option in self._options
except ValueError: if option.get("label") == data
pass )
except StopIteration:
pass
return super().to_internal_value(data) return super().to_internal_value(data)

View File

@ -46,7 +46,13 @@ class TestCustomFieldsSearch(DirectoriesMixin, APITestCase):
# Add some options to the select_field # Add some options to the select_field
select = self.custom_fields["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() select.save()
# Now we will create some test documents # Now we will create some test documents
@ -122,9 +128,9 @@ class TestCustomFieldsSearch(DirectoriesMixin, APITestCase):
# CustomField.FieldDataType.SELECT # CustomField.FieldDataType.SELECT
self._create_document(select_field=None) self._create_document(select_field=None)
self._create_document(select_field=0) self._create_document(select_field="abc-123")
self._create_document(select_field=1) self._create_document(select_field="def-456")
self._create_document(select_field=2) self._create_document(select_field="ghi-789")
def _create_document(self, **kwargs): def _create_document(self, **kwargs):
title = str(kwargs) title = str(kwargs)
@ -296,18 +302,18 @@ class TestCustomFieldsSearch(DirectoriesMixin, APITestCase):
) )
def test_select(self): 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. # or the name of the option. They function exactly the same.
self._assert_query_match_predicate( self._assert_query_match_predicate(
["select_field", "exact", 1], ["select_field", "exact", "def-456"],
lambda document: "select_field" in document lambda document: "select_field" in document
and document["select_field"] == 1, and document["select_field"] == "def-456",
) )
# This is the same as: # This is the same as:
self._assert_query_match_predicate( self._assert_query_match_predicate(
["select_field", "exact", "B"], ["select_field", "exact", "B"],
lambda document: "select_field" in document lambda document: "select_field" in document
and document["select_field"] == 1, and document["select_field"] == "def-456",
) )
# ==========================================================# # ==========================================================#