Skip to content
Permalink
Browse files

Merge pull request #1010 from nicolazilio/pdf_inline

Change send_file to open, rather than download, PDF files
  • Loading branch information
benjaoming committed Jan 8, 2020
2 parents f005925 + 5c5d7dc commit 72609e6f1f68c19f3e77d5d5bcbff4935ab20fcf
Showing with 24 additions and 2 deletions.
  1. +2 −1 docs/release_notes.rst
  2. +4 −1 src/wiki/core/http.py
  3. +18 −0 tests/core/test_basic.py
@@ -30,7 +30,8 @@ Fixed
Changed
~~~~~~~

* Test coverage upped from 75 to 78+% :url-issue:`976` (Mads Jensen, Benjamin Bach)
* Test coverage upped from 75 to 80+% :url-issue:`976` (Mads Jensen, Benjamin Bach)
* PDF attachment Content-Disposition header changed to ``inline`` for browser previewing :url-issue:`1010` (nicolazilio)
* PyTest upgraded to latest 5.3

Removed
@@ -44,6 +44,9 @@ def send_file(request, filepath, last_modified=None, filename=None):

if filename:
filename_escaped = filepath_to_uri(filename)
response["Content-Disposition"] = "attachment; filename=%s" % filename_escaped
if 'pdf' in mimetype.lower():
response["Content-Disposition"] = "inline; filename=%s" % filename_escaped
else:
response["Content-Disposition"] = "attachment; filename=%s" % filename_escaped

return response
@@ -1,5 +1,9 @@
import tempfile
from datetime import datetime

from django.test import TestCase
from wiki.conf import settings as wiki_settings
from wiki.core.http import send_file
from wiki.forms import Group
from wiki.models import Article, ArticleRevision, URLPath

@@ -36,3 +40,17 @@ def test_manager(self):
article.add_revision(ArticleRevision(title="Root", content="Hello\nworld"),
save=True)
self.assertEqual("Hello\r\nworld", article.current_revision.content)


class HttpTests(TestCase):
def test_send_file(self):
fabricate_request = self.client.get("/").wsgi_request
fobject = tempfile.NamedTemporaryFile("r")
response = send_file(fabricate_request, fobject.name, filename="test.pdf")
assert response.has_header("Content-Disposition")
assert "inline" in response.get("Content-Disposition")
response = send_file(fabricate_request, fobject.name, filename="test.jpeg")
assert response.has_header("Content-Disposition")
response = send_file(fabricate_request, fobject.name, filename="test.jpeg", last_modified=datetime.now())
assert response.has_header("Content-Disposition")
fobject.close()

0 comments on commit 72609e6

Please sign in to comment.
You can’t perform that action at this time.