Permalink
Browse files

easier way to add filters

  • Loading branch information...
1 parent 8d210a1 commit 58a74b50b35336f546414a5bf5e5fab630070da4 ojii committed Apr 15, 2010
View
@@ -0,0 +1,5 @@
+*.pyc
+.settings
+.*project
+dist
+*.egg-info
View
No changes.
View
@@ -1 +1 @@
-__version__ = '0.1.1'
+__version__ = '0.1.2'
View
Binary file not shown.
View
@@ -1,14 +1,45 @@
from django.contrib.admin.views.main import ChangeList
from django.contrib.admin.options import ModelAdmin
-
+from django.contrib.admin.filterspecs import FilterSpec
+
+class GenericFilterSpec(FilterSpec):
+ def __init__(self, data, request, title):
+ self.data = data
+ self.request = request
+ self._title = title
+
+ def title(self):
+ return self._title
+
+ def has_output(self):
+ return True
+
+ def choices(self, changelist):
+ if callable(self.data):
+ choices = list(self.data())
+ else:
+ choices = list(self.data)
+ for choice in [dict(zip(['selected', 'query_string', 'display'], x)) for x in choices]:
+ yield choice
+
-class GenericFilterChangeList(ChangeList):
+class GenericFilterChangeList(ChangeList):
+ def __init__(self, request, *args, **kwargs):
+ self.request = request
+ super(GenericFilterChangeList, self).__init__(request, *args, **kwargs)
+
@property
def generic_filters(self):
return getattr(self.model_admin, 'generic_filters', None)
+
+ def build_filter_spec(self, choices, title):
+ return GenericFilterSpec(choices, self.request, title)
def get_filters(self, request):
+ """
+ Extend ChangeList.get_filters to include generic_filters.
+ """
filter_specs = super(GenericFilterChangeList, self).get_filters(request)[0]
if self.generic_filters:
for fname in self.generic_filters:
@@ -21,4 +52,4 @@ def get_filters(self, request):
class GenericFilterAdmin(ModelAdmin):
def get_changelist(self, request, **kwargs):
- return GenericFilterChangeList
+ return GenericFilterChangeList
View
Binary file not shown.
View
@@ -7,6 +7,7 @@
version = version,
description = 'Django Admin Filters',
author = 'Jonas Obrist',
+ author_email = 'jonas.obrist@divio.ch',
url = 'http://github.com/ojii/django-admin-filters',
packages = find_packages(),
zip_safe=False,

0 comments on commit 58a74b5

Please sign in to comment.