kopia lustrzana https://github.com/wagtail/wagtail
documents served using django-sendfile
rodzic
e7e0ddcedd
commit
e33ff5d34d
1
setup.py
1
setup.py
|
@ -34,6 +34,7 @@ install_requires = [
|
||||||
"django-modelcluster>=0.6",
|
"django-modelcluster>=0.6",
|
||||||
"django-taggit>=0.13.0",
|
"django-taggit>=0.13.0",
|
||||||
"django-treebeard==3.0",
|
"django-treebeard==3.0",
|
||||||
|
"django-sendfile==0.3.6",
|
||||||
"Pillow>=2.6.1",
|
"Pillow>=2.6.1",
|
||||||
"beautifulsoup4>=4.3.2",
|
"beautifulsoup4>=4.3.2",
|
||||||
"html5lib==0.999",
|
"html5lib==0.999",
|
||||||
|
|
1
tox.ini
1
tox.ini
|
@ -23,6 +23,7 @@ deps =
|
||||||
django-modelcluster>=0.6
|
django-modelcluster>=0.6
|
||||||
django-taggit==0.13.0
|
django-taggit==0.13.0
|
||||||
django-treebeard==3.0
|
django-treebeard==3.0
|
||||||
|
django-sendfile==0.3.6
|
||||||
Pillow>=2.3.0
|
Pillow>=2.3.0
|
||||||
beautifulsoup4>=4.3.2
|
beautifulsoup4>=4.3.2
|
||||||
html5lib==0.999
|
html5lib==0.999
|
||||||
|
|
|
@ -83,6 +83,7 @@ INSTALLED_APPS = (
|
||||||
|
|
||||||
'taggit',
|
'taggit',
|
||||||
'compressor',
|
'compressor',
|
||||||
|
'sendfile',
|
||||||
|
|
||||||
'wagtail.wagtailcore',
|
'wagtail.wagtailcore',
|
||||||
'wagtail.wagtailadmin',
|
'wagtail.wagtailadmin',
|
||||||
|
@ -157,5 +158,7 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
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"
|
WAGTAIL_SITE_NAME = "Test Site"
|
||||||
|
|
|
@ -556,6 +556,9 @@ class TestServeView(TestCase):
|
||||||
def test_content_length_header(self):
|
def test_content_length_header(self):
|
||||||
self.assertEqual(self.get()['Content-Length'], '25')
|
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):
|
def test_is_streaming_response(self):
|
||||||
self.assertTrue(self.get().streaming)
|
self.assertTrue(self.get().streaming)
|
||||||
|
|
||||||
|
|
|
@ -1,27 +1,14 @@
|
||||||
from django.shortcuts import get_object_or_404
|
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
|
from wagtail.wagtaildocs.models import Document, document_served
|
||||||
|
|
||||||
|
|
||||||
def serve(request, document_id, document_filename):
|
def serve(request, document_id, document_filename):
|
||||||
doc = get_object_or_404(Document, id=document_id)
|
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
|
# Send document_served signal
|
||||||
document_served.send(sender=Document, instance=doc, request=request)
|
document_served.send(sender=Document, instance=doc, request=request)
|
||||||
|
|
||||||
return response
|
return sendfile(request, doc.file.path, attachment=True, attachment_filename=doc.filename)
|
||||||
|
|
Ładowanie…
Reference in New Issue