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

Bring up to date with recent versions and conventions #62

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*~
*.swp
*.pyc
*.db
Expand Down
46 changes: 46 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
sudo: false
language: python
cache: pip

matrix:
include:
- python: 2.7
env: DJANGO="Django>=1.8,<1.9"
- python: 3.2
env: DJANGO="Django>=1.8,<1.9"
- python: 3.3
env: DJANGO="Django>=1.8,<1.9"
- python: 3.4
env: DJANGO="Django>=1.8,<1.9"
- python: 3.5
env: DJANGO="Django>=1.8,<1.9"
- python: 2.7
env: DJANGO="Django>=1.9,<1.10"
- python: 3.4
env: DJANGO="Django>=1.9,<1.10"
- python: 3.5
env: DJANGO="Django>=1.9,<1.10"
- python: 2.7
env: DJANGO="Django>=1.10,<1.11"
- python: 3.4
env: DJANGO="Django>=1.10,<1.11"
- python: 3.5
env: DJANGO="Django>=1.10,<1.11"
- python: 2.7
env: DJANGO="Django>=1.11,<2.0"
- python: 3.4
env: DJANGO="Django>=1.11,<2.0"
- python: 3.5
env: DJANGO="Django>=1.11,<2.0"
- python: 3.6
env: DJANGO="Django>=1.11,<2.0"

install:
- pip install $DJANGO
- pip install flake8
- pip install -e .

before_script:
- flake8

script: ./runtests.sh
3 changes: 1 addition & 2 deletions examples/protected_downloads/download/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from .models import Download


@admin.register(Download)
class DownloadAdmin(admin.ModelAdmin):
list_display = ['title', 'file']

admin.site.register(Download, DownloadAdmin)
11 changes: 5 additions & 6 deletions examples/protected_downloads/download/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.db import models

from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.auth.models import User
from django.core.files.storage import FileSystemStorage
from django.db import models
from django.urls import reverse

sendfile_storage = FileSystemStorage(location=settings.SENDFILE_ROOT)

Expand All @@ -17,9 +17,8 @@ class Download(models.Model):
def is_user_allowed(self, user):
return self.users.filter(pk=user.pk).exists()

def __unicode__(self):
def __str__(self):
return self.title

@models.permalink
def get_absolute_url(self):
return ('download', [self.pk], {})
return reverse('download', [self.pk], {})
16 changes: 0 additions & 16 deletions examples/protected_downloads/download/tests.py

This file was deleted.

6 changes: 3 additions & 3 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.conf.urls import url

from .views import download, download_list

urlpatterns = patterns('',
urlpatterns = [
url(r'^$', download_list),
url(r'(?P<download_id>\d+)/$', download, name='download'),
)
]
9 changes: 3 additions & 6 deletions examples/protected_downloads/download/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseForbidden
from django.db.models import Q
from django.template import RequestContext
from django.http import HttpResponseForbidden
from django.shortcuts import get_object_or_404, render

from sendfile import sendfile

Expand All @@ -29,6 +28,4 @@ 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})
18 changes: 7 additions & 11 deletions examples/protected_downloads/manage.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#!/usr/bin/env python
import os
import sys

from __future__ import absolute_import
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from django.core.management import execute_manager
try:
from . import settings # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
sys.exit(1)

if __name__ == "__main__":
execute_manager(settings)
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'protected_downloads.settings')
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
118 changes: 55 additions & 63 deletions examples/protected_downloads/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,83 @@

import os.path

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

SECRET_KEY = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
# ('Your Name', '[email protected]'),
)
ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'protected_downloads.download',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

MANAGERS = ADMINS
ROOT_URLCONF = 'protected_downloads.urls'

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

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(PROJECT_ROOT, 'download.db'),
'NAME': os.path.join(BASE_DIR, 'download.db'),
}
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Chicago'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'
# Internationalization

SITE_ID = 1
TIME_ZONE = 'UTC'

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = ''
USE_L10N = True

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'
USE_TZ = True

# Make this unique, and don't share it with anybody.
SECRET_KEY = 'n309^dwk=@+g72ko--8vjyz&1v0u%xf#*0=wzr=2n#f3hb0a=l'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
# 'django.template.loaders.eggs.load_template_source',
)
# Static files (CSS, JavaScript, Images)

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
)

ROOT_URLCONF = 'protected_downloads.urls'

TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, 'templates'),
)

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'download',
'sendfile',
)
STATIC_URL = '/static/'


# SENDFILE settings
# Available backends:
# sendfile.backends.development
# sendfile.backends.mod_wsgi
# sendfile.backends.nginx
# sendfile.backends.simple
# sendfile.backends.xsendfile
SENDFILE_BACKEND = 'sendfile.backends.development'
#SENDFILE_BACKEND = 'sendfile.backends.xsendfile'
#SENDFILE_BACKEND = 'sendfile.backends.nginx'
SENDFILE_ROOT = os.path.join(PROJECT_ROOT, 'protected')
SENDFILE_ROOT = os.path.join(BASE_DIR, 'protected')
SENDFILE_URL = '/protected'
12 changes: 6 additions & 6 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.conf.urls import include, url
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
(r'^', include('protected_downloads.download.urls')),
(r'^admin/', include(admin.site.urls)),
)
urlpatterns = [
url(r'^', include('protected_downloads.download.urls')),
url(r'^admin/', admin.site.urls),
]
3 changes: 3 additions & 0 deletions runtests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

DJANGO_SETTINGS_MODULE=tests.settings django-admin test tests "$@"
26 changes: 11 additions & 15 deletions sendfile/__init__.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
VERSION = (0, 3, 11)
__version__ = '.'.join(map(str, VERSION))

import os.path
from mimetypes import guess_type
import unicodedata
from importlib import import_module


VERSION = (0, 3, 11)
__version__ = '.'.join(map(str, VERSION))


def _lazy_load(fn):
_cached = []

def _decorated():
if not _cached:
_cached.append(fn())
return _cached[0]

def clear():
while _cached:
_cached.pop()
_decorated.clear = clear

return _decorated


@_lazy_load
def _get_sendfile():
try:
from importlib import import_module
except ImportError:
from django.utils.importlib import import_module
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

Expand All @@ -35,7 +36,6 @@ def _get_sendfile():
return module.sendfile



def sendfile(request, filename, attachment=False, attachment_filename=None, mimetype=None, encoding=None):
'''
create a response to send file using backend configured in SENDFILE_BACKEND
Expand Down Expand Up @@ -64,20 +64,16 @@ def sendfile(request, filename, attachment=False, attachment_filename=None, mime
mimetype = guessed_mimetype
else:
mimetype = 'application/octet-stream'

response = _sendfile(request, filename, mimetype=mimetype)
if attachment:
if attachment_filename is None:
attachment_filename = os.path.basename(filename)
parts = ['attachment']
if attachment_filename:
try:
from django.utils.encoding import force_text
except ImportError:
# Django 1.3
from django.utils.encoding import force_unicode as force_text
from django.utils.encoding import force_text
attachment_filename = force_text(attachment_filename)
ascii_filename = unicodedata.normalize('NFKD', attachment_filename).encode('ascii','ignore')
ascii_filename = unicodedata.normalize('NFKD', attachment_filename).encode('ascii', 'ignore').decode('ascii')
parts.append('filename="%s"' % ascii_filename)
if ascii_filename != attachment_filename:
from django.utils.http import urlquote
Expand Down
Loading