diff --git a/README.md b/README.md index e71e5b8..eec8169 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/django_es_kit/forms.py b/src/django_es_kit/forms.py index 62d1ef2..1f1ffb4 100644 --- a/src/django_es_kit/forms.py +++ b/src/django_es_kit/forms.py @@ -1,6 +1,6 @@ from django import forms -from .fields import FacetField +from .fields import FacetField, FilterField, SortField class FacetedSearchForm(forms.Form): @@ -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)) + ]