Browse Source

Fix regression in #3988 where no revision records exist - fixes #4505

Matt Westcott 7 years ago
parent
commit
cd3b676c44
2 changed files with 20 additions and 5 deletions
  1. 14 0
      wagtail/admin/tests/test_pages_views.py
  2. 6 5
      wagtail/admin/views/pages.py

+ 14 - 0
wagtail/admin/tests/test_pages_views.py

@@ -1225,6 +1225,15 @@ class TestPageEdit(TestCase, WagtailTestUtils):
         )
         self.root_page.add_child(instance=self.single_event_page)
 
+        self.unpublished_page = SimplePage(
+            title="Hello unpublished world!",
+            slug="hello-unpublished-world",
+            content="hello",
+            live=False,
+            has_unpublished_changes=True,
+        )
+        self.root_page.add_child(instance=self.unpublished_page)
+
         # Login
         self.user = self.login()
 
@@ -1237,6 +1246,11 @@ class TestPageEdit(TestCase, WagtailTestUtils):
         self.assertContains(response, '<legend>Speaker lineup</legend>')
         self.assertContains(response, 'Add speakers')
 
+    def test_edit_draft_page_with_no_revisions(self):
+        # Tests that the edit page loads
+        response = self.client.get(reverse('wagtailadmin_pages:edit', args=(self.unpublished_page.id, )))
+        self.assertEqual(response.status_code, 200)
+
     def test_edit_multipart(self):
         """
         Test checks if 'enctype="multipart/form-data"' is added and only to forms that require multipart encoding.

+ 6 - 5
wagtail/admin/views/pages.py

@@ -308,8 +308,9 @@ def create(request, content_type_app_name, content_type_model_name, parent_page_
 
 
 def edit(request, page_id):
-    latest_revision = get_object_or_404(Page, id=page_id).get_latest_revision()
-    page = get_object_or_404(Page, id=page_id).get_latest_revision_as_page()
+    real_page_record = get_object_or_404(Page, id=page_id)
+    latest_revision = real_page_record.get_latest_revision()
+    page = real_page_record.get_latest_revision_as_page()
     parent = page.get_parent()
 
     content_type = ContentType.objects.get_for_model(page)
@@ -515,9 +516,9 @@ def edit(request, page_id):
 
         messages.warning(request, _("This page is currently awaiting moderation"), buttons=buttons)
 
-    # Page status needs to present the version of the page containing the correct live URL
-    if page.has_unpublished_changes:
-        page_for_status = latest_revision.page.specific
+    if page.live and page.has_unpublished_changes:
+        # Page status needs to present the version of the page containing the correct live URL
+        page_for_status = real_page_record.specific
     else:
         page_for_status = page