Commits

Chris Lasher  committed 7301827

Using Django 1.4's filters instead of filterspec.

This affects the filtering of the Expense models. I have removed
filterspecs.py and replaced it with filters.py, which uses Django 1.4's
new admin.contrib.filters module, a sturdier implementation of custom
filtering.

  • Participants
  • Parent commits 0e02f03

Comments (0)

Files changed (3)

File expense/admin.py

+from datetime import datetime
+from urlparse import urlparse
+
+from django.contrib import admin
+from django.utils.datastructures import MultiValueDictKeyError
+from django.conf import settings
+
 from forms import *
-from django.contrib import admin
-from datetime import datetime
-from django.utils.datastructures import MultiValueDictKeyError
-from django.contrib.admin.filterspecs import FilterSpec
-from filterspec import ExpenseDateFilterSpec
-from urlparse import urlparse
-from django.conf import settings
+from filters import ExpenseDateListFilter
 
 def get_user_id(request):
     """
                     'description',
                     'formatted_amount')
 
-    list_filter = ('user', 'date', 'category', )
+    list_filter = ('user', ('date', ExpenseDateListFilter), 'category', )
     ordering = ('-date', )
 
     def get_form(self, request, obj=None, **kwargs):
 
 admin.site.register(Expense, ExpenseAdmin)
 
-
-# registering the filterspec
-
-FilterSpec.filter_specs.insert(0, (lambda f: getattr(
-    f, 'expense_date_filter', False), ExpenseDateFilterSpec))

File expense/filters.py

+from django.contrib.admin.filters import DateFieldListFilter
+from django.utils.translation import ugettext as _
+from django.contrib.admin.filters import FieldListFilter
+
+
+class ExpenseDateListFilter(DateFieldListFilter):
+    """
+    A custom filter for the date field.
+
+    It list all the available months, years of the Expenses objects.
+    """
+    def __init__(self, field, request, params, model, model_admin,
+                 field_path=None):
+
+        super(ExpenseDateListFilter, self).__init__(
+                field, request, params, model, model_admin, field_path)
+
+        self.links = [(_('Any date'), {}),]
+
+        months = model.objects.get_months()
+        for m in months:
+            self.links.append(
+                (m.strftime("%B %Y"),
+                 {'%s__year' % self.field.name: str(m.year),
+                  '%s__month' % self.field.name: str(m.month)}))
+
+        years = model.objects.get_years()
+        for y in years:
+            self.links.append(
+                (y.strftime("%Y " + _("total")),
+                 {'%s__year' % self.field.name: str(y.year)}))
+

File expense/filterspec.py

-#from django.db import models
-from django.contrib.admin.filterspecs import DateFieldFilterSpec
-from django.utils.translation import ugettext as _
-from django.utils.html import escape
-from django.utils.safestring import mark_safe
-from django.utils.encoding import iri_to_uri
-from django.conf import settings
-
-class ExpenseDateFilterSpec(DateFieldFilterSpec):
-    """
-    A custom filter spec for the date field.
-
-    It list all the available months, years of the Expenses objects.
-    """
-
-    def __init__(self, f, request, params, model, model_admin,
-                 field_path=None):
-
-        super(ExpenseDateFilterSpec, self).__init__(f, request, params, model,
-                                                  model_admin)
-
-        self.links = [(_('Any date'), {}),]
-
-
-        months = model.objects.get_months()
-        for m in months:
-            self.links.append(
-                (m.strftime("%Y. %B"),
-                 {'%s__year' % self.field.name: str(m.year),
-                  '%s__month' % self.field.name: str(m.month)}))
-
-        years = model.objects.get_years()
-        for y in years:
-            self.links.append(
-                (y.strftime("%Y. total"),
-                 {'%s__year' % self.field.name: str(y.year)}))