Skip to content

Commit

Permalink
Add setting to redirect to the preview url
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Jan 29, 2022
1 parent adcb5d8 commit 33a4c82
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
12 changes: 7 additions & 5 deletions wagtail_headless_preview/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import datetime
import json
import urllib

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.signing import TimestampSigner
from django.db import models
from django.shortcuts import redirect, render
from django.utils.http import urlencode


class PagePreview(models.Model):
Expand Down Expand Up @@ -87,9 +87,7 @@ def get_preview_url(self, token):
return (
self.get_client_root_url()
+ "?"
+ urllib.parse.urlencode(
{"content_type": self.get_content_type_str(), "token": token}
)
+ urlencode({"content_type": self.get_content_type_str(), "token": token})
)

def dummy_request(self, original_request=None, **meta):
Expand Down Expand Up @@ -118,11 +116,15 @@ def serve_preview(self, request, mode_name):
page_preview.save()

response_token = token or page_preview.token
preview_url = self.get_preview_url(response_token)

if getattr(settings, "HEADLESS_PREVIEW_REDIRECT", False):
return redirect(preview_url)

response = render(
request,
"wagtail_headless_preview/preview.html",
{"preview_url": self.get_preview_url(response_token)},
{"preview_url": preview_url},
)

if use_live_preview:
Expand Down
33 changes: 19 additions & 14 deletions wagtail_headless_preview/tests/test_frontend.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import urllib

from django.conf import settings
from django.contrib.auth.models import User
from django.test import TestCase, override_settings
from django.urls import reverse
from django.utils.http import urlencode

from wagtail.core.models import Page

Expand All @@ -29,9 +28,9 @@ def setUp(self):
self.page.title = "Simple page with draft edit"
self.page.save_revision()

def test_view(self):
self.client.login(username=self.admin_user.username, password="password")

def test_view(self):
self.assertEqual(PagePreview.objects.count(), 0)
# Try getting page draft
view_draft_url = reverse("wagtailadmin_pages:view_draft", args=(self.page.id,))
Expand All @@ -42,28 +41,34 @@ def test_view(self):
self.assertEqual(PagePreview.objects.count(), 1)

preview_token = PagePreview.objects.first().token
self.assertContains(response, urllib.parse.urlencode({"token": preview_token}))
self.assertContains(
response, urllib.parse.urlencode({"content_type": "testapp.simplepage"})
)
self.assertContains(response, urlencode({"token": preview_token}))
self.assertContains(response, urlencode({"content_type": "testapp.simplepage"}))

params = {
"content_type": "testapp.simplepage",
"token": preview_token,
"format": "json",
}
preview_api_url = "{base_url}{page_id}/?{params}".format(
preview_url = "{base_url}{page_id}/?{params}".format(
base_url=reverse("wagtailapi_v2:page_preview:listing"),
page_id=self.page.id,
params=urllib.parse.urlencode(params),
params=urlencode(params),
)

response = self.client.get(preview_api_url)
response = self.client.get(preview_url)
self.assertContains(response, "Simple page with draft edit")

# TODO fix this.
# response = self.client.get(self.page.get_preview_url(preview_token))
# self.assertContains(response, '<title>Headless preview</title>')
@override_settings(HEADLESS_PREVIEW_REDIRECT=True)
def test_redirect_on_preview(self):
view_draft_url = reverse("wagtailadmin_pages:view_draft", args=(self.page.id,))
response = self.client.get(view_draft_url)

preview_token = PagePreview.objects.first().token

self.assertRedirects(
response,
self.page.get_preview_url(preview_token),
fetch_redirect_response=False,
)


class TestHeadlessRedirectMixin(TestCase):
Expand Down

0 comments on commit 33a4c82

Please sign in to comment.