Skip to content
This repository has been archived by the owner on Dec 9, 2021. It is now read-only.

Commit

Permalink
22: Address issue with wagtail-bakery where SECURE_SSL_REDIRECT=True …
Browse files Browse the repository at this point in the history
…bakes out empty HTML

This commit subclasses wagtail-bakery's `AllPublishedPagesView` in a way that detects
application-level SSL redirection in order to avoid an issue where rendered pages end up
being 0 bytes.

See wagtail-nest/wagtail-bakery#24 for confirmation of the
issue and the discussion on wagtail-nest/wagtail-bakery#25
that points to a custom view being the (current) workaround. Ideally we'll be
able to replace this when that issue is resolved.

The code in this commit is basically taken from that closed PR, which adds the
`secure_request` variable. Hat-tip to @loicteixeira - thanks!

No unit test added, but manually tested locally to confirm this does indeed fix the
static build while `SECURE_SSL_REDIRECT` is `True`
  • Loading branch information
Steve Jalim committed Sep 23, 2019
1 parent 98a9c3c commit 0ed1bfc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
39 changes: 39 additions & 0 deletions developerportal/apps/bakery/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import logging

from django.conf import settings
from django.test.client import RequestFactory

from wagtailbakery.views import AllPublishedPagesView

logger = logging.getLogger(__name__)


class AllPublishedPagesViewAllowingSecureRedirect(AllPublishedPagesView):
"""Extension of `AllPublishedPagesView` that detects application-level SSL
redirection in order to avoid an issue where rendered pages end up being 0 bytes
See https://github.com/wagtail/wagtail-bakery/issues/24 for confirmation of the
issue and the discussion on https://github.com/wagtail/wagtail-bakery/pull/25
that points to a custom view being the (current) workaround. Ideally we'll be
able to replace this when that issue is resolved.
The following code is taken from that closed PR, which adds the `secure_request`
variable.
"""

def build_object(self, obj):
"""
Build wagtail page and set SERVER_NAME to retrieve corresponding site
object.
"""
site = obj.get_site()
logger.debug("Building %s" % obj)
secure_request = site.port == 443 or getattr(
settings, "SECURE_SSL_REDIRECT", False
)
self.request = RequestFactory(SERVER_NAME=site.hostname).get(
self.get_url(obj), secure=secure_request
)
self.set_kwargs(obj)
path = self.get_build_path(obj)
self.build_file(path, self.get_content(obj))
4 changes: 3 additions & 1 deletion developerportal/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@
# Wagtail Bakery Settings
BUILD_DIR = os.path.join(BASE_DIR, "build")
BAKERY_MULTISITE = True
BAKERY_VIEWS = ("wagtailbakery.views.AllPublishedPagesView",)
BAKERY_VIEWS = (
"developerportal.apps.bakery.views.AllPublishedPagesViewAllowingSecureRedirect",
)
AWS_REGION = os.environ.get("AWS_REGION")
AWS_BUCKET_NAME = os.environ.get("AWS_BUCKET_NAME")

Expand Down

0 comments on commit 0ed1bfc

Please sign in to comment.