Code

Ticket #3400: 3400-against-10706.diff

File 3400-against-10706.diff, 14.0 KB (added by Honza_Kral, 5 years ago)

new patch with init and some more data in tests

Line 
1commit 6a328f5b06e13a25bf8a4214fe4bebfa2efe4dd7
2Author: Honza Kral <Honza.Kral@gmail.com>
3Date:   Fri May 8 13:19:26 2009 +0200
4
5    New version of #3400 against [10706]
6   
7    Added some data to the test to actually test the filtering and just he
8    fact that it works.
9
10diff --git a/django/contrib/admin/filterspecs.py b/django/contrib/admin/filterspecs.py
11index 6f643ee..7f473a2 100644
12--- a/django/contrib/admin/filterspecs.py
13+++ b/django/contrib/admin/filterspecs.py
14@@ -15,18 +15,19 @@ import datetime
15 
16 class FilterSpec(object):
17     filter_specs = []
18-    def __init__(self, f, request, params, model, model_admin):
19+    def __init__(self, f, request, params, model, model_admin, field_path=None):
20         self.field = f
21         self.params = params
22+        self.field_path = field_path or f.name
23 
24     def register(cls, test, factory):
25         cls.filter_specs.append((test, factory))
26     register = classmethod(register)
27 
28-    def create(cls, f, request, params, model, model_admin):
29+    def create(cls, f, request, params, model, model_admin, field_path=None):
30         for test, factory in cls.filter_specs:
31             if test(f):
32-                return factory(f, request, params, model, model_admin)
33+                return factory(f, request, params, model, model_admin, field_path=field_path)
34     create = classmethod(create)
35 
36     def has_output(self):
37@@ -52,14 +53,14 @@ class FilterSpec(object):
38         return mark_safe("".join(t))
39 
40 class RelatedFilterSpec(FilterSpec):
41-    def __init__(self, f, request, params, model, model_admin):
42-        super(RelatedFilterSpec, self).__init__(f, request, params, model, model_admin)
43+    def __init__(self, f, request, params, model, model_admin, field_path=None):
44+        super(RelatedFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
45         if isinstance(f, models.ManyToManyField):
46             self.lookup_title = f.rel.to._meta.verbose_name
47         else:
48             self.lookup_title = f.verbose_name
49-        rel_name = f.rel.get_related_field().name
50-        self.lookup_kwarg = '%s__%s__exact' % (f.name, rel_name)
51+        rel_name = f.rel.to._meta.pk.name
52+        self.lookup_kwarg = '%s__%s__exact' % (self.field_path, rel_name)
53         self.lookup_val = request.GET.get(self.lookup_kwarg, None)
54         self.lookup_choices = f.get_choices(include_blank=False)
55 
56@@ -81,9 +82,9 @@ class RelatedFilterSpec(FilterSpec):
57 FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec)
58 
59 class ChoicesFilterSpec(FilterSpec):
60-    def __init__(self, f, request, params, model, model_admin):
61-        super(ChoicesFilterSpec, self).__init__(f, request, params, model, model_admin)
62-        self.lookup_kwarg = '%s__exact' % f.name
63+    def __init__(self, f, request, params, model, model_admin, field_path=None):
64+        super(ChoicesFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
65+        self.lookup_kwarg = '%s__exact' % self.field_path
66         self.lookup_val = request.GET.get(self.lookup_kwarg, None)
67 
68     def choices(self, cl):
69@@ -98,10 +99,10 @@ class ChoicesFilterSpec(FilterSpec):
70 FilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec)
71 
72 class DateFieldFilterSpec(FilterSpec):
73-    def __init__(self, f, request, params, model, model_admin):
74-        super(DateFieldFilterSpec, self).__init__(f, request, params, model, model_admin)
75+    def __init__(self, f, request, params, model, model_admin, field_path=None):
76+        super(DateFieldFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
77 
78-        self.field_generic = '%s__' % self.field.name
79+        self.field_generic = '%s__' % self.field_path
80 
81         self.date_params = dict([(k, v) for k, v in params.items() if k.startswith(self.field_generic)])
82 
83@@ -111,14 +112,14 @@ class DateFieldFilterSpec(FilterSpec):
84 
85         self.links = (
86             (_('Any date'), {}),
87-            (_('Today'), {'%s__year' % self.field.name: str(today.year),
88-                       '%s__month' % self.field.name: str(today.month),
89-                       '%s__day' % self.field.name: str(today.day)}),
90-            (_('Past 7 days'), {'%s__gte' % self.field.name: one_week_ago.strftime('%Y-%m-%d'),
91-                             '%s__lte' % f.name: today_str}),
92-            (_('This month'), {'%s__year' % self.field.name: str(today.year),
93-                             '%s__month' % f.name: str(today.month)}),
94-            (_('This year'), {'%s__year' % self.field.name: str(today.year)})
95+            (_('Today'), {'%s__year' % self.field_path: str(today.year),
96+                       '%s__month' % self.field_path: str(today.month),
97+                       '%s__day' % self.field_path: str(today.day)}),
98+            (_('Past 7 days'), {'%s__gte' % self.field_path: one_week_ago.strftime('%Y-%m-%d'),
99+                             '%s__lte' % self.field_path: today_str}),
100+            (_('This month'), {'%s__year' % self.field_path: str(today.year),
101+                             '%s__month' % self.field_path: str(today.month)}),
102+            (_('This year'), {'%s__year' % self.field_path: str(today.year)})
103         )
104 
105     def title(self):
106@@ -133,10 +134,10 @@ class DateFieldFilterSpec(FilterSpec):
107 FilterSpec.register(lambda f: isinstance(f, models.DateField), DateFieldFilterSpec)
108 
109 class BooleanFieldFilterSpec(FilterSpec):
110-    def __init__(self, f, request, params, model, model_admin):
111-        super(BooleanFieldFilterSpec, self).__init__(f, request, params, model, model_admin)
112-        self.lookup_kwarg = '%s__exact' % f.name
113-        self.lookup_kwarg2 = '%s__isnull' % f.name
114+    def __init__(self, f, request, params, model, model_admin, field_path=None):
115+        super(BooleanFieldFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
116+        self.lookup_kwarg = '%s__exact' % self.field_path
117+        self.lookup_kwarg2 = '%s__isnull' % self.field_path
118         self.lookup_val = request.GET.get(self.lookup_kwarg, None)
119         self.lookup_val2 = request.GET.get(self.lookup_kwarg2, None)
120 
121@@ -159,21 +160,22 @@ FilterSpec.register(lambda f: isinstance(f, models.BooleanField) or isinstance(f
122 # if a field is eligible to use the BooleanFieldFilterSpec, that'd be much
123 # more appropriate, and the AllValuesFilterSpec won't get used for it.
124 class AllValuesFilterSpec(FilterSpec):
125-    def __init__(self, f, request, params, model, model_admin):
126-        super(AllValuesFilterSpec, self).__init__(f, request, params, model, model_admin)
127-        self.lookup_val = request.GET.get(f.name, None)
128-        self.lookup_choices = model_admin.queryset(request).distinct().order_by(f.name).values(f.name)
129+    def __init__(self, f, request, params, model, model_admin, field_path=None):
130+        super(AllValuesFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
131+        self.lookup_val = request.GET.get(self.field_path, None)
132+        #self.lookup_choices = model_admin.queryset(request).distinct().order_by(f.name).values(f.name)
133+        self.lookup_choices = model._default_manager.all().distinct().order_by(f.name).values(f.name)
134 
135     def title(self):
136         return self.field.verbose_name
137 
138     def choices(self, cl):
139         yield {'selected': self.lookup_val is None,
140-               'query_string': cl.get_query_string({}, [self.field.name]),
141+               'query_string': cl.get_query_string({}, [self.field_path]),
142                'display': _('All')}
143         for val in self.lookup_choices:
144             val = smart_unicode(val[self.field.name])
145             yield {'selected': self.lookup_val == val,
146-                   'query_string': cl.get_query_string({self.field.name: val}),
147+                   'query_string': cl.get_query_string({self.field_path: val}),
148                    'display': val}
149 FilterSpec.register(lambda f: True, AllValuesFilterSpec)
150diff --git a/django/contrib/admin/validation.py b/django/contrib/admin/validation.py
151index 4bef007..70d2dfc 100644
152--- a/django/contrib/admin/validation.py
153+++ b/django/contrib/admin/validation.py
154@@ -279,7 +279,16 @@ def check_isdict(cls, label, obj):
155 
156 def get_field(cls, model, opts, label, field):
157     try:
158-        return opts.get_field(field)
159+        if '__' in field:
160+            f = None
161+            m = model
162+            path = field.split('__')
163+            for field_name in path[:-1]:
164+                f = model._meta.get_field(field_name)
165+                model = f.rel.to
166+            return opts.get_field(path[0])
167+        else:
168+            return opts.get_field(field)
169     except models.FieldDoesNotExist:
170         raise ImproperlyConfigured("'%s.%s' refers to field '%s' that is missing from model '%s'."
171                 % (cls.__name__, label, field, model.__name__))
172diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py
173index 98e5b9f..e444a17 100644
174--- a/django/contrib/admin/views/main.py
175+++ b/django/contrib/admin/views/main.py
176@@ -74,9 +74,20 @@ class ChangeList(object):
177     def get_filters(self, request):
178         filter_specs = []
179         if self.list_filter:
180-            filter_fields = [self.lookup_opts.get_field(field_name) for field_name in self.list_filter]
181-            for f in filter_fields:
182-                spec = FilterSpec.create(f, request, self.params, self.model, self.model_admin)
183+            lookup_opts = self.lookup_opts
184+            for filter_name in self.list_filter:
185+                if '__' in filter_name:
186+                    f = None
187+                    model = self.model
188+                    path = filter_name.split('__')
189+                    for field_name in path[:-1]:
190+                        f = model._meta.get_field(field_name)
191+                        model = f.rel.to
192+                        f = model._meta.get_field(path[-1])
193+                        spec = FilterSpec.create(f, request, self.params, model, self.model_admin, field_path=filter_name)
194+                else:
195+                    f = lookup_opts.get_field(filter_name)
196+                    spec = FilterSpec.create(f, request, self.params, self.model, self.model_admin)
197                 if spec and spec.has_output():
198                     filter_specs.append(spec)
199         return filter_specs, bool(filter_specs)
200diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt
201index 700303f..263f4c2 100644
202--- a/docs/ref/contrib/admin/index.txt
203+++ b/docs/ref/contrib/admin/index.txt
204@@ -446,6 +446,11 @@ how both ``list_display`` and ``list_filter`` work::
205         list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
206         list_filter = ('is_staff', 'is_superuser')
207 
208+In ``list_filter`` can be defined lookup separator as well::
209+
210+    class UserAdminWithLookup(UserAdmin):
211+        list_filter = ('groups__name')
212+
213 The above code results in an admin change list page that looks like this:
214 
215     .. image:: _images/users_changelist.png
216diff --git a/tests/regressiontests/admin_filters/__init__.py b/tests/regressiontests/admin_filters/__init__.py
217new file mode 100644
218index 0000000..e69de29
219diff --git a/tests/regressiontests/admin_filters/fixtures/admin-filter-data.json b/tests/regressiontests/admin_filters/fixtures/admin-filter-data.json
220new file mode 100644
221index 0000000..dd739a3
222--- /dev/null
223+++ b/tests/regressiontests/admin_filters/fixtures/admin-filter-data.json
224@@ -0,0 +1,23 @@
225+[
226+    {
227+        "pk": 1,
228+        "model": "admin_filters.filterable",
229+        "fields": {
230+            "sites": [1]
231+        }
232+    },
233+    {
234+        "pk": 2,
235+        "model": "admin_filters.filterable",
236+        "fields": {
237+            "sites": []
238+        }
239+    },
240+    {
241+        "pk": 3,
242+        "model": "admin_filters.filterable",
243+        "fields": {
244+            "sites": [1]
245+        }
246+    }
247+]
248diff --git a/tests/regressiontests/admin_filters/fixtures/admin-filter-user.json b/tests/regressiontests/admin_filters/fixtures/admin-filter-user.json
249new file mode 100644
250index 0000000..33ae10e
251--- /dev/null
252+++ b/tests/regressiontests/admin_filters/fixtures/admin-filter-user.json
253@@ -0,0 +1,20 @@
254+[
255+    {
256+        "pk": 1,
257+        "model": "auth.user",
258+        "fields": {
259+            "username": "admin",
260+            "first_name": "",
261+            "last_name": "",
262+            "is_active": true,
263+            "is_superuser": true,
264+            "is_staff": true,
265+            "last_login": "2009-03-30 22:05:28",
266+            "groups": [],
267+            "user_permissions": [],
268+            "password": "sha1$6d71f$d2b636e70cbd76dd4138766efffc46f30bcc5895",
269+            "email": "a@a.cz",
270+            "date_joined": "2009-03-30 22:05:28"
271+        }
272+    }
273+]
274diff --git a/tests/regressiontests/admin_filters/models.py b/tests/regressiontests/admin_filters/models.py
275new file mode 100644
276index 0000000..6c28ae2
277--- /dev/null
278+++ b/tests/regressiontests/admin_filters/models.py
279@@ -0,0 +1,14 @@
280+from django.db import models
281+from django.contrib.sites.models import Site
282+
283+class Filterable(models.Model):
284+    sites = models.ManyToManyField(Site, blank=True)
285+
286+
287+from django.contrib import admin
288+
289+class FilterableAdmin(admin.ModelAdmin):
290+    list_filter = ('sites__domain',)
291+
292+admin.site.register(Filterable, FilterableAdmin)
293+
294diff --git a/tests/regressiontests/admin_filters/tests.py b/tests/regressiontests/admin_filters/tests.py
295new file mode 100644
296index 0000000..4b71c1a
297--- /dev/null
298+++ b/tests/regressiontests/admin_filters/tests.py
299@@ -0,0 +1,22 @@
300+from django.test import TestCase
301+
302+
303+class AdminFilters(TestCase):
304+    fixtures = ['admin-filter-user.json', 'admin-filter-data.json']
305+    admin_url = '/test_admin/admin'
306+
307+    def setUp(self):
308+        self.client.login(username='admin', password='admin')
309+
310+    def tearDown(self):
311+        self.client.logout()
312+
313+    def test_filters_are_enabled(self):
314+        """
315+        log superuser in and go to filtered page
316+        """
317+        response = self.client.get('%s/admin_filters/filterable/' % self.admin_url, {'sites__domain': 'example.com'})
318+
319+        self.failUnlessEqual(response.status_code, 200)
320+        self.failUnlessEqual('2 filterables' in response.content, True)
321+