Dj1.11 - Fix rendering of group permission checkboxes

The format_permissions tag used to lay out the permission checkboxes
worked by iterating over the BoundField, which gave a sequence of
CheckboxChoiceInput objects. This class has been dropped in 1.11 and
superseded by BoundWidget, which has a different way of accessing the
checkbox value. Add separate code paths for the two cases.
This commit is contained in:
Matt Westcott 2017-02-28 15:02:33 +00:00 committed by Mikalai Radchuk
parent a98e0d9862
commit 313c2092bd
2 changed files with 23 additions and 16 deletions

View file

@ -2,6 +2,7 @@ from __future__ import absolute_import, unicode_literals
import itertools
import django
from django import template
from wagtail.wagtailcore import hooks
@ -29,8 +30,9 @@ def format_permissions(permission_bound_field):
(any_non_add_change_delete_permission, checkbox),
]
(where 'checkbox' is an instance of django.forms.widgets.CheckboxChoiceInput, renderable
as HTML using checkbox.tag() )
(where 'checkbox' is an object with a tag() method that renders the checkbox as HTML;
this is an instance of django.forms.widgets.CheckboxChoiceInput on Django <1.11,
and a BoundWidget on Django >=1.11)
- and returns a table template formatted with this list.
@ -41,10 +43,20 @@ def format_permissions(permission_bound_field):
# iterate over permission_bound_field to build a lookup of individual renderable
# checkbox objects
checkboxes_by_id = {
int(checkbox.choice_value): checkbox
for checkbox in permission_bound_field
}
if django.VERSION < (1, 11):
# On Django <1.11, iterating over the BoundField returns a sequence of CheckboxChoiceInput objects,
# whose ID is available as .choice_value
checkboxes_by_id = {
int(checkbox.choice_value): checkbox
for checkbox in permission_bound_field
}
else:
# On Django >=1.11, iterating over the BoundField returns a sequence of BoundWidget objects,
# whose ID is available as .data['value']
checkboxes_by_id = {
int(checkbox.data['value']): checkbox
for checkbox in permission_bound_field
}
object_perms = []
other_perms = []

View file

@ -756,16 +756,11 @@ class TestGroupEditView(TestCase, WagtailTestUtils):
# the checkbox for self.existing_permission should be ticked
response = self.get()
# Use BeautifulSoup to search for a checkbox element with name="permissions", checked="checked",
# value=<existing_permission.id>. Can't use assertContains here because the id attribute is unpredictable
soup = BeautifulSoup(response.content, 'html5lib')
self.assertTrue(
soup.find(
'input',
{'name': 'permissions', 'type': 'checkbox', 'checked': 'checked', 'value': self.existing_permission.id}
)
)
# use allow_extra_attrs because the input will also have an id (with an unpredictable value)
self.assertTagInHTML(
'<input name="permissions" type="checkbox" checked value="%s">' % self.existing_permission.id,
str(response.content),
allow_extra_attrs=True)
def test_group_edit_loads_with_page_permissions_shown(self):
# The test group has one page permission to begin with