Code

Ticket #11195: 11195_admin-changelist-cell-class-fieldname.1.diff

File 11195_admin-changelist-cell-class-fieldname.1.diff, 3.5 KB (added by akaihola, 5 years ago)

patch: initial implementation and tests

Line 
1diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py
2index d97c36a..e046246 100644
3--- a/django/contrib/admin/templatetags/admin_list.py
4+++ b/django/contrib/admin/templatetags/admin_list.py
5@@ -137,7 +137,7 @@ def items_for_result(cl, result, form):
6     first = True
7     pk = cl.lookup_opts.pk.attname
8     for field_name in cl.list_display:
9-        row_class = ''
10+        row_classes = ['fieldname_%s' % field_name]
11         try:
12             f = cl.lookup_opts.get_field(field_name)
13         except models.FieldDoesNotExist:
14@@ -193,7 +193,7 @@ def items_for_result(cl, result, form):
15                         result_repr = capfirst(dateformat.format(field_val, date_format))
16                 else:
17                     result_repr = EMPTY_CHANGELIST_VALUE
18-                row_class = ' class="nowrap"'
19+                row_classes.append('nowrap')
20             # Booleans are special: We use images.
21             elif isinstance(f, models.BooleanField) or isinstance(f, models.NullBooleanField):
22                 result_repr = _boolean_icon(field_val)
23@@ -211,6 +211,7 @@ def items_for_result(cl, result, form):
24                 result_repr = escape(field_val)
25         if force_unicode(result_repr) == '':
26             result_repr = mark_safe(' ')
27+        row_class = ' class="%s"' % ' '.join(row_classes)
28         # If list_display_links not defined, add the link tag to the first field
29         if (first and not cl.list_display_links) or field_name in cl.list_display_links:
30             table_tag = {True:'th', False:'td'}[first]
31diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
32index 8e7010b..c3c5dbd 100644
33--- a/tests/regressiontests/admin_views/tests.py
34+++ b/tests/regressiontests/admin_views/tests.py
35@@ -577,6 +577,27 @@ class AdminViewPermissionsTest(TestCase):
36         self.failUnlessEqual(logged.object_id, u'1')
37         self.client.get('/test_admin/admin/logout/')
38 
39+class AdminChangeListFieldNameClassAttrTest(TestCase):
40+    fixtures = ['admin-views-users.xml']
41+
42+    def setUp(self):
43+        self.client.login(username='super', password='secret')
44+
45+    def tearDown(self):
46+        self.client.logout()
47+
48+    def test_class_attributes(self):
49+        "Cells of the change list table should contain the field name in their class attribute"
50+        Podcast.objects.create(name="This Week in Django",
51+            release_date=datetime.date.today())
52+        response = self.client.get('/test_admin/admin/admin_views/podcast/')
53+        self.assertContains(
54+            response, '<td class="fieldname_action_checkbox">')
55+        self.assertContains(
56+            response, '<th class="fieldname_name">')
57+        self.assertContains(
58+            response, '<td class="fieldname_release_date nowrap">')
59+
60 class AdminViewStringPrimaryKeyTest(TestCase):
61     fixtures = ['admin-views-users.xml', 'string-primary-key.xml']
62 
63@@ -601,7 +622,7 @@ class AdminViewStringPrimaryKeyTest(TestCase):
64     def test_changelist_to_changeform_link(self):
65         "The link from the changelist referring to the changeform of the object should be quoted"
66         response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/')
67-        should_contain = """<th><a href="%s/">%s</a></th></tr>""" % (quote(self.pk), escape(self.pk))
68+        should_contain = """<th class="fieldname___str__"><a href="%s/">%s</a></th></tr>""" % (quote(self.pk), escape(self.pk))
69         self.assertContains(response, should_contain)
70 
71     def test_recentactions_link(self):