Set `request.is_preview` flag to show whether page is served as preview

pull/2044/head
Denis Voskvitsov 2015-10-27 14:47:24 +03:00 zatwierdzone przez Matt Westcott
rodzic 1f78af1ac8
commit ab45915a90
4 zmienionych plików z 20 dodań i 0 usunięć

Wyświetl plik

@ -23,6 +23,7 @@ Changelog
* Added the ability to override the default manager on Page models
* Added an optional human-friendly `site_name` field to sites (Timo Rieber)
* Added success message after updating image from the image upload view (Christian Peters)
* Added a `request.is_preview` variable for templates to distinguish between previewing and live (Denis Voskvitsov)
* New translations for Arabic and Latvian
* Fix: Images and page revisions created by a user are no longer deleted when the user is deleted (Rich Atkinson)
* Fix: HTTP cache purge now works again on Python 2 (Mitchel Cabuloy)

Wyświetl plik

@ -68,6 +68,7 @@ Minor features
* Added the ability to override the default manager on Page models
* Added an optional human-friendly ``site_name`` field to sites (Timo Rieber)
* Added success message after updating image from the image upload view (Christian Peters)
* Added a ``request.is_preview`` variable for templates to distinguish between previewing and live (Denis Voskvitsov)
* New translations for Arabic and Latvian

Wyświetl plik

@ -212,3 +212,17 @@ By default the User Bar appears in the top right of the browser window, flush wi
top:200px
}
Varying output between preview and live
=======================================
Sometimes you may wish to vary the template output depending on whether the page is being previewed or viewed live. For example, if you have visitor tracking code such as Google Analytics in place on your site, it's a good idea to leave this out when previewing, so that editor activity doesn't appear in your analytics reports. Wagtail provides a ``request.is_preview`` variable to distinguish between preview and live:
.. code-block:: html+django
{% if not request.is_preview %}
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
...
</script>
{% endif %}

Wyświetl plik

@ -659,6 +659,8 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed
return self.template
def serve(self, request, *args, **kwargs):
request.is_preview = getattr(request, 'is_preview', False)
return TemplateResponse(
request,
self.get_template(request, *args, **kwargs),
@ -1193,6 +1195,8 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed
here - this ensures that request.user and other properties are set appropriately for
the wagtail user bar to be displayed. This request will always be a GET.
"""
request.is_preview = True
return self.serve(request)
def get_cached_paths(self):