Code

Ticket #9969: 9969-admin-bugs-with-nested-choices-r10283.diff

File 9969-admin-bugs-with-nested-choices-r10283.diff, 6.2 KB (added by ramiro, 5 years ago)

Patch updated to apply cleanly to trunk as of r10283

Line 
1diff -r f44ec14ec084 django/contrib/admin/filterspecs.py
2--- a/django/contrib/admin/filterspecs.py       Tue Mar 31 16:55:20 2009 -0300
3+++ b/django/contrib/admin/filterspecs.py       Tue Mar 31 18:34:44 2009 -0300
4@@ -90,7 +90,7 @@
5         yield {'selected': self.lookup_val is None,
6                'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
7                'display': _('All')}
8-        for k, v in self.field.choices:
9+        for k, v in self.field.flatchoices:
10             yield {'selected': smart_unicode(k) == self.lookup_val,
11                     'query_string': cl.get_query_string({self.lookup_kwarg: k}),
12                     'display': v}
13diff -r f44ec14ec084 django/contrib/admin/templatetags/admin_list.py
14--- a/django/contrib/admin/templatetags/admin_list.py   Tue Mar 31 16:55:20 2009 -0300
15+++ b/django/contrib/admin/templatetags/admin_list.py   Tue Mar 31 18:34:44 2009 -0300
16@@ -205,8 +205,8 @@
17                     result_repr = EMPTY_CHANGELIST_VALUE
18             # Fields with choices are special: Use the representation
19             # of the choice.
20-            elif f.choices:
21-                result_repr = dict(f.choices).get(field_val, EMPTY_CHANGELIST_VALUE)
22+            elif f.flatchoices:
23+                result_repr = dict(f.flatchoices).get(field_val, EMPTY_CHANGELIST_VALUE)
24             else:
25                 result_repr = escape(field_val)
26         if force_unicode(result_repr) == '':
27diff -r f44ec14ec084 tests/regressiontests/admin_views/customadmin.py
28--- a/tests/regressiontests/admin_views/customadmin.py  Tue Mar 31 16:55:20 2009 -0300
29+++ b/tests/regressiontests/admin_views/customadmin.py  Tue Mar 31 18:34:44 2009 -0300
30@@ -28,3 +28,4 @@
31 site.register(models.Article, models.ArticleAdmin)
32 site.register(models.Section, inlines=[models.ArticleInline])
33 site.register(models.Thing, models.ThingAdmin)
34+site.register(models.Fabric, models.FabricAdmin)
35diff -r f44ec14ec084 tests/regressiontests/admin_views/fixtures/admin-views-fabrics.xml
36--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
37+++ b/tests/regressiontests/admin_views/fixtures/admin-views-fabrics.xml        Tue Mar 31 18:34:44 2009 -0300
38@@ -0,0 +1,12 @@
39+<?xml version="1.0" encoding="utf-8"?>
40+<django-objects version="1.0">
41+  <object pk="1" model="admin_views.fabric">
42+    <field type="CharField" name="surface">x</field>
43+  </object>
44+  <object pk="2" model="admin_views.fabric">
45+    <field type="CharField" name="surface">y</field>
46+  </object>
47+  <object pk="3" model="admin_views.fabric">
48+    <field type="CharField" name="surface">plain</field>
49+  </object>
50+</django-objects>
51diff -r f44ec14ec084 tests/regressiontests/admin_views/models.py
52--- a/tests/regressiontests/admin_views/models.py       Tue Mar 31 16:55:20 2009 -0300
53+++ b/tests/regressiontests/admin_views/models.py       Tue Mar 31 18:34:44 2009 -0300
54@@ -134,6 +134,21 @@
55 
56 class ThingAdmin(admin.ModelAdmin):
57     list_filter = ('color',)
58+
59+class Fabric(models.Model):
60+    NG_CHOICES = (
61+        ('Textured', (
62+                ('x', 'Horizontal'),
63+                ('y', 'Vertical'),
64+            )
65+        ),
66+        ('plain', 'Smooth'),
67+    )
68+    surface = models.CharField(max_length=20, choices=NG_CHOICES)
69+
70+class FabricAdmin(admin.ModelAdmin):
71+    list_display = ('surface',)
72+    list_filter = ('surface',)
73 
74 class Person(models.Model):
75     GENDER_CHOICES = (
76@@ -274,6 +289,7 @@
77 admin.site.register(ExternalSubscriber, ExternalSubscriberAdmin)
78 admin.site.register(Podcast, PodcastAdmin)
79 admin.site.register(Parent, ParentAdmin)
80+admin.site.register(Fabric, FabricAdmin)
81 
82 # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
83 # That way we cover all four cases:
84diff -r f44ec14ec084 tests/regressiontests/admin_views/tests.py
85--- a/tests/regressiontests/admin_views/tests.py        Tue Mar 31 16:55:20 2009 -0300
86+++ b/tests/regressiontests/admin_views/tests.py        Tue Mar 31 18:34:44 2009 -0300
87@@ -21,7 +21,7 @@
88     from sets import Set as set
89 
90 class AdminViewBasicTest(TestCase):
91-    fixtures = ['admin-views-users.xml', 'admin-views-colors.xml']
92+    fixtures = ['admin-views-users.xml', 'admin-views-colors.xml', 'admin-views-fabrics.xml']
93 
94     # Store the bit of the URL where the admin is registered as a class
95     # variable. That way we can test a second AdminSite just by subclassing
96@@ -181,6 +181,37 @@
97         self.assertRedirects(response, '/test_admin/%s/admin_views/thing/?e=1' % self.urlbit)
98         response = self.client.get('/test_admin/%s/admin_views/thing/' % self.urlbit, {'color__id__exact': 'StringNotInteger!'})
99         self.assertRedirects(response, '/test_admin/%s/admin_views/thing/?e=1' % self.urlbit)
100+
101+    def testNamedGroupFieldChoicesChangeList(self):
102+        """
103+        Ensures the admin changelist shows correct values in the relevant column
104+        for rows corresponding to instances of a model in which a named group
105+        has been used in the choices option of a field.
106+        """
107+        response = self.client.get('/test_admin/%s/admin_views/fabric/' % self.urlbit)
108+        self.failUnlessEqual(response.status_code, 200)
109+        self.failUnless(
110+            '<a href="1/">Horizontal</a>' in response.content and
111+            '<a href="2/">Vertical</a>' in response.content,
112+            "Changelist table isn't showing the right human-readable values set by a model field 'choices' option named group."
113+        )
114+
115+    def testNamedGroupFieldChoicesFilter(self):
116+        """
117+        Ensures the filter UI shows correctly when at least one named group has
118+        been used in the choices option of a model field.
119+        """
120+        response = self.client.get('/test_admin/%s/admin_views/fabric/' % self.urlbit)
121+        self.failUnlessEqual(response.status_code, 200)
122+        self.failUnless(
123+            '<div id="changelist-filter">' in response.content,
124+            "Expected filter not found in changelist view."
125+        )
126+        self.failUnless(
127+            '<a href="?surface__exact=x">Horizontal</a>' in response.content and
128+            '<a href="?surface__exact=y">Vertical</a>' in response.content,
129+            "Changelist filter isn't showing options contained inside a model field 'choices' option named group."
130+        )
131 
132 class CustomModelAdminTest(AdminViewBasicTest):
133     urlbit = "admin2"