wagtail-bakery icon indicating copy to clipboard operation
wagtail-bakery copied to clipboard

support for subpages

Open jorenham opened this issue 7 years ago • 6 comments

http://docs.wagtail.io/en/stable/reference/contrib/routablepage.html Perhaps using a setting to enable this.

jorenham avatar Mar 02 '18 21:03 jorenham

Thanks for your suggestions @jorenham . First thought will be override the get_static_site_paths method.

You can use this already to make your custom page render multiple static html files. However we want to provide people with off-the-shelf solutions to make it much easier as you suggest.

Could you let me know if this works (for now) for you?

robmoorman avatar Mar 05 '18 11:03 robmoorman

See https://github.com/moorinteractive/wagtail-bakery/blob/master/tests/models.py#L26 for an example

robmoorman avatar Mar 05 '18 11:03 robmoorman

@robmoorman The get_static_site_paths solution does not seem to do the trick. Adding a print statement to it reveals it does not get called when building.

jorenham avatar Mar 05 '18 13:03 jorenham

I solved it. Just add the attribute subpage_urls = ['some_sub_page/'] to your routable page models and import of the following views instead of 'wagtailbakery.views.AllPagesView'.

import logging

import os
from django.test.client import RequestFactory
from wagtailbakery.views import AllPagesView, AllPublishedPagesView

logger = logging.getLogger(__name__)


class AllSubpagesView(AllPagesView):
    def build_subpages(self, obj):
        site = obj.get_site()
        base_url = self.get_url(obj)

        for subpage_url in obj.subpage_urls:
            url = base_url + subpage_url
            logger.debug("Building %s" % url)
            self.request = RequestFactory(SERVER_NAME=site.hostname).get(url)
            self.set_kwargs(obj)

            build_path = self.get_subpage_build_path(obj, subpage_url)
            self.build_file(build_path, self.get_content(obj))

    def get_subpage_build_path(self, obj, subpage_url):
        base_path = os.path.dirname(self.get_build_path(obj))
        path = os.path.join(base_path, subpage_url)

        os.path.exists(path) or os.makedirs(path)

        return os.path.join(path, 'index.html')

    def build_queryset(self):
        super(AllSubpagesView, self).build_queryset()

        for item in self.get_queryset().all():
            specific_item = item.specific
            if hasattr(specific_item, 'subpage_urls'):
                self.build_subpages(specific_item)


class AllPublishedSubpagesView(AllSubpagesView, AllPublishedPagesView):
    pass

jorenham avatar Mar 18 '18 15:03 jorenham

That's awesome @jorenham . I'll keep your implementation as an example till we have supported this.

robmoorman avatar Mar 20 '18 07:03 robmoorman

This or something like it would be great to add to wagtail-bakery itself. Any blockers?

smulloni avatar Jun 07 '21 21:06 smulloni