0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Define a 'before_serve_document' hook

This commit is contained in:
Matt Westcott 2017-06-06 14:45:51 +01:00
parent 83c8db1ab5
commit 049b15f3fe
2 changed files with 18 additions and 1 deletions

View File

@ -697,3 +697,14 @@ Page serving
def block_googlebot(page, request, serve_args, serve_kwargs):
if request.META.get('HTTP_USER_AGENT') == 'GoogleBot':
return HttpResponse("<h1>bad googlebot no cookie</h1>")
Document serving
----------------
.. _before_serve_document:
``before_serve_document``
~~~~~~~~~~~~~~~~~~~~~~~~~
Called when Wagtail is about to serve a document. The callable passed into the hook will receive the document object and the request object. If the callable returns an ``HttpResponse``, that response will be returned immediately to the user, instead of serving the document.

View File

@ -3,12 +3,13 @@ from __future__ import absolute_import, unicode_literals
from wsgiref.util import FileWrapper
from django.conf import settings
from django.http import BadHeaderError, Http404, StreamingHttpResponse
from django.http import BadHeaderError, Http404, HttpResponse, StreamingHttpResponse
from django.shortcuts import get_object_or_404
from unidecode import unidecode
from wagtail.utils import sendfile_streaming_backend
from wagtail.utils.sendfile import sendfile
from wagtail.wagtailcore import hooks
from wagtail.wagtaildocs.models import document_served, get_document_model
@ -22,6 +23,11 @@ def serve(request, document_id, document_filename):
if doc.filename != document_filename:
raise Http404('This document does not match the given filename.')
for fn in hooks.get_hooks('before_serve_document'):
result = fn(doc, request)
if isinstance(result, HttpResponse):
return result
# Send document_served signal
document_served.send(sender=Document, instance=doc, request=request)