Code

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

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

Updated patch includes fixes for poblems in changelist tabel and in filter section, includes tests

Line 
1diff -r 3976471d3430 django/contrib/admin/filterspecs.py
2--- a/django/contrib/admin/filterspecs.py       Wed Feb 25 21:56:00 2009 -0200
3+++ b/django/contrib/admin/filterspecs.py       Thu Feb 26 13:39:24 2009 -0200
4@@ -89,7 +89,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 3976471d3430 django/contrib/admin/templatetags/admin_list.py
14--- a/django/contrib/admin/templatetags/admin_list.py   Wed Feb 25 21:56:00 2009 -0200
15+++ b/django/contrib/admin/templatetags/admin_list.py   Thu Feb 26 13:39:24 2009 -0200
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 3976471d3430 tests/regressiontests/admin_views/customadmin.py
28--- a/tests/regressiontests/admin_views/customadmin.py  Wed Feb 25 21:56:00 2009 -0200
29+++ b/tests/regressiontests/admin_views/customadmin.py  Thu Feb 26 13:39:24 2009 -0200
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 3976471d3430 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        Thu Feb 26 13:39:24 2009 -0200
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 3976471d3430 tests/regressiontests/admin_views/models.py
52--- a/tests/regressiontests/admin_views/models.py       Wed Feb 25 21:56:00 2009 -0200
53+++ b/tests/regressiontests/admin_views/models.py       Thu Feb 26 13:39:24 2009 -0200
54@@ -134,12 +134,28 @@
55 class ThingAdmin(admin.ModelAdmin):
56     list_filter = ('color',)
57 
58+class Fabric(models.Model):
59+    NG_CHOICES = (
60+        ('Textured', (
61+                ('x', 'Horizontal'),
62+                ('y', 'Vertical'),
63+            )
64+        ),
65+        ('plain', 'Smooth'),
66+    )
67+    surface = models.CharField(max_length=20, choices=NG_CHOICES)
68+
69+class FabricAdmin(admin.ModelAdmin):
70+    list_display = ('surface',)
71+    list_filter = ('surface',)
72+
73 admin.site.register(Article, ArticleAdmin)
74 admin.site.register(CustomArticle, CustomArticleAdmin)
75 admin.site.register(Section, inlines=[ArticleInline])
76 admin.site.register(ModelWithStringPrimaryKey)
77 admin.site.register(Color)
78 admin.site.register(Thing, ThingAdmin)
79+admin.site.register(Fabric, FabricAdmin)
80 
81 # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
82 # That way we cover all four cases:
83diff -r 3976471d3430 tests/regressiontests/admin_views/tests.py
84--- a/tests/regressiontests/admin_views/tests.py        Wed Feb 25 21:56:00 2009 -0200
85+++ b/tests/regressiontests/admin_views/tests.py        Thu Feb 26 13:39:24 2009 -0200
86@@ -12,9 +12,9 @@
87 from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey
88 
89 class AdminViewBasicTest(TestCase):
90-    fixtures = ['admin-views-users.xml', 'admin-views-colors.xml']
91-   
92-    # Store the bit of the URL where the admin is registered as a class
93+    fixtures = ['admin-views-users.xml', 'admin-views-colors.xml', 'admin-views-fabrics.xml']
94+
95+    # Store the bit of the URL where the admin is registered as a class
96     # variable. That way we can test a second AdminSite just by subclassing
97     # this test case and changing urlbit.
98     urlbit = 'admin'
99@@ -172,6 +172,37 @@
100         self.assertRedirects(response, '/test_admin/%s/admin_views/thing/?e=1' % self.urlbit)       
101         response = self.client.get('/test_admin/%s/admin_views/thing/' % self.urlbit, {'color__id__exact': 'StringNotInteger!'})
102         self.assertRedirects(response, '/test_admin/%s/admin_views/thing/?e=1' % self.urlbit)
103+
104+    def testNamedGroupFieldChoicesChangeList(self):
105+        """
106+        Ensures the admin changelist shows correct values in the relevant column
107+        for rows corresponding to instances of a model in which a named group
108+        has been used in the choices option of a field.
109+        """
110+        response = self.client.get('/test_admin/%s/admin_views/fabric/' % self.urlbit)
111+        self.failUnlessEqual(response.status_code, 200)
112+        self.failUnless(
113+            '<a href="1/">Horizontal</a>' in response.content and
114+            '<a href="2/">Vertical</a>' in response.content,
115+            "Changelist table isn't showing the right human-readable values set by a model field 'choices' option named group."
116+        )
117+
118+    def testNamedGroupFieldChoicesFilter(self):
119+        """
120+        Ensures the filter UI shows correctly when at least one named group has
121+        been used in the choices option of a model field.
122+        """
123+        response = self.client.get('/test_admin/%s/admin_views/fabric/' % self.urlbit)
124+        self.failUnlessEqual(response.status_code, 200)
125+        self.failUnless(
126+            '<div id="changelist-filter">' in response.content,
127+            "Expected filter not found in changelist view."
128+        )
129+        self.failUnless(
130+            '<a href="?surface__exact=x">Horizontal</a>' in response.content and
131+            '<a href="?surface__exact=y">Vertical</a>' in response.content,
132+            "Changelist filter isn't showing options contained inside a model field 'choices' option named group."
133+        )
134 
135 class CustomModelAdminTest(AdminViewBasicTest):
136     urlbit = "admin2"