From 85366fbca723c9b37d0ac9db1d44e3f1cb188db2 Mon Sep 17 00:00:00 2001
From: Durval Carvalho <durvalcsouza@outlook.com>
Date: Tue, 31 Jan 2023 15:05:03 -0300
Subject: [PATCH] Fixed #34045 -- Improved accessibility of selecting items in
 admin changelist.

This adds "aria-label".
---
 AUTHORS                                       |  1 +
 django/contrib/admin/helpers.py               |  3 ---
 django/contrib/admin/options.py               |  9 ++++++---
 .../contrib/admin/templatetags/admin_list.py  |  6 +++++-
 docs/releases/5.0.txt                         |  2 ++
 js_tests/tests.html                           |  2 +-
 tests/admin_changelist/tests.py               | 19 ++++++++++++-------
 7 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/AUTHORS b/AUTHORS
index 2cddbf9280..34f40bbbe6 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -288,6 +288,7 @@ answer newbie questions, and generally made Django that much better:
     Doug Beck <doug@douglasbeck.com>
     Doug Napoleone <doug@dougma.com>
     dready <wil@mojipage.com>
+    Durval Carvalho de Souza <dudurval2@gmail.com>
     dusk@woofle.net
     Dustyn Gibson <miigotu@gmail.com>
     Ed Morley <https://github.com/edmorley>
diff --git a/django/contrib/admin/helpers.py b/django/contrib/admin/helpers.py
index b038b3ebbb..90ca7affc8 100644
--- a/django/contrib/admin/helpers.py
+++ b/django/contrib/admin/helpers.py
@@ -36,9 +36,6 @@ class ActionForm(forms.Form):
     )
 
 
-checkbox = forms.CheckboxInput({"class": "action-select"}, lambda value: False)
-
-
 class AdminForm:
     def __init__(
         self,
diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
index ed70f93628..1e08458fe1 100644
--- a/django/contrib/admin/options.py
+++ b/django/contrib/admin/options.py
@@ -13,7 +13,6 @@ from django.contrib.admin.checks import (
     InlineModelAdminChecks,
     ModelAdminChecks,
 )
-from django.contrib.admin.decorators import display
 from django.contrib.admin.exceptions import DisallowedModelAdminToField
 from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
 from django.contrib.admin.utils import (
@@ -962,12 +961,16 @@ class ModelAdmin(BaseModelAdmin):
             action_flag=DELETION,
         )
 
-    @display(description=mark_safe('<input type="checkbox" id="action-toggle">'))
     def action_checkbox(self, obj):
         """
         A list_display column containing a checkbox widget.
         """
-        return helpers.checkbox.render(helpers.ACTION_CHECKBOX_NAME, str(obj.pk))
+        attrs = {
+            "class": "action-select",
+            "aria-label": format_html(_("Select this object for an action - {}"), obj),
+        }
+        checkbox = forms.CheckboxInput(attrs, lambda value: False)
+        return checkbox.render(helpers.ACTION_CHECKBOX_NAME, str(obj.pk))
 
     @staticmethod
     def _get_action_description(func, name):
diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py
index ee6f3a7666..761a64ae5f 100644
--- a/django/contrib/admin/templatetags/admin_list.py
+++ b/django/contrib/admin/templatetags/admin_list.py
@@ -96,8 +96,12 @@ def result_headers(cl):
 
             # if the field is the action checkbox: no sorting and special class
             if field_name == "action_checkbox":
+                aria_label = _("Select all objects on this page for an action")
                 yield {
-                    "text": text,
+                    "text": mark_safe(
+                        f'<input type="checkbox" id="action-toggle" '
+                        f'aria-label="{aria_label}">'
+                    ),
                     "class_attrib": mark_safe(' class="action-checkbox-column"'),
                     "sortable": False,
                 }
diff --git a/docs/releases/5.0.txt b/docs/releases/5.0.txt
index 87185ee7f0..3e14e17972 100644
--- a/docs/releases/5.0.txt
+++ b/docs/releases/5.0.txt
@@ -254,6 +254,8 @@ Miscellaneous
 * The ``instance`` argument of the undocumented
   ``BaseModelFormSet.save_existing()`` method is renamed to ``obj``.
 
+* The undocumented ``django.contrib.admin.helpers.checkbox`` is removed.
+
 .. _deprecated-features-5.0:
 
 Features deprecated in 5.0
diff --git a/js_tests/tests.html b/js_tests/tests.html
index c3c1055c9a..adeac66125 100644
--- a/js_tests/tests.html
+++ b/js_tests/tests.html
@@ -17,7 +17,7 @@
             <table id="result_list">
                 <tr>
                     <th>
-                       <input type="checkbox" id="action-toggle">
+                       <input type="checkbox" id="action-toggle" aria-label="Select all objects on this page for an action">
                     </th>
                 </tr>
                 <tr>
diff --git a/tests/admin_changelist/tests.py b/tests/admin_changelist/tests.py
index 413d6d4d7f..c603f15298 100644
--- a/tests/admin_changelist/tests.py
+++ b/tests/admin_changelist/tests.py
@@ -74,15 +74,15 @@ from .models import (
 )
 
 
-def build_tbody_html(pk, href, extra_fields):
+def build_tbody_html(obj, href, extra_fields):
     return (
         "<tbody><tr>"
         '<td class="action-checkbox">'
         '<input type="checkbox" name="_selected_action" value="{}" '
-        'class="action-select"></td>'
+        'class="action-select" aria-label="Select this object for an action - {}"></td>'
         '<th class="field-name"><a href="{}">name</a></th>'
         "{}</tr></tbody>"
-    ).format(pk, href, extra_fields)
+    ).format(obj.pk, str(obj), href, extra_fields)
 
 
 @override_settings(ROOT_URLCONF="admin_changelist.urls")
