Tweak how an "enum" grid filter is initialized

wasn't working quite right for Buefy theme
This commit is contained in:
Lance Edgar 2020-11-25 18:49:02 -06:00
parent 3cd5fa7f4a
commit c87a452471

View file

@ -161,7 +161,7 @@ class GridFilter(object):
if value_renderer: if value_renderer:
self.set_value_renderer(value_renderer) self.set_value_renderer(value_renderer)
elif value_enum: elif value_enum:
self.set_value_renderer(EnumValueRenderer(value_enum)) self.set_choices(value_enum)
else: else:
self.set_value_renderer(self.value_renderer_factory) self.set_value_renderer(self.value_renderer_factory)
self.default_active = default_active self.default_active = default_active
@ -189,13 +189,13 @@ class GridFilter(object):
return verbs return verbs
return ['equal', 'not_equal', 'is_null', 'is_not_null', 'is_any'] return ['equal', 'not_equal', 'is_null', 'is_not_null', 'is_any']
def set_choices(self, choices): def normalize_choices(self, choices):
""" """
Set the value choices for the filter, post-construction. Note that Normalize a set of "choices" to a format suitable for use with the
this also will set the value renderer to one which supports choices. filter.
:param choices: A collection of "choices" for the filter. This must be :param choices: A collection of "choices" in one of the following
in one of the following formats: formats:
* simple list, each value of which should be a string, which is * simple list, each value of which should be a string, which is
assumed to be able to serve as both key and value (ordering of assumed to be able to serve as both key and value (ordering of
@ -205,20 +205,30 @@ class GridFilter(object):
* OrderedDict, keys and values of which will define the choices * OrderedDict, keys and values of which will define the choices
(ordering of choices will be preserved) (ordering of choices will be preserved)
""" """
# first must normalize choices
if isinstance(choices, OrderedDict): if isinstance(choices, OrderedDict):
normalized = choices normalized = choices
elif isinstance(choices, dict): elif isinstance(choices, dict):
normalized = OrderedDict([ normalized = OrderedDict([
(key, choices[key]) (key, choices[key])
for key in sorted(choices)]) for key in sorted(choices)])
elif isinstance(choices, list): elif isinstance(choices, list):
normalized = OrderedDict([ normalized = OrderedDict([
(key, key) (key, key)
for key in choices]) for key in choices])
# store normalized choices, and set renderer return normalized
self.choices = normalized
def set_choices(self, choices):
"""
Set the value choices for the filter. Note that this also will set the
value renderer to one which supports choices.
:param choices: A collection of "choices" which will be normalized by
way of :meth:`normalize_choices()`.
"""
self.choices = self.normalize_choices(choices)
self.set_value_renderer(ChoiceValueRenderer(self.choices)) self.set_value_renderer(ChoiceValueRenderer(self.choices))
def set_value_renderer(self, renderer): def set_value_renderer(self, renderer):