From 5fca8e4963f239bebb67dcfc88d9f246a5d95ca1 Mon Sep 17 00:00:00 2001 From: Ben Silverman Date: Thu, 10 Oct 2024 14:03:31 -0400 Subject: [PATCH] Fix choice field init and complete test coverage --- geniza/corpus/forms.py | 27 ++++++++++++------------ geniza/corpus/tests/test_corpus_views.py | 1 + geniza/corpus/tests/test_forms.py | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/geniza/corpus/forms.py b/geniza/corpus/forms.py index 5bcef5f0c..248431dc9 100644 --- a/geniza/corpus/forms.py +++ b/geniza/corpus/forms.py @@ -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 @@ -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. diff --git a/geniza/corpus/tests/test_corpus_views.py b/geniza/corpus/tests/test_corpus_views.py index 62913f0e8..cec5ae8ca 100644 --- a/geniza/corpus/tests/test_corpus_views.py +++ b/geniza/corpus/tests/test_corpus_views.py @@ -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 diff --git a/geniza/corpus/tests/test_forms.py b/geniza/corpus/tests/test_forms.py index 3c87784b5..9a08f9db9 100644 --- a/geniza/corpus/tests/test_forms.py +++ b/geniza/corpus/tests/test_forms.py @@ -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