0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Use the live database record as the result of get_latest_revision_as_page if there are no draft edits.

This ensures that the live database state will be reflected in the page editor, which provides two benefits:
* any changes made directly at the model / database level (e.g. automated data imports) will be visible in the editor
* inline child objects will be associated with their actual database IDs even if this information is missing from the revision record. This ensures that their IDs will be preserved on next publish, rather than the records being deleted and recreated (#1853)
This commit is contained in:
Matt Westcott 2015-10-29 17:16:07 +00:00
parent 26857f525d
commit bd6a7c768f
4 changed files with 49 additions and 1 deletions

View File

@ -118,6 +118,11 @@ class RelatedLink(LinkFields):
class SimplePage(Page):
content = models.TextField()
content_panels = [
FieldPanel('title', classname="full title"),
FieldPanel('content'),
]
class PageWithOldStyleRouteMethod(Page):
"""

View File

@ -900,6 +900,33 @@ class TestPageEdit(TestCase, WagtailTestUtils):
self.assertTemplateUsed(response, 'tests/simple_page.html')
self.assertEqual(response.context['request'].site.hostname, 'childpage.example.com')
def test_editor_picks_up_direct_model_edits(self):
# If a page has no draft edits, the editor should show the version from the live database
# record rather than the latest revision record. This ensures that the edit interface
# reflects any changes made directly on the model.
self.child_page.title = "This title only exists on the live database record"
self.child_page.save()
response = self.client.get(reverse('wagtailadmin_pages:edit', args=(self.child_page.id, )))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "This title only exists on the live database record")
def test_editor_does_not_pick_up_direct_model_edits_when_draft_edits_exist(self):
# If a page has draft edits, we should always show those in the editor, not the live
# database record
self.child_page.content = "Some content with a draft edit"
self.child_page.save_revision()
# make an independent change to the live database record
self.child_page = SimplePage.objects.get(id=self.child_page.id)
self.child_page.title = "This title only exists on the live database record"
self.child_page.save()
response = self.client.get(reverse('wagtailadmin_pages:edit', args=(self.child_page.id, )))
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, "This title only exists on the live database record")
self.assertContains(response, "Some content with a draft edit")
class TestPageEditReordering(TestCase, WagtailTestUtils):
def setUp(self):

View File

@ -474,6 +474,15 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed
return self.revisions.order_by('-created_at', '-id').first()
def get_latest_revision_as_page(self):
if not self.has_unpublished_changes:
# Use the live database copy in preference to the revision record, as:
# 1) this will pick up any changes that have been made directly to the model,
# such as automated data imports;
# 2) it ensures that inline child objects pick up real database IDs even if
# those are absent from the revision data. (If this wasn't the case, the child
# objects would be recreated with new IDs on next publish - see #1853)
return self.specific
latest_revision = self.get_latest_revision()
if latest_revision:
@ -826,7 +835,7 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed
# Create a new revision
# This code serves a few purposes:
# * It makes sure update_attrs gets applied to the latest revision so the changes are reflected in the editor
# * It makes sure update_attrs gets applied to the latest revision
# * It bumps the last_revision_created_at value so the new page gets ordered as if it was just created
# * It sets the user of the new revision so it's possible to see who copied the page by looking at its history
latest_revision = page_copy.get_latest_revision_as_page()

View File

@ -447,6 +447,13 @@ class TestCopyPage(TestCase):
self.assertEqual(latest_revision.title, "New christmas event")
self.assertEqual(latest_revision.slug, 'new-christmas-event')
# get_latest_revision_as_page might bypass the revisions table if it determines
# that there are no draft edits since publish - so retrieve it explicitly from the
# revision data, to ensure it's been updated there too
latest_revision = new_christmas_event.get_latest_revision().as_page_object()
self.assertEqual(latest_revision.title, "New christmas event")
self.assertEqual(latest_revision.slug, 'new-christmas-event')
# Check that the ids within the revision were updated correctly
new_revision = new_christmas_event.revisions.first()
new_revision_content = json.loads(new_revision.content_json)