From 15419f4d0e405141e5388f1909e86acca872b0ed Mon Sep 17 00:00:00 2001 From: Tim Heap Date: Thu, 21 Aug 2014 10:08:29 +1000 Subject: [PATCH] Make RoutablePage a mixin If a developer wanted to have a site-wide base page class, and also have some pages be `RoutablePage`s, a conflict between the automatically generated `page_ptr` fields would occur. ```python from wagtail.wagtailcore.models import Page from wagtail.contrib.wagtailroutablepage.models import RoutablePage class SitePageBase(Page): # common functionality is_abstract = True class Meta: abstract = True class MyPage(RoutablePage, SitePageBase): # This model is invalid pass ``` `RoutablePage` has been changed to be a mixin `RoutablePageMixin`. Page classes can use this to gain the `RoutablePage` functionality while still retaining the ability to subclass other models. A `RoutablePage` class that derives from both `RoutablePageMixin` and `Page` has been left in for backwards compatibility, so old code will continue to function without any modifications. --- ...table_page.rst => routable_page_mixin.rst} | 23 +++++++++++-------- docs/core_components/pages/index.rst | 2 +- docs/releases/0.5.rst | 4 ++-- wagtail/contrib/wagtailroutablepage/models.py | 14 ++++++++--- 4 files changed, 27 insertions(+), 16 deletions(-) rename docs/core_components/pages/advanced_topics/{routable_page.rst => routable_page_mixin.rst} (66%) diff --git a/docs/core_components/pages/advanced_topics/routable_page.rst b/docs/core_components/pages/advanced_topics/routable_page_mixin.rst similarity index 66% rename from docs/core_components/pages/advanced_topics/routable_page.rst rename to docs/core_components/pages/advanced_topics/routable_page_mixin.rst index c3e2625c9..36ca024c3 100644 --- a/docs/core_components/pages/advanced_topics/routable_page.rst +++ b/docs/core_components/pages/advanced_topics/routable_page_mixin.rst @@ -1,4 +1,4 @@ -.. _routable_page: +.. _routable_page_mixin: ==================================== Embedding URL configuration in Pages @@ -6,15 +6,15 @@ Embedding URL configuration in Pages .. versionadded:: 0.5 -The ``RoutablePage`` class provides a convenient way for a page to respond on multiple sub-URLs with different views. For example, a blog section on a site might provide several different types of index page at URLs like ``/blog/2013/06/``, ``/blog/authors/bob/``, ``/blog/tagged/python/``, all served by the same ``BlogIndex`` page. +The ``RoutablePageMixin`` mixin provides a convenient way for a page to respond on multiple sub-URLs with different views. For example, a blog section on a site might provide several different types of index page at URLs like ``/blog/2013/06/``, ``/blog/authors/bob/``, ``/blog/tagged/python/``, all served by the same ``BlogIndex`` page. -A ``RoutablePage`` exists within the page tree like any other page, but URL paths underneath it are checked against a list of patterns, using Django's urlconf scheme. If none of the patterns match, control is passed to subpages as usual (or failing that, a 404 error is thrown). +A ``Page`` using ``RoutablePageMixin`` exists within the page tree like any other page, but URL paths underneath it are checked against a list of patterns, using Django's urlconf scheme. If none of the patterns match, control is passed to subpages as usual (or failing that, a 404 error is thrown). The basics ========== -To use ``RoutablePage``, you need to make your class inherit from :class:`wagtail.contrib.wagtailroutablepage.models.RoutablePage` and configure the ``subpage_urls`` attribute with your URL configuration. +To use ``RoutablePageMixin``, you need to make your class inherit from both :class:`wagtail.contrib.wagtailroutablepage.models.RoutablePageMixin` and :class:`wagtail.wagtailcore.models.Page`, and configure the ``subpage_urls`` attribute with your URL configuration. Here's an example of an ``EventPage`` with three views: @@ -22,10 +22,11 @@ Here's an example of an ``EventPage`` with three views: from django.conf.urls import url - from wagtail.contrib.wagtailroutablepage.models import RoutablePage + from wagtail.contrib.wagtailroutablepage.models import RoutablePageMixin + from wagtail.wagtailcore.models import Page - class EventPage(RoutablePage): + class EventPage(RoutablePageMixin, Page): subpage_urls = ( url(r'^$', 'current_events', name='current_events'), url(r'^past/$', 'past_events', name='past_events'), @@ -51,11 +52,11 @@ Here's an example of an ``EventPage`` with three views: ... -The ``RoutablePage`` class -========================== +The ``RoutablePageMixin`` class +=============================== .. automodule:: wagtail.contrib.wagtailroutablepage.models -.. autoclass:: RoutablePage +.. autoclass:: RoutablePageMixin .. autoattribute:: subpage_urls @@ -65,7 +66,9 @@ The ``RoutablePage`` class from django.conf.urls import url - class MyPage(RoutablePage): + from wagtail.wagtailcore.models import Page + + class MyPage(RoutablePageMixin, Page): subpage_urls = ( url(r'^$', 'serve', name='main'), url(r'^archive/$', 'archive', name='archive'), diff --git a/docs/core_components/pages/index.rst b/docs/core_components/pages/index.rst index 1f350f35c..70d20d116 100644 --- a/docs/core_components/pages/index.rst +++ b/docs/core_components/pages/index.rst @@ -21,4 +21,4 @@ The presentation of your content, the actual webpages, includes the normal use o editing_api advanced_topics/queryset_methods advanced_topics/private_pages - advanced_topics/routable_page + advanced_topics/routable_page_mixin diff --git a/docs/releases/0.5.rst b/docs/releases/0.5.rst index 1e440e27f..6f4912de5 100644 --- a/docs/releases/0.5.rst +++ b/docs/releases/0.5.rst @@ -37,7 +37,7 @@ RoutablePage A ``RoutablePage`` model has been added to allow embedding Django-style URL routing within a page. -:ref:`routable_page` +:ref:`routable_page_mixin` Usage stats for images, documents and snippets @@ -129,4 +129,4 @@ South upgraded to 1.0 In preparation for Django 1.7 support in a future release, Wagtail now depends on South 1.0, and its migration files have been moved from ``migrations`` to ``south_migrations``. Older versions of South will fail to find the migrations in the new location. -If your project's requirements file (most commonly requirements.txt or requirements/base.txt) references a specific older version of South, this must be updated to South 1.0. \ No newline at end of file +If your project's requirements file (most commonly requirements.txt or requirements/base.txt) references a specific older version of South, this must be updated to South 1.0. diff --git a/wagtail/contrib/wagtailroutablepage/models.py b/wagtail/contrib/wagtailroutablepage/models.py index 926e5f48a..88379b37a 100644 --- a/wagtail/contrib/wagtailroutablepage/models.py +++ b/wagtail/contrib/wagtailroutablepage/models.py @@ -8,9 +8,10 @@ from wagtail.wagtailcore.models import Page from wagtail.wagtailcore.url_routing import RouteResult -class RoutablePage(Page): +class RoutablePageMixin(object): """ - This class extends Page by adding methods to allow urlconfs to be embedded inside pages + This class can be mixed in to a Page subclass to allow urlconfs to be + embedded inside pages. """ #: Set this to a tuple of ``django.conf.urls.url`` objects. subpage_urls = None @@ -59,7 +60,7 @@ class RoutablePage(Page): except Http404: pass - return super(RoutablePage, self).route(request, path_components) + return super(RoutablePageMixin, self).route(request, path_components) def serve(self, request, view, args, kwargs): return view(request, *args, **kwargs) @@ -68,6 +69,13 @@ class RoutablePage(Page): view, args, kwargs = self.resolve_subpage('/') return view(*args, **kwargs) + +class RoutablePage(RoutablePageMixin, Page): + """ + This class extends Page by adding methods to allow urlconfs + to be embedded inside pages + """ + is_abstract = True class Meta: