Skip to content

Commit

Permalink
Fix choice field init and complete test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
blms committed Oct 10, 2024
1 parent da07d3e commit 5fca8e4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
27 changes: 14 additions & 13 deletions geniza/corpus/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,8 @@ class FacetFieldMixin:
def __init__(self, *args, **kwargs):
if "required" not in kwargs:
kwargs["required"] = False

# get custom kwarg and remove before passing to MultipleChoiceField
# super method, which would cause an error
if hasattr(self.widget, "legend"):
self.widget.legend = None
if "legend" in kwargs:
self.widget.legend = kwargs["legend"]
del kwargs["legend"]

super().__init__(*args, **kwargs)

# if no custom legend, set it from label
if hasattr(self.widget, "legend") and not self.widget.legend:
self.widget.legend = self.label

def valid_value(self, value):
return True

Expand All @@ -113,6 +100,20 @@ class FacetChoiceField(FacetFieldMixin, forms.ChoiceField):
# use a custom widget so we can add facet count as a data attribute
widget = CheckboxSelectWithCount

def __init__(self, *args, **kwargs):
# get custom kwarg and remove before passing to MultipleChoiceField
# super method, which would cause an error
self.widget.legend = None
if "legend" in kwargs:
self.widget.legend = kwargs["legend"]
del kwargs["legend"]

super().__init__(*args, **kwargs)

# if no custom legend, set it from label
if not self.widget.legend:
self.widget.legend = self.label

def populate_from_facets(self, facet_dict):
"""
Populate the field choices from the facets returned by solr.
Expand Down
1 change: 1 addition & 0 deletions geniza/corpus/tests/test_corpus_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ def test_get_queryset(self, mock_solr_queryset):
"has_discussion": "on",
"has_translation": "on",
"has_image": "on",
"translation_language": "English",
}
qs = docsearch_view.get_queryset()
mock_sqs = mock_queryset_cls.return_value
Expand Down
2 changes: 1 addition & 1 deletion geniza/corpus/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TestFacetChoiceField:
# test adapted from ppa-django

def test_init(self):
fcf = FacetChoiceField()
fcf = FacetChoiceField(legend="Document type")
# uses RadioSelectWithCount
fcf.widget == CheckboxSelectWithCount
# not required by default
Expand Down

0 comments on commit 5fca8e4

Please sign in to comment.