Skip to content

Commit

Permalink
Fixed #34045 -- Improved accessibility of selecting items in admin ch…
Browse files Browse the repository at this point in the history
…angelist.

This adds "aria-label".
  • Loading branch information
Durval Carvalho authored and felixxm committed Feb 16, 2023
1 parent 6bdc3c5 commit 85366fb
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ answer newbie questions, and generally made Django that much better:
Doug Beck <[email protected]>
Doug Napoleone <[email protected]>
dready <[email protected]>
Durval Carvalho de Souza <[email protected]>
[email protected]
Dustyn Gibson <[email protected]>
Ed Morley <https://github.com/edmorley>
Expand Down
3 changes: 0 additions & 3 deletions django/contrib/admin/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ class ActionForm(forms.Form):
)


checkbox = forms.CheckboxInput({"class": "action-select"}, lambda value: False)


class AdminForm:
def __init__(
self,
Expand Down
9 changes: 6 additions & 3 deletions django/contrib/admin/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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 (
Expand Down Expand Up @@ -962,12 +961,16 @@ def log_deletion(self, request, obj, object_repr):
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):
Expand Down
6 changes: 5 additions & 1 deletion django/contrib/admin/templatetags/admin_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 2 additions & 0 deletions docs/releases/5.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion js_tests/tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
19 changes: 12 additions & 7 deletions tests/admin_changelist/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@
)


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")
Expand Down Expand Up @@ -245,7 +245,7 @@ def test_result_list_empty_changelist_value(self):
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),
Expand All @@ -272,7 +272,7 @@ def test_result_list_set_empty_value_display_on_admin_site(self):
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),
Expand All @@ -297,7 +297,7 @@ def test_result_list_set_empty_value_display_in_model_admin(self):
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>',
Expand Down Expand Up @@ -327,13 +327,18 @@ def test_result_list_html(self):
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):
"""
Expand Down

0 comments on commit 85366fb

Please sign in to comment.