Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compatibility with Django 4.0 #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/protected_downloads/download/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.contrib.auth.models import User
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.urls import reverse

sendfile_storage = FileSystemStorage(location=settings.SENDFILE_ROOT)

Expand All @@ -20,6 +21,5 @@ def is_user_allowed(self, user):
def __unicode__(self):
return self.title

@models.permalink
def get_absolute_url(self):
return ('download', [self.pk], {})
return reverse('download', args=[self.pk])
10 changes: 5 additions & 5 deletions examples/protected_downloads/download/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.conf.urls.defaults import *
from django.urls import re_path

from .views import download, download_list

urlpatterns = patterns('',
url(r'^$', download_list),
url(r'(?P<download_id>\d+)/$', download, name='download'),
)
urlpatterns = [
re_path(r'^$', download_list),
re_path(r'(?P<download_id>\d+)/$', download, name='download'),
]
8 changes: 4 additions & 4 deletions examples/protected_downloads/download/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, render_to_response
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseForbidden
from django.db.models import Q
from django.template import RequestContext
Expand Down Expand Up @@ -29,6 +29,6 @@ def download_list(request):
downloads = downloads.filter(Q(is_public=True) | Q(users=request.user))
else:
downloads = downloads.filter(is_public=True)
return render_to_response('download/download_list.html',
{'download_list': downloads},
context_instance=RequestContext(request))
return render(request, 'download/download_list.html',
{'download_list': downloads},
context_instance=RequestContext(request))
24 changes: 22 additions & 2 deletions examples/protected_downloads/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,29 @@
# 'django.template.loaders.eggs.load_template_source',
)

MIDDLEWARE_CLASSES = (
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.abspath(os.path.join(os.path.dirname(__file__), 'templates')
),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
]
}
},
]

MIDDLEWARE = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_URLCONF = 'protected_downloads.urls'
Expand All @@ -79,7 +98,8 @@
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'download',
'django.contrib.messages',
'protected_downloads.download',
'sendfile',
)

Expand Down
10 changes: 5 additions & 5 deletions examples/protected_downloads/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.conf.urls.defaults import *
from django.urls import include, re_path, path

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
(r'^', include('protected_downloads.download.urls')),
(r'^admin/', include(admin.site.urls)),
)
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^', include('protected_downloads.download.urls')),
]
17 changes: 12 additions & 5 deletions sendfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,22 @@ def sendfile(request, filename, attachment=False, attachment_filename=None, mime
parts = ['attachment']
if attachment_filename:
try:
from django.utils.encoding import force_text
from django.utils.encoding import force_str as force_text
except ImportError:
# Django 1.3
from django.utils.encoding import force_unicode as force_text
# Django 2.x
try:
from django.utils.encoding import force_text
except ImportError:
# Django 1.3
from django.utils.encoding import force_unicode as force_text
attachment_filename = force_text(attachment_filename)
ascii_filename = unicodedata.normalize('NFKD', attachment_filename).encode('ascii','ignore')
ascii_filename = force_text(unicodedata.normalize('NFKD', attachment_filename).encode('ascii','ignore'))
parts.append('filename="%s"' % ascii_filename)
if ascii_filename != attachment_filename:
from django.utils.http import urlquote
try:
from urllib.parse import quote as urlquote
except ImportError:
from django.utils.http import urlquote
quoted_filename = urlquote(attachment_filename)
parts.append('filename*=UTF-8\'\'%s' % quoted_filename)
response['Content-Disposition'] = '; '.join(parts)
Expand Down
6 changes: 5 additions & 1 deletion sendfile/backends/_internalredirect.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os.path

from django.conf import settings
from django.utils.encoding import smart_text, smart_bytes
from django.utils.encoding import smart_bytes
try:
from django.utils.encoding import smart_str as smart_text
except ImportError:
from django.utils.encoding import smart_text

try:
from urllib.parse import quote
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 force_str


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

return response
4 changes: 2 additions & 2 deletions sendfile/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ 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'.encode('utf-8'), unquote(response['X-Accel-Redirect']))
self.assertEqual(u'/private/péter_là_gueule.txt', unquote(response['X-Accel-Redirect']))


class TestModWsgiBackend(TempFileTestCase):
Expand All @@ -154,4 +154,4 @@ def test_location_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'.encode('utf-8'), unquote(response['Location']))
self.assertEqual(u'/private/péter_là_gueule.txt', unquote(response['Location']))