Fix #3639, KeyError on Preview Link (#3663)

If the session key does not exist, fall back to the preview error.  The user can then try previewing the article again.
pull/3694/head
Paul Kamp 2017-06-19 08:48:46 -07:00 zatwierdzone przez Matt Westcott
rodzic 5f651299df
commit b92b862537
2 zmienionych plików z 27 dodań i 0 usunięć

Wyświetl plik

@ -1550,6 +1550,29 @@ class TestPageEdit(TestCase, WagtailTestUtils):
self.assertTemplateUsed(response, 'tests/simple_page.html')
self.assertContains(response, "I've been edited!")
def test_preview_on_edit_no_session_key(self):
preview_url = reverse('wagtailadmin_pages:preview_on_edit',
args=(self.child_page.id,))
# get() without corresponding post(), key not set.
response = self.client.get(preview_url)
# Check the HTML response
self.assertEqual(response.status_code, 200)
# We should have an error page because we are unable to
# preview; the page key was not in the session.
self.assertContains(
response,
"<title>Wagtail - Preview error</title>",
html=True
)
self.assertContains(
response,
"<h1>Preview error</h1>",
html=True
)
@modify_settings(ALLOWED_HOSTS={'append': 'childpage.example.com'})
def test_preview_uses_correct_site(self):
# create a Site record for the child page

Wyświetl plik

@ -573,6 +573,10 @@ class PreviewOnEdit(View):
page = self.get_page()
form_class = page.get_edit_handler().get_form_class(page._meta.model)
parent_page = page.get_parent().specific
if self.session_key not in self.request.session:
# Session key not in session, returning null form
return form_class(instance=page, parent_page=parent_page)
post_data_dict, timestamp = self.request.session[self.session_key]
# convert post_data_dict back into a QueryDict