Skip to content

Commit

Permalink
fix lint errors and allow passing a formatter to a field
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyjurjens committed Jun 4, 2024
1 parent 7bc8781 commit a02cce7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/django_es_kit/faceted_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class you can add them dynamically. This allows us to add facets from django for
4. Allow pagination on the search results (set_pagination or page & page_size arguments)
"""

doc_types = []
default_filter_queries = []

# pylint: disable=dangerous-default-value,too-many-arguments
Expand Down Expand Up @@ -77,13 +78,12 @@ def search(self):
Make sure to use the django-elasticsearch-dsl Search object, so you can call to_queryset on it.
Note: This only works if you have ONE doc_type in your FacetedSearch class.
"""
if not self.doc_types or len(self.doc_types) > 1:
if len(self.doc_types) > 1:
model = None
logger.warning(
"Your FacetedSearch class has no or multiple doc_types, this means you can NOT use the to_queryset method"
)
else:
# pylint: disable=unsubscriptable-object
model = self.doc_types[0].Django.model

s = Search(
Expand Down
18 changes: 11 additions & 7 deletions src/django_es_kit/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
class FacetField(forms.MultipleChoiceField):
widget = forms.CheckboxSelectMultiple

def __init__(self, es_field, field_type=None, **kwargs):
def __init__(self, es_field, field_type=None, formatter=None, **kwargs):
self.es_field = es_field
self.field_type = field_type
self.formatter = formatter
if "required" not in kwargs:
kwargs["required"] = False
super().__init__(**kwargs)
Expand Down Expand Up @@ -59,19 +60,20 @@ def get_es_filter_value(self, raw_value):

return raw_value

def process_facet_buckets(self, buckets):
def process_facet_buckets(self, request, buckets):
"""
This method processes the ES facet bucket from the response and updates the available choices.
A bucket look likes this: [(0, 11, False), (23, 8, False), (7, 6, False)]
"""
choices = []
for bucket in buckets:
key, doc_count, _ = bucket
choices.append((key, self.format_choice_label(key, doc_count)))
choices.append((key, self.format_choice_label(request, key, doc_count)))
self.choices = choices

def format_choice_label(self, key, doc_count):
# ToDo: Allow label formatters for displayable facet keys
def format_choice_label(self, request, key, doc_count):
if self.formatter:
return self.formatter(request, key, doc_count)
return f"{key} ({doc_count})"


Expand Down Expand Up @@ -117,7 +119,7 @@ def get_es_facet(self):
]
return RangeFacet(field=self.es_field, ranges=ranges)

def process_facet_buckets(self, buckets):
def process_facet_buckets(self, request, buckets):
"""
This method is overriden from the FacetField class to make use of
the potential RangeOption label. Also if the doc_count is 0,
Expand All @@ -129,7 +131,9 @@ def process_facet_buckets(self, buckets):
if doc_count > 0:
range_option = self.ranges.get(key)
label = range_option.get("label", key) if range_option else key
choices.append((key, self.format_choice_label(label, doc_count)))
choices.append(
(key, self.format_choice_label(request, label, doc_count))
)
self.choices = choices


Expand Down
3 changes: 2 additions & 1 deletion src/django_es_kit/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

@pytest.fixture(scope="class")
def elasticsearch_container():
container = ElasticSearchContainer(f"elasticsearch:8.13.4", mem_limit="3G")
container = ElasticSearchContainer("elasticsearch:8.13.4", mem_limit="3G")
container.start()
# Set python-elasticsearch-dsl host to the container
connections.configure(default={"hosts": container.get_url()})
Expand Down Expand Up @@ -73,6 +73,7 @@ class UsersFacetedSearchView(ESFacetedSearchView):


@pytest.mark.usefixtures("elasticsearch_container")
# pylint: disable=attribute-defined-outside-init
class TestESFacetedSearchView(TestCase):
def setUp(self):
self.factory = RequestFactory()
Expand Down
7 changes: 6 additions & 1 deletion src/django_es_kit/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def get_faceted_search(self):
return self._faceted_search

faceted_search_class = self.get_faceted_search_class()
# pylint: disable=not-callable
self._faceted_search = faceted_search_class(
facets=self.get_form().get_es_facets(), query=self.get_search_query()
)
Expand All @@ -66,9 +67,11 @@ def get_form(self):

form_class = self.get_form_class()
if self.request.GET:
# pylint: disable=not-callable
self._form = form_class(self.request.GET)
return self._form

# pylint: disable=not-callable
self._form = form_class()
return self._form

Expand Down Expand Up @@ -131,4 +134,6 @@ def reflect_es_response_to_form_fields(self, es_response, form):

for field in form.fields.values():
if isinstance(field, FacetField) and field.es_field in es_response.facets:
field.process_facet_buckets(es_response.facets[field.es_field])
field.process_facet_buckets(
self.request, es_response.facets[field.es_field]
)

0 comments on commit a02cce7

Please sign in to comment.