Made ListFilter.choices() argument name more explicit.

This commit is contained in:
Johannes Ammon 2015-11-22 13:03:56 +01:00 committed by Tim Graham
parent e07def14b8
commit 2ab244ff3a
1 changed files with 22 additions and 24 deletions

View File

@ -37,9 +37,11 @@ class ListFilter(object):
""" """
raise NotImplementedError('subclasses of ListFilter must provide a has_output() method') raise NotImplementedError('subclasses of ListFilter must provide a has_output() method')
def choices(self, cl): def choices(self, changelist):
""" """
Returns choices ready to be output in the template. Returns choices ready to be output in the template.
`changelist` is the ChangeList to be displayed.
""" """
raise NotImplementedError('subclasses of ListFilter must provide a choices() method') raise NotImplementedError('subclasses of ListFilter must provide a choices() method')
@ -98,18 +100,16 @@ class SimpleListFilter(ListFilter):
def expected_parameters(self): def expected_parameters(self):
return [self.parameter_name] return [self.parameter_name]
def choices(self, cl): def choices(self, changelist):
yield { yield {
'selected': self.value() is None, 'selected': self.value() is None,
'query_string': cl.get_query_string({}, [self.parameter_name]), 'query_string': changelist.get_query_string({}, [self.parameter_name]),
'display': _('All'), 'display': _('All'),
} }
for lookup, title in self.lookup_choices: for lookup, title in self.lookup_choices:
yield { yield {
'selected': self.value() == force_text(lookup), 'selected': self.value() == force_text(lookup),
'query_string': cl.get_query_string({ 'query_string': changelist.get_query_string({self.parameter_name: lookup}, []),
self.parameter_name: lookup,
}, []),
'display': title, 'display': title,
} }
@ -197,17 +197,17 @@ class RelatedFieldListFilter(FieldListFilter):
def field_choices(self, field, request, model_admin): def field_choices(self, field, request, model_admin):
return field.get_choices(include_blank=False) return field.get_choices(include_blank=False)
def choices(self, cl): def choices(self, changelist):
yield { yield {
'selected': self.lookup_val is None and not self.lookup_val_isnull, 'selected': self.lookup_val is None and not self.lookup_val_isnull,
'query_string': cl.get_query_string({}, 'query_string': changelist.get_query_string({},
[self.lookup_kwarg, self.lookup_kwarg_isnull]), [self.lookup_kwarg, self.lookup_kwarg_isnull]),
'display': _('All'), 'display': _('All'),
} }
for pk_val, val in self.lookup_choices: for pk_val, val in self.lookup_choices:
yield { yield {
'selected': self.lookup_val == smart_text(pk_val), 'selected': self.lookup_val == smart_text(pk_val),
'query_string': cl.get_query_string({ 'query_string': changelist.get_query_string({
self.lookup_kwarg: pk_val, self.lookup_kwarg: pk_val,
}, [self.lookup_kwarg_isnull]), }, [self.lookup_kwarg_isnull]),
'display': val, 'display': val,
@ -215,7 +215,7 @@ class RelatedFieldListFilter(FieldListFilter):
if self.include_empty_choice: if self.include_empty_choice:
yield { yield {
'selected': bool(self.lookup_val_isnull), 'selected': bool(self.lookup_val_isnull),
'query_string': cl.get_query_string({ 'query_string': changelist.get_query_string({
self.lookup_kwarg_isnull: 'True', self.lookup_kwarg_isnull: 'True',
}, [self.lookup_kwarg]), }, [self.lookup_kwarg]),
'display': self.empty_value_display, 'display': self.empty_value_display,
@ -236,14 +236,14 @@ class BooleanFieldListFilter(FieldListFilter):
def expected_parameters(self): def expected_parameters(self):
return [self.lookup_kwarg, self.lookup_kwarg2] return [self.lookup_kwarg, self.lookup_kwarg2]
def choices(self, cl): def choices(self, changelist):
for lookup, title in ( for lookup, title in (
(None, _('All')), (None, _('All')),
('1', _('Yes')), ('1', _('Yes')),
('0', _('No'))): ('0', _('No'))):
yield { yield {
'selected': self.lookup_val == lookup and not self.lookup_val2, 'selected': self.lookup_val == lookup and not self.lookup_val2,
'query_string': cl.get_query_string({ 'query_string': changelist.get_query_string({
self.lookup_kwarg: lookup, self.lookup_kwarg: lookup,
}, [self.lookup_kwarg2]), }, [self.lookup_kwarg2]),
'display': title, 'display': title,
@ -251,7 +251,7 @@ class BooleanFieldListFilter(FieldListFilter):
if isinstance(self.field, models.NullBooleanField): if isinstance(self.field, models.NullBooleanField):
yield { yield {
'selected': self.lookup_val2 == 'True', 'selected': self.lookup_val2 == 'True',
'query_string': cl.get_query_string({ 'query_string': changelist.get_query_string({
self.lookup_kwarg2: 'True', self.lookup_kwarg2: 'True',
}, [self.lookup_kwarg]), }, [self.lookup_kwarg]),
'display': _('Unknown'), 'display': _('Unknown'),
@ -271,17 +271,16 @@ class ChoicesFieldListFilter(FieldListFilter):
def expected_parameters(self): def expected_parameters(self):
return [self.lookup_kwarg] return [self.lookup_kwarg]
def choices(self, cl): def choices(self, changelist):
yield { yield {
'selected': self.lookup_val is None, 'selected': self.lookup_val is None,
'query_string': cl.get_query_string({}, [self.lookup_kwarg]), 'query_string': changelist.get_query_string({}, [self.lookup_kwarg]),
'display': _('All') 'display': _('All')
} }
for lookup, title in self.field.flatchoices: for lookup, title in self.field.flatchoices:
yield { yield {
'selected': smart_text(lookup) == self.lookup_val, 'selected': smart_text(lookup) == self.lookup_val,
'query_string': cl.get_query_string({ 'query_string': changelist.get_query_string({self.lookup_kwarg: lookup}),
self.lookup_kwarg: lookup}),
'display': title, 'display': title,
} }
@ -338,12 +337,11 @@ class DateFieldListFilter(FieldListFilter):
def expected_parameters(self): def expected_parameters(self):
return [self.lookup_kwarg_since, self.lookup_kwarg_until] return [self.lookup_kwarg_since, self.lookup_kwarg_until]
def choices(self, cl): def choices(self, changelist):
for title, param_dict in self.links: for title, param_dict in self.links:
yield { yield {
'selected': self.date_params == param_dict, 'selected': self.date_params == param_dict,
'query_string': cl.get_query_string( 'query_string': changelist.get_query_string(param_dict, [self.field_generic]),
param_dict, [self.field_generic]),
'display': title, 'display': title,
} }
@ -377,11 +375,11 @@ class AllValuesFieldListFilter(FieldListFilter):
def expected_parameters(self): def expected_parameters(self):
return [self.lookup_kwarg, self.lookup_kwarg_isnull] return [self.lookup_kwarg, self.lookup_kwarg_isnull]
def choices(self, cl): def choices(self, changelist):
yield { yield {
'selected': (self.lookup_val is None 'selected': (self.lookup_val is None
and self.lookup_val_isnull is None), and self.lookup_val_isnull is None),
'query_string': cl.get_query_string({}, 'query_string': changelist.get_query_string({},
[self.lookup_kwarg, self.lookup_kwarg_isnull]), [self.lookup_kwarg, self.lookup_kwarg_isnull]),
'display': _('All'), 'display': _('All'),
} }
@ -393,7 +391,7 @@ class AllValuesFieldListFilter(FieldListFilter):
val = smart_text(val) val = smart_text(val)
yield { yield {
'selected': self.lookup_val == val, 'selected': self.lookup_val == val,
'query_string': cl.get_query_string({ 'query_string': changelist.get_query_string({
self.lookup_kwarg: val, self.lookup_kwarg: val,
}, [self.lookup_kwarg_isnull]), }, [self.lookup_kwarg_isnull]),
'display': val, 'display': val,
@ -401,7 +399,7 @@ class AllValuesFieldListFilter(FieldListFilter):
if include_none: if include_none:
yield { yield {
'selected': bool(self.lookup_val_isnull), 'selected': bool(self.lookup_val_isnull),
'query_string': cl.get_query_string({ 'query_string': changelist.get_query_string({
self.lookup_kwarg_isnull: 'True', self.lookup_kwarg_isnull: 'True',
}, [self.lookup_kwarg]), }, [self.lookup_kwarg]),
'display': self.empty_value_display, 'display': self.empty_value_display,