Made cosmetic edits to contrib.admin.filters.

This commit is contained in:
Tim Graham 2018-02-14 13:40:24 -05:00 committed by GitHub
parent 98dd943893
commit f5c9bbd3e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 43 deletions

View File

@ -27,8 +27,9 @@ class ListFilter:
self.used_parameters = {} self.used_parameters = {}
if self.title is None: if self.title is None:
raise ImproperlyConfigured( raise ImproperlyConfigured(
"The list filter '%s' does not specify " "The list filter '%s' does not specify a 'title'."
"a 'title'." % self.__class__.__name__) % self.__class__.__name__
)
def has_output(self): def has_output(self):
""" """
@ -66,8 +67,9 @@ class SimpleListFilter(ListFilter):
super().__init__(request, params, model, model_admin) super().__init__(request, params, model, model_admin)
if self.parameter_name is None: if self.parameter_name is None:
raise ImproperlyConfigured( raise ImproperlyConfigured(
"The list filter '%s' does not specify " "The list filter '%s' does not specify a 'parameter_name'."
"a 'parameter_name'." % self.__class__.__name__) % self.__class__.__name__
)
if self.parameter_name in params: if self.parameter_name in params:
value = params.pop(self.parameter_name) value = params.pop(self.parameter_name)
self.used_parameters[self.parameter_name] = value self.used_parameters[self.parameter_name] = value
@ -93,7 +95,8 @@ class SimpleListFilter(ListFilter):
""" """
raise NotImplementedError( raise NotImplementedError(
'The SimpleListFilter.lookups() method must be overridden to ' 'The SimpleListFilter.lookups() method must be overridden to '
'return a list of tuples (value, verbose value)') 'return a list of tuples (value, verbose value).'
)
def expected_parameters(self): def expected_parameters(self):
return [self.parameter_name] return [self.parameter_name]
@ -101,13 +104,13 @@ class SimpleListFilter(ListFilter):
def choices(self, changelist): def choices(self, changelist):
yield { yield {
'selected': self.value() is None, 'selected': self.value() is None,
'query_string': changelist.get_query_string({}, [self.parameter_name]), 'query_string': changelist.get_query_string(remove=[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() == str(lookup), 'selected': self.value() == str(lookup),
'query_string': changelist.get_query_string({self.parameter_name: lookup}, []), 'query_string': changelist.get_query_string({self.parameter_name: lookup}),
'display': title, 'display': title,
} }
@ -196,26 +199,19 @@ class RelatedFieldListFilter(FieldListFilter):
def choices(self, changelist): 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': changelist.get_query_string( 'query_string': changelist.get_query_string(remove=[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 == str(pk_val), 'selected': self.lookup_val == str(pk_val),
'query_string': changelist.get_query_string({ 'query_string': changelist.get_query_string({self.lookup_kwarg: pk_val}, [self.lookup_kwarg_isnull]),
self.lookup_kwarg: pk_val,
}, [self.lookup_kwarg_isnull]),
'display': val, 'display': val,
} }
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': changelist.get_query_string({ 'query_string': changelist.get_query_string({self.lookup_kwarg_isnull: 'True'}, [self.lookup_kwarg]),
self.lookup_kwarg_isnull: 'True',
}, [self.lookup_kwarg]),
'display': self.empty_value_display, 'display': self.empty_value_display,
} }
@ -244,17 +240,13 @@ class BooleanFieldListFilter(FieldListFilter):
('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': changelist.get_query_string({ 'query_string': changelist.get_query_string({self.lookup_kwarg: lookup}, [self.lookup_kwarg2]),
self.lookup_kwarg: lookup,
}, [self.lookup_kwarg2]),
'display': title, 'display': title,
} }
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': changelist.get_query_string({ 'query_string': changelist.get_query_string({self.lookup_kwarg2: 'True'}, [self.lookup_kwarg]),
self.lookup_kwarg2: 'True',
}, [self.lookup_kwarg]),
'display': _('Unknown'), 'display': _('Unknown'),
} }
@ -279,9 +271,7 @@ class ChoicesFieldListFilter(FieldListFilter):
def choices(self, changelist): def choices(self, changelist):
yield { yield {
'selected': self.lookup_val is None, 'selected': self.lookup_val is None,
'query_string': changelist.get_query_string( 'query_string': changelist.get_query_string(remove=[self.lookup_kwarg, self.lookup_kwarg_isnull]),
{}, [self.lookup_kwarg, self.lookup_kwarg_isnull]
),
'display': _('All') 'display': _('All')
} }
none_title = '' none_title = ''
@ -291,17 +281,13 @@ class ChoicesFieldListFilter(FieldListFilter):
continue continue
yield { yield {
'selected': str(lookup) == self.lookup_val, 'selected': str(lookup) == self.lookup_val,
'query_string': changelist.get_query_string( 'query_string': changelist.get_query_string({self.lookup_kwarg: lookup}, [self.lookup_kwarg_isnull]),
{self.lookup_kwarg: lookup}, [self.lookup_kwarg_isnull]
),
'display': title, 'display': title,
} }
if none_title: if none_title:
yield { yield {
'selected': bool(self.lookup_val_isnull), 'selected': bool(self.lookup_val_isnull),
'query_string': changelist.get_query_string({ 'query_string': changelist.get_query_string({self.lookup_kwarg_isnull: 'True'}, [self.lookup_kwarg]),
self.lookup_kwarg_isnull: 'True',
}, [self.lookup_kwarg]),
'display': none_title, 'display': none_title,
} }
@ -395,10 +381,7 @@ class AllValuesFieldListFilter(FieldListFilter):
queryset = model_admin.get_queryset(request) queryset = model_admin.get_queryset(request)
else: else:
queryset = parent_model._default_manager.all() queryset = parent_model._default_manager.all()
self.lookup_choices = (queryset self.lookup_choices = queryset.distinct().order_by(field.name).values_list(field.name, flat=True)
.distinct()
.order_by(field.name)
.values_list(field.name, flat=True))
super().__init__(field, request, params, model, model_admin, field_path) super().__init__(field, request, params, model, model_admin, field_path)
def expected_parameters(self): def expected_parameters(self):
@ -407,7 +390,7 @@ class AllValuesFieldListFilter(FieldListFilter):
def choices(self, changelist): def choices(self, changelist):
yield { yield {
'selected': self.lookup_val is None and self.lookup_val_isnull is None, 'selected': self.lookup_val is None and self.lookup_val_isnull is None,
'query_string': changelist.get_query_string({}, [self.lookup_kwarg, self.lookup_kwarg_isnull]), 'query_string': changelist.get_query_string(remove=[self.lookup_kwarg, self.lookup_kwarg_isnull]),
'display': _('All'), 'display': _('All'),
} }
include_none = False include_none = False
@ -418,17 +401,13 @@ class AllValuesFieldListFilter(FieldListFilter):
val = str(val) val = str(val)
yield { yield {
'selected': self.lookup_val == val, 'selected': self.lookup_val == val,
'query_string': changelist.get_query_string({ 'query_string': changelist.get_query_string({self.lookup_kwarg: val}, [self.lookup_kwarg_isnull]),
self.lookup_kwarg: val,
}, [self.lookup_kwarg_isnull]),
'display': val, 'display': val,
} }
if include_none: if include_none:
yield { yield {
'selected': bool(self.lookup_val_isnull), 'selected': bool(self.lookup_val_isnull),
'query_string': changelist.get_query_string({ 'query_string': changelist.get_query_string({self.lookup_kwarg_isnull: 'True'}, [self.lookup_kwarg]),
self.lookup_kwarg_isnull: 'True',
}, [self.lookup_kwarg]),
'display': self.empty_value_display, 'display': self.empty_value_display,
} }