From e771fefa5f540d8664826b39b0a0949965184985 Mon Sep 17 00:00:00 2001 From: Andy Babic Date: Sun, 22 Jan 2017 23:12:42 +0000 Subject: [PATCH] - Fix positional argument order when calling ModelAdmin.get_extra_class_names_for_field_col() and ModelAdmin.get_extra_attrs_for_field_col() from items_for_result() in template tags. - Added new tests to confirm the things are now correct. --- CHANGELOG.txt | 1 + docs/releases/1.9.rst | 1 + .../templatetags/modeladmin_tags.py | 12 ++++---- .../tests/test_simple_modeladmin.py | 30 +++++++++++++++++-- wagtail/tests/modeladmintest/models.py | 7 +++++ wagtail/tests/modeladmintest/wagtail_hooks.py | 25 +++++++++++++++- 6 files changed, 67 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index d3d71657e1..8fce434d5e 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -20,6 +20,7 @@ Changelog * Fix: Help text for StreamField is now visible and does not cover block controls (Stein Strindhaug) * Fix: "X minutes ago" timestamps are now marked for translation (Janneke Janssen, Matt Westcott) * Fix: Avoid indexing unsaved field content on `save(update_fields=[...])` operations (Matt Westcott) + * Fix: Corrected ordering of arguments passed to ModelAdmin `get_extra_class_names_for_field_col` / `get_extra_attrs_for_field_col` methods (Andy Babic) 1.8.1 (26.01.2017) diff --git a/docs/releases/1.9.rst b/docs/releases/1.9.rst index 0c2e9ee49c..5e843382b9 100644 --- a/docs/releases/1.9.rst +++ b/docs/releases/1.9.rst @@ -53,6 +53,7 @@ Bug fixes * Help text for StreamField is now visible and does not cover block controls (Stein Strindhaug) * "X minutes ago" timestamps are now marked for translation (Janneke Janssen, Matt Westcott) * Avoid indexing unsaved field content on `save(update_fields=[...])` operations (Matt Westcott) + * Corrected ordering of arguments passed to ModelAdmin ``get_extra_class_names_for_field_col`` / ``get_extra_attrs_for_field_col`` methods (Andy Babic) Upgrade considerations diff --git a/wagtail/contrib/modeladmin/templatetags/modeladmin_tags.py b/wagtail/contrib/modeladmin/templatetags/modeladmin_tags.py index c216a82940..4119bea364 100644 --- a/wagtail/contrib/modeladmin/templatetags/modeladmin_tags.py +++ b/wagtail/contrib/modeladmin/templatetags/modeladmin_tags.py @@ -71,12 +71,12 @@ def items_for_result(view, result): if force_text(result_repr) == '': result_repr = mark_safe(' ') row_classes.extend( - modeladmin.get_extra_class_names_for_field_col(field_name, result)) - row_attrs_dict = modeladmin.get_extra_attrs_for_field_col( - field_name, result) - row_attrs_dict['class'] = ' ' . join(row_classes) - row_attrs = flatatt(row_attrs_dict) - yield format_html('{}', row_attrs, result_repr) + modeladmin.get_extra_class_names_for_field_col(result, field_name) + ) + row_attrs = modeladmin.get_extra_attrs_for_field_col(result, field_name) + row_attrs['class'] = ' ' . join(row_classes) + row_attrs_flat = flatatt(row_attrs) + yield format_html('{}', row_attrs_flat, result_repr) def results(view, object_list): diff --git a/wagtail/contrib/modeladmin/tests/test_simple_modeladmin.py b/wagtail/contrib/modeladmin/tests/test_simple_modeladmin.py index a2dd97db00..0e75ca0236 100644 --- a/wagtail/contrib/modeladmin/tests/test_simple_modeladmin.py +++ b/wagtail/contrib/modeladmin/tests/test_simple_modeladmin.py @@ -10,7 +10,7 @@ from wagtail.wagtailimages.models import Image from wagtail.wagtailimages.tests.utils import get_test_image_file -class TestIndexView(TestCase, WagtailTestUtils): +class TestBookIndexView(TestCase, WagtailTestUtils): fixtures = ['modeladmintest_test.json'] def setUp(self): @@ -47,7 +47,7 @@ class TestIndexView(TestCase, WagtailTestUtils): self.assertContains(response, 'data-object-pk="3"') # There should be two odd rows and two even ones, and 'book' should be - # add to the `class` attribute for every one. + # added to the `class` attribute for every one. self.assertContains(response, 'class="book odd"', count=2) self.assertContains(response, 'class="book even"', count=2) @@ -98,6 +98,32 @@ class TestIndexView(TestCase, WagtailTestUtils): self.assertEqual(response.context['result_count'], 4) +class TestAuthorIndexView(TestCase, WagtailTestUtils): + fixtures = ['modeladmintest_test.json'] + + def setUp(self): + self.login() + + def get(self, **params): + return self.client.get('/admin/modeladmintest/author/', params) + + def test_col_extra_class_names(self): + response = self.get() + self.assertEqual(response.status_code, 200) + test_html = """ + The Lord of the Rings + """ + self.assertContains(response, test_html, html=True) + + def test_col_extra_attributes(self): + response = self.get() + self.assertEqual(response.status_code, 200) + test_html = """ + The Hobbit + """ + self.assertContains(response, test_html, html=True) + + class TestCreateView(TestCase, WagtailTestUtils): fixtures = ['modeladmintest_test.json'] diff --git a/wagtail/tests/modeladmintest/models.py b/wagtail/tests/modeladmintest/models.py index b47b587489..3ce685adce 100644 --- a/wagtail/tests/modeladmintest/models.py +++ b/wagtail/tests/modeladmintest/models.py @@ -15,6 +15,13 @@ class Author(models.Model): def __str__(self): return self.name + def first_book(self): + # For testing use of object methods in list_display + book = self.book_set.first() + if book: + return book.title + return '' + @python_2_unicode_compatible class Book(models.Model, index.Indexed): diff --git a/wagtail/tests/modeladmintest/wagtail_hooks.py b/wagtail/tests/modeladmintest/wagtail_hooks.py index 384d6c0004..569189224c 100644 --- a/wagtail/tests/modeladmintest/wagtail_hooks.py +++ b/wagtail/tests/modeladmintest/wagtail_hooks.py @@ -12,12 +12,35 @@ from .models import Author, Book, Publisher, Token, VenuePage class AuthorModelAdmin(ModelAdmin): model = Author menu_order = 200 - list_display = ('name', 'date_of_birth') + list_display = ('name', 'first_book', 'last_book', 'date_of_birth') list_filter = ('date_of_birth', ) search_fields = ('name', ) inspect_view_enabled = True inspect_view_fields = ('name', ) + def last_book(self, obj): + # For testing use of modeladmin methods in list_display + book = obj.book_set.last() + if book: + return book.title + return '' + + def get_extra_class_names_for_field_col(self, obj, field_name): + class_names = super( + AuthorModelAdmin, self + ).get_extra_class_names_for_field_col(field_name, obj) + if field_name == 'first_book': + class_names.append('for-author-%s' % obj.pk) + return class_names + + def get_extra_attrs_for_field_col(self, obj, field_name): + attrs = super(AuthorModelAdmin, self).get_extra_attrs_for_field_col( + field_name, obj + ) + if field_name == 'last_book': + attrs['data-for_author'] = obj.id + return attrs + class BookModelAdmin(ThumbnailMixin, ModelAdmin): model = Book