documents served using django-sendfile

pull/1278/merge
jordij 2015-04-10 15:44:41 +12:00 zatwierdzone przez Karl Hobley
rodzic e7e0ddcedd
commit e33ff5d34d
5 zmienionych plików z 10 dodań i 15 usunięć

Wyświetl plik

@ -34,6 +34,7 @@ install_requires = [
"django-modelcluster>=0.6",
"django-taggit>=0.13.0",
"django-treebeard==3.0",
"django-sendfile==0.3.6",
"Pillow>=2.6.1",
"beautifulsoup4>=4.3.2",
"html5lib==0.999",

Wyświetl plik

@ -23,6 +23,7 @@ deps =
django-modelcluster>=0.6
django-taggit==0.13.0
django-treebeard==3.0
django-sendfile==0.3.6
Pillow>=2.3.0
beautifulsoup4>=4.3.2
html5lib==0.999

Wyświetl plik

@ -83,6 +83,7 @@ INSTALLED_APPS = (
'taggit',
'compressor',
'sendfile',
'wagtail.wagtailcore',
'wagtail.wagtailadmin',
@ -157,5 +158,7 @@ try:
except ImportError:
pass
# Sendfile dev backend, do NOT use in production https://github.com/johnsensible/django-sendfile#django-sendfile
SENDFILE_BACKEND = 'sendfile.backends.development'
WAGTAIL_SITE_NAME = "Test Site"

Wyświetl plik

@ -556,6 +556,9 @@ class TestServeView(TestCase):
def test_content_length_header(self):
self.assertEqual(self.get()['Content-Length'], '25')
def test_content_type_header(self):
self.assertEqual(self.get()['Content-Type'], 'application/msword')
def test_is_streaming_response(self):
self.assertTrue(self.get().streaming)

Wyświetl plik

@ -1,27 +1,14 @@
from django.shortcuts import get_object_or_404
from wsgiref.util import FileWrapper
from django.http import StreamingHttpResponse, BadHeaderError
from unidecode import unidecode
from sendfile import sendfile
from wagtail.wagtaildocs.models import Document, document_served
def serve(request, document_id, document_filename):
doc = get_object_or_404(Document, id=document_id)
wrapper = FileWrapper(doc.file)
response = StreamingHttpResponse(wrapper, content_type='application/octet-stream')
try:
response['Content-Disposition'] = 'attachment; filename=%s' % doc.filename
except BadHeaderError:
# Unicode filenames can fail on Django <1.8, Python 2 due to
# https://code.djangoproject.com/ticket/20889 - try with an ASCIIfied version of the name
response['Content-Disposition'] = 'attachment; filename=%s' % unidecode(doc.filename)
response['Content-Length'] = doc.file.size
# Send document_served signal
document_served.send(sender=Document, instance=doc, request=request)
return response
return sendfile(request, doc.file.path, attachment=True, attachment_filename=doc.filename)