Package: wagtail-sharing
wagtail-sharingEasier sharing ofWagtaildrafts.Wagtail Sharing makes it easier to share Wagtail draft content for review by users who don’t have access to the Wagtail admin site. It allows you to define an alternate hostname and/or port on which to expose the latest revision of all of your Wagtail pages.For example, let’s say your Wagtail site is running onhttp://mysite.com. You’ve created a draft page at slug/path/to/draft, but haven’t yet published it. Wagtail Sharing lets you expose that draft page at some other domain, for examplehttp://sharing.mysite.com/path/to/draft.In another use case, you might have a published page athttp://mysite.com/already/published/page, and you’ve made some draft changes. Wagtail Sharing lets you expose those draft changes athttp://sharing.mysite.com/already/published/pagewhile still keeping the same published content at your regular domain.These examples obviously work best when you have some method of restricting access tohttp://sharing.mysite.com, for example by only exposing that subdomain on a private network.Wagtail Sharing lets you create separate sharing sites for each Wagtail Site you have defined. It also supports a configurable visual banner on shared pages to remind reviewers that content may differ from your published site.This new logic only applies toGETrequests. Other HTTP methods likePOSTdefer to standard Wagtail handling.SetupInstall the package using pip:$pipinstallwagtail-sharingAddwagtailsharingas an installed app in your Django settings:# in settings.pyINSTALLED_APPS=(...'wagtailsharing',...)wagtail.snippetsis also required and must be included in your list of installed apps.The code examples below assume that you are using a recent Wagtail version (3.0+).Run migrations to create required database tables:$pythonmanage.pymigratewagtailsharingReplace use of Wagtail’s catch-all URL pattern:# in urls.py-from wagtail import urls as wagtail_urls+from wagtailsharing import urls as wagtailsharing_urls...-urlpatterns.append(url(r'', include(wagtail_urls)))+urlpatterns.append(url(r'', include(wagtailsharing_urls)))Sharing sitesThe Wagtail admin now contains a new section under Settings called Sharing Sites that allows users to define how they would like to expose latest page revisions.No sharing sites exist by default. A sharing site must be manually created for each Wagtail Site to make its latest revisions shareable. Each sharing site is defined by a unique hostname and port number.Important: configuring your sharing sites improperly could expose draft/private content publicly. Be careful when setting them up!Creating a new sharing siteAfter following the setup steps above, you should be able to create a new sharing site to use this functionality in a local Django development server. Let’s assume that you are running your local development server on the default port 8000, and that pages there are being served athttp://localhost:8000. We want to create a new sharing site athttp://sharing.localhost:8000at which latest page revisions will be exposed.To simulate accessing your site on a different hostname, you’ll need to loosenDjango’s default security settingsthat only allow access onlocalhost. Edit your settings file (e.g.myproject/settings/local.py) to add the following:ALLOWED_HOSTS=['*']Verify that you can access your local server athttp://sharing.localhost:8000. You should see the same content there as onhttp://localhost:8000, as you haven’t enabled wagtail-sharing for the default site yet.To do so, in the Wagtail admin, under Settings, Sharing Sites, create a new sharing site for the default site, with hostnamesharing.localhostand port8000.Your latest page revisions (including drafts) should now be available athttp://sharing.localhost:8000.BannersPages viewed on a wagtail-sharing shared site have a simple banner added to them to remind reviewers that the current published content may differ from the content they are viewing.This behavior can be disabled by settingsettings.WAGTAILSHARING_BANNER = False.  The banner template can be overridden by providing an alternate template file atwagtailsharing/banner.htmlsimilar to howwagtailadmin template overridesare supported.Sharing linksA page’s sharing URL can be retrieved by passing itsPageinstance towagtailsharing.helpers.get_sharing_url. This method returnsNoneif no shared sites are configured or if the specified page is not routable to a shared site.
A page’s sharing URL is based on the slug of its most recently published revision
or, if the page has never been published, its initial revision.Shared pages will also have a new dropdown menu option that links to this sharing URL from the Wagtail page explorer.HooksAs with normal page serving, the serving of shared pages continues to respect Wagtail’s built-inbefore_serve_pagehook.This project adds these additional hooks:before_route_pageCalled when routing, before a page’sroute()method is called. This hook is passed therequestand thepagethat will haveroute()called on it. If the callable returns anHttpResponse, that response will be returned immediately to the user.This hook allows for any necessary customization of Wagtail’s built-in routing behavior, for example to supportShareableRoutablePageMixin.before_serve_shared_pageCalled before the latest revision of the page is about to be served, just before itsserve()method is called. Likebefore_serve_pagethis hook is passed the page object, the request object, and theargsandkwargsthat will be passed to the page’sserve()method. If the callable returns anHttpResponse, that response will be returned immediately to the user.This hook could be useful for limiting sharing to only certain page types or for modifying a page’s contents when it is shared.fromwagtailimporthooks@hooks.register('before_serve_shared_page')defmodify_shared_title(page,request,args,kwargs):page.title+=' (Shared)'after_serve_shared_pageCalled after the page’sserve()method is called but before the response is returned to the user. This hook is passed the page object and the response object returned byserve(). If the callable returns anHttpResponse, that response will be returned immediately to the user.This hook could be useful for directly modifying the response content, for example by adding custom headers or altering the generated HTML. This hook is used to implement the notification banner described above.fromwagtailimporthooks@hooks.register('after_serve_shared_page')defadd_custom_header(page,response):response['Wagtail-Is-Shared']='1'MixinsShareableRoutablePageMixinBy default, Wagtail’sRoutablePageMixinis not compatible with Wagtail-Sharing, instead you need to useShareableRoutablePageMixinin order to view share draft content fields on routable pages.ShareableRoutablePageMixinis used exactly the same way asRoutablePageMixin:fromwagtail.fieldsimportRichTextFieldfromwagtail.modelsimportPagefromwagtail.contrib.routable_page.modelsimportroutefromwagtailsharing.modelsimportShareableRoutablePageMixinclassEventIndexPage(ShareableRoutablePageMixin,Page):intro=RichTextField()@route(r'^$')defcurrent_events(self,request):# …@route(r'^past/$')defpast_events(self,request):# …CompatibilityThis project has been tested for compatibility with:Python 3.9+Django 3.2+Wagtail 5.1+ (see past releases for older Wagtail support)It should be compatible with all intermediate versions, as well.
If you find that it is not, pleasefile an issue.TestingRunning project unit tests requirestox:$toxTo run the test app interactively, run:$tox-einteractiveNow you can visithttp://localhost:8000/admin/in a browser and log in withadmin/changeme.Open source licensing infoTERMSLICENSECFPB Source Code Policy