-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathfilterspecs.py
More file actions
113 lines (91 loc) · 4.11 KB
/
filterspecs.py
File metadata and controls
113 lines (91 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# encoding=utf-8
# Thanks to http://www.djangosnippets.org/snippets/1051/
#
# Authors: Marinho Brandao <marinho at gmail.com>
# Guilherme M. Gondim (semente) <semente at taurinus.org>
try:
from django.contrib.admin.filters import FieldListFilter, ChoicesFieldListFilter
legacy = False
except ImportError: # Django up to 1.3
from django.contrib.admin.filterspecs import (
FilterSpec as FieldListFilter,
ChoicesFilterSpec as ChoicesFieldListFilter)
legacy = True
from django.utils.encoding import smart_unicode
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
class ParentFieldListFilter(ChoicesFieldListFilter):
"""
Improved list_filter display for parent Pages by nicely indenting hierarchy
In theory this would work with any mptt model which uses a "title" attribute.
my_model_field.page_parent_filter = True
"""
def __init__(self, f, request, params, model, model_admin, field_path=None):
from feincms.utils import shorten_string
try:
super(ParentFieldListFilter, self).__init__(f, request, params, model, model_admin, field_path)
except TypeError: # Django 1.2
super(ParentFieldListFilter, self).__init__(f, request, params, model, model_admin)
parent_ids = model.objects.exclude(parent=None).values_list("parent__id", flat=True).order_by("parent__id").distinct()
parents = model.objects.filter(pk__in=parent_ids).values_list("pk", "title", "level")
self.lookup_choices = [(pk, "%s%s" % (" " * level, shorten_string(title, max_length=25))) for pk, title, level in parents]
def choices(self, cl):
yield {
'selected': self.lookup_val is None,
'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
'display': _('All')
}
for pk, title in self.lookup_choices:
yield {
'selected': pk == int(self.lookup_val or '0'),
'query_string': cl.get_query_string({self.lookup_kwarg: pk}),
'display': mark_safe(smart_unicode(title))
}
def title(self):
return _('Parent')
class CategoryFieldListFilter(ChoicesFieldListFilter):
"""
Customization of ChoicesFilterSpec which sorts in the user-expected format
my_model_field.category_filter = True
"""
def __init__(self, f, request, params, model, model_admin, field_path=None):
try:
super(CategoryFieldListFilter, self).__init__(f, request, params, model, model_admin, field_path)
except TypeError: # Django 1.2
super(CategoryFieldListFilter, self).__init__(f, request, params, model, model_admin)
# Restrict results to categories which are actually in use:
self.lookup_choices = [
(i.pk, unicode(i)) for i in f.related.parent_model.objects.exclude(**{
f.related.var_name: None
})
]
self.lookup_choices.sort(key=lambda i: i[1])
def choices(self, cl):
yield {
'selected': self.lookup_val is None,
'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
'display': _('All')
}
for pk, title in self.lookup_choices:
yield {
'selected': pk == int(self.lookup_val or '0'),
'query_string': cl.get_query_string({self.lookup_kwarg: pk}),
'display': mark_safe(smart_unicode(title))
}
def title(self):
return _('Category')
if legacy:
# registering the filter
FieldListFilter.filter_specs.insert(0,
(lambda f: getattr(f, 'parent_filter', False), ParentFieldListFilter)
)
FieldListFilter.filter_specs.insert(1,
(lambda f: getattr(f, 'category_filter', False), CategoryFieldListFilter)
)
else:
FieldListFilter.register(lambda f: getattr(f, 'parent_filter', False),
ParentFieldListFilter,
take_priority=True)
FieldListFilter.register(lambda f: getattr(f, 'category_filter', False),
CategoryFieldListFilter,
take_priority=True)