|
@@ -1,5 +1,6 @@
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
from django.contrib.staticfiles.templatetags.staticfiles import static
|
|
|
+from django.http.response import HttpResponse
|
|
|
from django.utils.html import format_html
|
|
|
from wagtail.contrib.forms.models import AbstractForm
|
|
|
from wagtail.core import hooks
|
|
@@ -8,6 +9,7 @@ from wagtail.core.models import UserPagePermissionsProxy, get_page_models
|
|
|
from coderedcms import utils
|
|
|
from coderedcms.models import CoderedFormPage
|
|
|
|
|
|
+import mimetypes
|
|
|
|
|
|
@hooks.register('insert_global_admin_css')
|
|
|
def global_admin_css():
|
|
@@ -50,3 +52,15 @@ def codered_forms(user, editable_forms):
|
|
|
editable_forms = editable_forms.filter(content_type__in=form_types)
|
|
|
|
|
|
return editable_forms
|
|
|
+
|
|
|
+@hooks.register('before_serve_document')
|
|
|
+def serve_document_directly(document, request):
|
|
|
+ """
|
|
|
+ This hook prevents documents from being downloaded unless specified by an <a> tag with the download attribute.
|
|
|
+ """
|
|
|
+ content_type, content_encoding = mimetypes.guess_type(document.filename)
|
|
|
+ response = HttpResponse(document.file.read(), content_type=content_type)
|
|
|
+ response['Content-Disposition'] = 'inline;filename="{0}"'.format(document.filename)
|
|
|
+ response['Content-Encoding'] = content_encoding
|
|
|
+ return response
|
|
|
+
|