@@ -245,7 +245,7 @@ class ChangeListTests(TestCase):
         table_output = template.render(context)
         link = reverse("admin:admin_changelist_child_change", args=(new_child.id,))
         row_html = build_tbody_html(
-            new_child.id, link, '<td class="field-parent nowrap">-</td>'
+            new_child, link, '<td class="field-parent nowrap">-</td>'
         )
         self.assertNotEqual(
             table_output.find(row_html),
@@ -272,7 +272,7 @@ class ChangeListTests(TestCase):
         table_output = template.render(context)
         link = reverse("admin:admin_changelist_child_change", args=(new_child.id,))
         row_html = build_tbody_html(
-            new_child.id, link, '<td class="field-parent nowrap">???</td>'
+            new_child, link, '<td class="field-parent nowrap">???</td>'
         )
         self.assertNotEqual(
             table_output.find(row_html),
@@ -297,7 +297,7 @@ class ChangeListTests(TestCase):
         table_output = template.render(context)
         link = reverse("admin:admin_changelist_child_change", args=(new_child.id,))
         row_html = build_tbody_html(
-            new_child.id,
+            new_child,
             link,
             '<td class="field-age_display">&amp;dagger;</td>'
             '<td class="field-age">-empty-</td>',
@@ -327,13 +327,18 @@ class ChangeListTests(TestCase):
         table_output = template.render(context)
         link = reverse("admin:admin_changelist_child_change", args=(new_child.id,))
         row_html = build_tbody_html(
-            new_child.id, link, '<td class="field-parent nowrap">%s</td>' % new_parent
+            new_child, link, '<td class="field-parent nowrap">%s</td>' % new_parent
         )
         self.assertNotEqual(
             table_output.find(row_html),
             -1,
             "Failed to find expected row element: %s" % table_output,
         )
+        self.assertInHTML(
+            '<input type="checkbox" id="action-toggle" '
+            'aria-label="Select all objects on this page for an action">',
+            table_output,
+        )
 
     def test_result_list_editable_html(self):
         """