Code

Ticket #11195: 11195.changelist-cells-css-classes.2.diff

File 11195.changelist-cells-css-classes.2.diff, 7.8 KB (added by julien, 3 years ago)
Line 
1diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py
2index ba3d132..a9db7aa 100644
3--- a/django/contrib/admin/templatetags/admin_list.py
4+++ b/django/contrib/admin/templatetags/admin_list.py
5@@ -170,7 +170,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 = ['field-%s' % field_name]
11         try:
12             f, attr, value = lookup_field(field_name, result, cl.model_admin)
13         except (AttributeError, ObjectDoesNotExist):
14@@ -178,7 +178,7 @@ def items_for_result(cl, result, form):
15         else:
16             if f is None:
17                 if field_name == u'action_checkbox':
18-                    row_class = ' class="action-checkbox"'
19+                    row_classes = ['action-checkbox']
20                 allow_tags = getattr(attr, 'allow_tags', False)
21                 boolean = getattr(attr, 'boolean', False)
22                 if boolean:
23@@ -204,9 +204,10 @@ def items_for_result(cl, result, form):
24                 if isinstance(f, models.DateField)\
25                 or isinstance(f, models.TimeField)\
26                 or isinstance(f, models.ForeignKey):
27-                    row_class = ' class="nowrap"'
28+                    row_classes.append('nowrap')
29         if force_unicode(result_repr) == '':
30             result_repr = mark_safe(' ')
31+        row_class = ' class="%s"' % ' '.join(row_classes)
32         # If list_display_links not defined, add the link tag to the first field
33         if (first and not cl.list_display_links) or field_name in cl.list_display_links:
34             table_tag = {True:'th', False:'td'}[first]
35diff --git a/tests/regressiontests/admin_changelist/tests.py b/tests/regressiontests/admin_changelist/tests.py
36index 9347dbf..27a7cc4 100644
37--- a/tests/regressiontests/admin_changelist/tests.py
38+++ b/tests/regressiontests/admin_changelist/tests.py
39@@ -49,7 +49,7 @@ class ChangeListTests(TestCase):
40         template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
41         context = Context({'cl': cl})
42         table_output = template.render(context)
43-        row_html = '<tbody><tr class="row1"><td class="action-checkbox"><input type="checkbox" class="action-select" value="%d" name="_selected_action" /></td><th><a href="%d/">name</a></th><td class="nowrap">(None)</td></tr></tbody>' % (new_child.id, new_child.id)
44+        row_html = '<tbody><tr class="row1"><td class="action-checkbox"><input type="checkbox" class="action-select" value="%d" name="_selected_action" /></td><th class="field-name"><a href="%d/">name</a></th><td class="field-parent nowrap">(None)</td></tr></tbody>' % (new_child.id, new_child.id)
45         self.assertFalse(table_output.find(row_html) == -1,
46             'Failed to find expected row element: %s' % table_output)
47 
48@@ -69,7 +69,7 @@ class ChangeListTests(TestCase):
49         template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
50         context = Context({'cl': cl})
51         table_output = template.render(context)
52-        row_html = '<tbody><tr class="row1"><td class="action-checkbox"><input type="checkbox" class="action-select" value="%d" name="_selected_action" /></td><th><a href="%d/">name</a></th><td class="nowrap">Parent object</td></tr></tbody>' % (new_child.id, new_child.id)
53+        row_html = '<tbody><tr class="row1"><td class="action-checkbox"><input type="checkbox" class="action-select" value="%d" name="_selected_action" /></td><th class="field-name"><a href="%d/">name</a></th><td class="field-parent nowrap">Parent object</td></tr></tbody>' % (new_child.id, new_child.id)
54         self.assertFalse(table_output.find(row_html) == -1,
55             'Failed to find expected row element: %s' % table_output)
56 
57diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
58index b6e7b9e..ceda533 100644
59--- a/tests/regressiontests/admin_views/tests.py
60+++ b/tests/regressiontests/admin_views/tests.py
61@@ -1180,6 +1180,31 @@ class AdminViewDeletedObjectsTest(TestCase):
62         response = self.client.get('/test_admin/admin/admin_views/plot/%s/delete/' % quote(3))
63         self.assertContains(response, should_contain)
64 
65+class AdminChangeListCSSClassesTest(TestCase):
66+    urls = "regressiontests.admin_views.urls"
67+    fixtures = ['admin-views-users.xml']
68+
69+    def setUp(self):
70+        self.client.login(username='super', password='secret')
71+
72+    def tearDown(self):
73+        self.client.logout()
74+
75+    def test_class_attributes(self):
76+        """
77+        Cells of the change list table should contain the field name in their class attribute
78+        Refs #11195.
79+        """
80+        Podcast.objects.create(name="Django Dose",
81+            release_date=datetime.date.today())
82+        response = self.client.get('/test_admin/admin/admin_views/podcast/')
83+        self.assertContains(
84+            response, '<th class="field-name">')
85+        self.assertContains(
86+            response, '<td class="field-release_date nowrap">')
87+        self.assertContains(
88+            response, '<td class="action-checkbox">')
89+
90 class AdminViewStringPrimaryKeyTest(TestCase):
91     urls = "regressiontests.admin_views.urls"
92     fixtures = ['admin-views-users.xml', 'string-primary-key.xml']
93@@ -1211,7 +1236,7 @@ class AdminViewStringPrimaryKeyTest(TestCase):
94     def test_changelist_to_changeform_link(self):
95         "The link from the changelist referring to the changeform of the object should be quoted"
96         response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/')
97-        should_contain = """<th><a href="%s/">%s</a></th></tr>""" % (quote(self.pk), escape(self.pk))
98+        should_contain = """<th class="field-__str__"><a href="%s/">%s</a></th></tr>""" % (quote(self.pk), escape(self.pk))
99         self.assertContains(response, should_contain)
100 
101     def test_recentactions_link(self):
102@@ -1823,8 +1848,8 @@ class AdminViewListEditable(TestCase):
103         self.assertContains(response, 'id="id_form-0-id"', 1) # Only one hidden field, in a separate place than the table.
104         self.assertContains(response, 'id="id_form-1-id"', 1)
105         self.assertContains(response, '<div class="hiddenfields">\n<input type="hidden" name="form-0-id" value="%d" id="id_form-0-id" /><input type="hidden" name="form-1-id" value="%d" id="id_form-1-id" />\n</div>' % (story2.id, story1.id))
106-        self.assertContains(response, '<td>%d</td>' % story1.id, 1)
107-        self.assertContains(response, '<td>%d</td>' % story2.id, 1)
108+        self.assertContains(response, '<td class="field-id">%d</td>' % story1.id, 1)
109+        self.assertContains(response, '<td class="field-id">%d</td>' % story2.id, 1)
110 
111     def test_pk_hidden_fields_with_list_display_links(self):
112         """ Similarly as test_pk_hidden_fields, but when the hidden pk fields are
113@@ -1837,8 +1862,8 @@ class AdminViewListEditable(TestCase):
114         self.assertContains(response, 'id="id_form-0-id"', 1) # Only one hidden field, in a separate place than the table.
115         self.assertContains(response, 'id="id_form-1-id"', 1)
116         self.assertContains(response, '<div class="hiddenfields">\n<input type="hidden" name="form-0-id" value="%d" id="id_form-0-id" /><input type="hidden" name="form-1-id" value="%d" id="id_form-1-id" />\n</div>' % (story2.id, story1.id))
117-        self.assertContains(response, '<th><a href="%d/">%d</a></th>' % (story1.id, story1.id), 1)
118-        self.assertContains(response, '<th><a href="%d/">%d</a></th>' % (story2.id, story2.id), 1)
119+        self.assertContains(response, '<th class="field-id"><a href="%d/">%d</a></th>' % (story1.id, story1.id), 1)
120+        self.assertContains(response, '<th class="field-id"><a href="%d/">%d</a></th>' % (story2.id, story2.id), 1)
121 
122 
123 class AdminSearchTest(TestCase):