Skip to content

Commit

Permalink
Add some helper methods to the FacetForm for grouping type of fields …
Browse files Browse the repository at this point in the history
…together.
  • Loading branch information
joeyjurjens committed Jun 6, 2024
1 parent 6adf889 commit 26f49a0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ As you can see, the implementation is quite straight forward, you define the doc

#### FacetedSearchForm

The FacetedSearchForm is a very simple subclass of Django's default Form class. It has a method `get_es_facets` that returns all `FacetField` on the form, which is then later used within the `ESFacetedSearchView` to populate the available choices for each `FacetField`
The FacetedSearchForm is a very simple subclass of Django's default Form class. It has a method `get_es_facets` that returns a dictionary of facets which can be passed to the `DynamicFacetedSearch` class. It also has some helper methods that you can use to render your form: `get_facet_fields` `get_filter_fields` `get_sort_fields` & `get_regular_fields`, the names speaks for themselves. Those methods could be useful, as most likely you'll render those types of fields in groups.

#### FacetField, TermsFacetField, RangeFacetField, FilterField & SortField

Expand Down
46 changes: 45 additions & 1 deletion src/django_es_kit/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django import forms

from .fields import FacetField
from .fields import FacetField, FilterField, SortField


class FacetedSearchForm(forms.Form):
Expand Down Expand Up @@ -38,3 +38,47 @@ def get_es_facets(self):
if isinstance(field, FacetField):
form_facets[field.es_field] = field.get_es_facet()
return form_facets

def get_facet_fields(self):
"""
Retrieve all facet fields from the form.
Returns:
list: A list of `FacetField` instances.
"""
return [
field for field in self.fields.values() if isinstance(field, FacetField)
]

def get_filter_fields(self):
"""
Retrieve all filter fields from the form.
Returns:
list: A list of `FilterField` instances.
"""
return [
field for field in self.fields.values() if isinstance(field, FilterField)
]

def get_sort_fields(self):
"""
Retrieve all sort fields from the form.
Returns:
list: A list of `SortField` instances.
"""
return [field for field in self.fields.values() if isinstance(field, SortField)]

def get_regular_fields(self):
"""
Retrieve all regular fields from the form.
Returns:
list: A list of regular form fields.
"""
return [
field
for field in self.fields.values()
if not isinstance(field, (FacetField, FilterField, SortField))
]

0 comments on commit 26f49a0

Please sign in to comment.