diff --git a/tailbone/grids/filters.py b/tailbone/grids/filters.py index 54643c94..9d08c881 100644 --- a/tailbone/grids/filters.py +++ b/tailbone/grids/filters.py @@ -161,7 +161,7 @@ class GridFilter(object): if value_renderer: self.set_value_renderer(value_renderer) elif value_enum: - self.set_value_renderer(EnumValueRenderer(value_enum)) + self.set_choices(value_enum) else: self.set_value_renderer(self.value_renderer_factory) self.default_active = default_active @@ -189,13 +189,13 @@ class GridFilter(object): return verbs 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 - this also will set the value renderer to one which supports choices. + Normalize a set of "choices" to a format suitable for use with the + filter. - :param choices: A collection of "choices" for the filter. This must be - in one of the following formats: + :param choices: A collection of "choices" in one of the following + formats: * 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 @@ -205,20 +205,30 @@ class GridFilter(object): * OrderedDict, keys and values of which will define the choices (ordering of choices will be preserved) """ - # first must normalize choices if isinstance(choices, OrderedDict): normalized = choices + elif isinstance(choices, dict): normalized = OrderedDict([ (key, choices[key]) for key in sorted(choices)]) + elif isinstance(choices, list): normalized = OrderedDict([ (key, key) for key in choices]) - # store normalized choices, and set renderer - self.choices = normalized + return 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)) def set_value_renderer(self, renderer):