Skip to content

Commit

Permalink
Python 3 support
Browse files Browse the repository at this point in the history
Tested with:
- Python 3.3 / Django 1.6
- Python 2.7 / Django 1.6
- Python 2.7 / Django 1.3
  • Loading branch information
peterdewachter committed Feb 20, 2014
1 parent 629df02 commit b0079de
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
3 changes: 2 additions & 1 deletion sendfile/backends/nginx.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.http import HttpResponse
from django.utils.encoding import smart_str

from _internalredirect import _convert_file_to_url

def sendfile(request, filename, **kwargs):
response = HttpResponse()
url = _convert_file_to_url(filename)
response['X-Accel-Redirect'] = url.encode('utf-8')
response['X-Accel-Redirect'] = smart_str(url)

return response
7 changes: 5 additions & 2 deletions sendfile/backends/simple.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os
import stat
import re
from email.Utils import parsedate_tz, mktime_tz
try:
from email.utils import parsedate_tz, mktime_tz
except ImportError:
from email.Utils import parsedate_tz, mktime_tz

from django.core.files.base import File
from django.http import HttpResponse, HttpResponseNotModified
Expand All @@ -16,7 +19,7 @@ def sendfile(request, filename, **kwargs):
return HttpResponseNotModified()


response = HttpResponse(File(file(filename, 'rb')))
response = HttpResponse(File(open(filename, 'rb')))

response["Last-Modified"] = http_date(statobj[stat.ST_MTIME])
return response
Expand Down
3 changes: 2 additions & 1 deletion sendfile/backends/xsendfile.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.http import HttpResponse
from django.utils.encoding import smart_str

def sendfile(request, filename, **kwargs):
response = HttpResponse()
response['X-Sendfile'] = unicode(filename).encode('utf-8')
response['X-Sendfile'] = smart_str(unicode(filename))

return response

7 changes: 4 additions & 3 deletions sendfile/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.conf import settings
from django.test import TestCase
from django.http import HttpResponse, Http404, HttpRequest
from django.utils.encoding import smart_str
import os.path
from tempfile import mkdtemp
import shutil
Expand Down Expand Up @@ -54,7 +55,7 @@ def test_sendfile(self):
response = real_sendfile(HttpRequest(), self._get_readme())
self.assertTrue(response is not None)
self.assertEqual('text/plain', response['Content-Type'])
self.assertEqual(self._get_readme(), response.content)
self.assertEqual(self._get_readme(), smart_str(response.content))

def test_set_mimetype(self):
response = real_sendfile(HttpRequest(), self._get_readme(), mimetype='text/plain')
Expand Down Expand Up @@ -104,7 +105,7 @@ def test_xsendfile_header_containing_unicode(self):
filepath = self.ensure_file(u'péter_là_gueule.txt')
response = real_sendfile(HttpRequest(), filepath)
self.assertTrue(response is not None)
self.assertEqual(filepath, response['X-Sendfile'].decode('utf-8'))
self.assertEqual(smart_str(filepath), response['X-Sendfile'])


class TestNginxBackend(TempFileTestCase):
Expand All @@ -126,4 +127,4 @@ def test_xaccelredirect_header_containing_unicode(self):
filepath = self.ensure_file(u'péter_là_gueule.txt')
response = real_sendfile(HttpRequest(), filepath)
self.assertTrue(response is not None)
self.assertEqual(u'/private/péter_là_gueule.txt', response['X-Accel-Redirect'].decode('utf-8'))
self.assertEqual(smart_str(u'/private/péter_là_gueule.txt'), response['X-Accel-Redirect'])
8 changes: 8 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from distutils.core import setup

try:
from distutils.command.build_py import build_py_2to3 as build_py
except ImportError:
from distutils.command.build_py import build_py


version=__import__('sendfile').__version__


Expand Down Expand Up @@ -36,4 +42,6 @@
'Programming Language :: Python',
'Topic :: Software Development :: Libraries :: Python Modules',
],

cmdclass = {'build_py': build_py},
)

0 comments on commit b0079de

Please sign in to comment.