Last active
September 21, 2019 01:20
-
-
Save kaedroho/10296244 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import Http404 | |
from django.core.urlresolvers import RegexURLResolver | |
from django.conf.urls import url | |
from wagtail.wagtailcore.models import Page | |
class SuperPage(Page): | |
""" | |
This class extends Page by adding methods to allow urlconfs to be embedded inside pages | |
""" | |
def get_subpage_urls(self): | |
""" | |
Override this method to add your own subpage urls | |
""" | |
return [ | |
url('^$', self.serve, name='main'), | |
] | |
def reverse_subpage(self, name, *args, **kwargs): | |
""" | |
This method does the same job as Djangos' built in "urlresolvers.reverse()" function for subpage urlconfs. | |
""" | |
resolver = RegexURLResolver(r'^', self.get_subpage_urls()) | |
return self.url + resolver.reverse(name, *args, **kwargs) | |
def resolve_subpage(self, path): | |
""" | |
This finds a view method/function from a URL path. | |
""" | |
resolver = RegexURLResolver(r'^', self.get_subpage_urls()) | |
return resolver.resolve(path) | |
def route(self, request, path_components): | |
""" | |
This hooks the subpage urls into Wagtails routing. | |
""" | |
if self.live: | |
try: | |
path = '/'.join(path_components) | |
if path: | |
path += '/' | |
view, args, kwargs = self.resolve_subpage(path) | |
return view(request, *args, **kwargs) | |
except Http404: | |
pass | |
return super(SuperPage, self).route(request, path_components) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I did a super-rough update of this to use the new
RouteResult
class. I don't know how hacky the override ofserve()
is or if it makes sense as an abstract class, but feel free to use what I've added if you wanted to try again to put this into wagtail.contrib. ¯_(ツ)_/¯With this consuming it: