From 443503d1450e628de24943822799a34b1c3ee92c Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 12 Apr 2018 17:23:13 +0100 Subject: [PATCH] Fix non-model form fields being compared. Fixes #3737 --- wagtail/admin/edit_handlers.py | 5 +- wagtail/admin/tests/test_pages_views.py | 69 ++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/wagtail/admin/edit_handlers.py b/wagtail/admin/edit_handlers.py index a28d21c38a..b152ed193f 100644 --- a/wagtail/admin/edit_handlers.py +++ b/wagtail/admin/edit_handlers.py @@ -508,7 +508,10 @@ class FieldPanel(EditHandler): comparator_class = self.get_comparison_class() if comparator_class: - return [curry(comparator_class, self.db_field)] + try: + return [curry(comparator_class, self.db_field)] + except FieldDoesNotExist: + return [] return [] @cached_property diff --git a/wagtail/admin/tests/test_pages_views.py b/wagtail/admin/tests/test_pages_views.py index 5699038fb0..4ff87af403 100644 --- a/wagtail/admin/tests/test_pages_views.py +++ b/wagtail/admin/tests/test_pages_views.py @@ -27,8 +27,8 @@ from wagtail.search.index import SearchField from wagtail.tests.testapp.models import ( EVENT_AUDIENCE_CHOICES, Advert, AdvertPlacement, BusinessChild, BusinessIndex, BusinessSubIndex, DefaultStreamPage, EventCategory, EventPage, EventPageCarouselItem, FilePage, - ManyToManyBlogPage, SimplePage, SingleEventPage, SingletonPage, StandardChild, StandardIndex, - TaggedPage) + FormClassAdditionalFieldPage, ManyToManyBlogPage, SimplePage, SingleEventPage, SingletonPage, + StandardChild, StandardIndex, TaggedPage) from wagtail.tests.utils import WagtailTestUtils from wagtail.users.models import UserProfile @@ -4052,6 +4052,71 @@ class TestCompareRevisions(TestCase, WagtailTestUtils): self.assertContains(response, 'Last Christmas I gave you my heart, but the very next day you gave it awayThis year, to save me from tears, I'll just feed it to the dog') +class TestCompareRevisionsWithNonModelField(TestCase, WagtailTestUtils): + """ + Tests if form fields defined in the base_form_class will not be included. + in revisions view as they are not actually on the model. + Flagged in issue #3737 + Note: Actual tests for comparison classes can be found in test_compare.py + """ + + fixtures = ['test.json'] + # FormClassAdditionalFieldPage + + def setUp(self): + # Find root page + self.root_page = Page.objects.get(id=2) + + # Add child page of class with base_form_class override + # non model field is 'code' + self.test_page = FormClassAdditionalFieldPage( + title='A Statement', + slug='a-statement', + location='Early Morning Cafe, Mainland, NZ', + body="

hello

" + ) + self.root_page.add_child(instance=self.test_page) + + # add new revision + self.test_page.title = 'Statement' + self.test_page.location = 'Victory Monument, Bangkok' + self.test_page.body = ( + "

I would like very much to go into the forrest.

" + ) + self.test_page_revision = self.test_page.save_revision() + self.test_page_revision.created_at = local_datetime(2017, 10, 15) + self.test_page_revision.save() + + # add another new revision + self.test_page.title = 'True Statement' + self.test_page.location = 'Victory Monument, Bangkok' + self.test_page.body = ( + "

I would like very much to go into the forest.

" + ) + self.test_page_revision_new = self.test_page.save_revision() + self.test_page_revision_new.created_at = local_datetime(2017, 10, 16) + self.test_page_revision_new.save() + + self.login() + + def test_base_form_class_used(self): + """First ensure that the non-model field is appearing in edit.""" + edit_url = reverse('wagtailadmin_pages:add', args=('tests', 'formclassadditionalfieldpage', self.test_page.id)) + response = self.client.get(edit_url) + self.assertContains(response, '', html=True) + + def test_compare_revisions(self): + """Confirm that the non-model field is not shown in revision.""" + compare_url = reverse( + 'wagtailadmin_pages:revisions_compare', + args=(self.test_page.id, self.test_page_revision.id, self.test_page_revision_new.id) + ) + response = self.client.get(compare_url) + self.assertContains(response, 'forrest.forest.') + # should not contain the field defined in the formclass used + self.assertNotContains(response, '

Code:

') + + class TestRevisionsUnschedule(TestCase, WagtailTestUtils): fixtures = ['test.json']