wagtail-nest / wagtail-bakery

A set of helpers for baking your Django Wagtail site out as flat files.
MIT License
182 stars 40 forks source link

support for subpages #29

Open jorenham opened 6 years ago

jorenham commented 6 years ago

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

robmoorman commented 6 years ago

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 commented 6 years ago

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

jorenham commented 6 years ago

@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 commented 6 years ago

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
robmoorman commented 6 years ago

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

smulloni commented 3 years ago

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