Skip to content

Commit

Permalink
Merge pull request #23 from timgraham/drop-old-versions
Browse files Browse the repository at this point in the history
Drop support for Django < 1.8
  • Loading branch information
brutasse committed Sep 14, 2016
2 parents 5c1b58b + 47a9855 commit 16a608d
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 129 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,3 +1,5 @@
*.py[co]
__pycache__
dist
django_ratelimit_backend.egg-info
.tox
Expand Down
13 changes: 0 additions & 13 deletions .travis.yml
Expand Up @@ -2,22 +2,9 @@ language: python
python: 3.5
sudo: false
env:
- TOXENV=py26-django14
- TOXENV=py26-django15
- TOXENV=py26-django16
- TOXENV=py27-django14
- TOXENV=py27-django15
- TOXENV=py27-django16
- TOXENV=py27-django17
- TOXENV=py27-django18
- TOXENV=py27-django19
- TOXENV=py33-django15
- TOXENV=py33-django16
- TOXENV=py33-django17
- TOXENV=py33-django18
- TOXENV=py34-django15
- TOXENV=py34-django16
- TOXENV=py34-django17
- TOXENV=py34-django18
- TOXENV=py34-django19
- TOXENV=py35-django19
Expand Down
6 changes: 3 additions & 3 deletions docs/usage.rst
Expand Up @@ -38,9 +38,9 @@ Quickstart
admin.autodiscover()
urlpatterns += patterns('',
urlpatterns += [
(r'^admin/', include(admin.site.urls)),
)
]
Ratelimitbackend's admin site overrides the default admin login view to add
rate-limiting. You can keep registering your models to the default admin
Expand All @@ -59,7 +59,7 @@ Quickstart
if request.method == 'POST':
form = AuthenticationForm(data=request.POST, request=request)
# etc. etc.
If you use ``django.contrib.auth.authenticate``, pass it the request object
as well.

Expand Down
2 changes: 1 addition & 1 deletion ratelimitbackend/admin.py
Expand Up @@ -10,7 +10,7 @@
from .views import login


class RateLimitAdminSite(AdminSite):
class RateLimitAdminSite(AdminSite): # noqa
def login(self, request, extra_context=None):
"""
Displays the login form for the given HttpRequest.
Expand Down
8 changes: 1 addition & 7 deletions ratelimitbackend/forms.py
@@ -1,12 +1,6 @@
from django import forms
from django.contrib.admin.forms import AdminAuthenticationForm as AdminAuthForm

try:
from django.contrib.admin.forms import ERROR_MESSAGE
except ImportError:
# The ERROR_MESSAGE has been moved in Django>=1.7
ERROR_MESSAGE = AdminAuthForm.error_messages['invalid_login']

from django.contrib.auth import authenticate
from django.contrib.auth.forms import AuthenticationForm as AuthForm
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -35,7 +29,7 @@ class AdminAuthenticationForm(AdminAuthForm):
def clean(self):
username = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')
message = ERROR_MESSAGE
message = self.error_messages['invalid_login']

if username and password:
self.user_cache = authenticate(username=username,
Expand Down
23 changes: 3 additions & 20 deletions ratelimitbackend/views.py
@@ -1,33 +1,16 @@
try:
from urllib.parse import urlparse
except ImportError: # Python2
from urlparse import urlparse # noqa

import django

from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME, login as auth_login
try:
from django.contrib.sites.shortcuts import get_current_site
except ImportError:
from django.contrib.sites.models import get_current_site
from django.contrib.sites.shortcuts import get_current_site
from django.shortcuts import redirect
from django.template.response import TemplateResponse as BaseTemplateResponse
from django.template.response import TemplateResponse
from django.utils.six.moves.urllib.parse import urlparse
from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import csrf_protect
from django.views.decorators.debug import sensitive_post_parameters

from .forms import AuthenticationForm


class TemplateResponse(BaseTemplateResponse):
def __init__(self, request, template, context=None, **kwargs):
if django.VERSION < (1, 8):
kwargs['current_app'] = request.current_app
super(TemplateResponse, self).__init__(
request, template, context=context, **kwargs)


@sensitive_post_parameters()
@csrf_protect
@never_cache
Expand Down
26 changes: 5 additions & 21 deletions runtests.py
@@ -1,18 +1,10 @@
#!/usr/bin/env python
import logging
import os
import sys

from django.conf import settings
try:
from django.utils.functional import empty
except ImportError:
empty = None


class NullHandler(logging.Handler): # NullHandler isn't in Python 2.6
def emit(self, record):
pass
from django.test.runner import DiscoverRunner
from django.utils.functional import empty


def setup_test_environment():
Expand Down Expand Up @@ -62,7 +54,7 @@ def setup_test_environment():
'version': 1,
'handlers': {
'null': {
'class': 'runtests.NullHandler',
'class': 'logging.NullHandler',
}
},
'loggers': {
Expand All @@ -86,23 +78,15 @@ def setup_test_environment():

# set up settings for running tests for all apps
settings.configure(**settings_dict)
try:
from django import setup
except ImportError:
pass
else:
setup() # Needed for Django>=1.7
from django import setup
setup()


def runtests():
setup_test_environment()

parent = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, parent)
try:
from django.test.runner import DiscoverRunner
except ImportError:
from discover_runner.runner import DiscoverRunner

runner = DiscoverRunner(verbosity=1, interactive=True, failfast=False)
failures = runner.run_tests(())
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Expand Up @@ -25,11 +25,13 @@
'Development Status :: 5 - Production/Stable',
'Environment :: Web Environment',
'Framework :: Django',
'Framework :: Django :: 1.8',
'Framework :: Django :: 1.9',
'Framework :: Django :: 1.10',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Natural Language :: English',
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
Expand Down
68 changes: 33 additions & 35 deletions tests/models.py
@@ -1,40 +1,38 @@
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.utils import timezone

try:
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.utils import timezone
except ImportError:
pass
else:
class UserManager(BaseUserManager):
def create_user(self, email, password=None, **extra_fields):
now = timezone.now()
if not email:
raise ValueError('The email must be set.')
email = UserManager.normalize_email(email)
user = self.model(email=email, last_login=now, date_joined=now,
**extra_fields)
user.set_password(password)
user.save(using=self._db)
return user

def create_superuser(self, email, password, **extra_fields):
user = self.create_user(email, password, **extra_fields)
user.is_staff = True
user.is_active = True
user.is_superuser = True
user.save(using=self._db, update_fields=['is_staff', 'is_active',
'is_superuser'])
return user
class UserManager(BaseUserManager):
def create_user(self, email, password=None, **extra_fields):
now = timezone.now()
if not email:
raise ValueError('The email must be set.')
email = UserManager.normalize_email(email)
user = self.model(email=email, last_login=now, date_joined=now,
**extra_fields)
user.set_password(password)
user.save(using=self._db)
return user

class User(AbstractBaseUser):
"""A user with email as identifier"""
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
email = models.EmailField(max_length=255, unique=True, db_index=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
date_joined = models.DateTimeField(default=timezone.now)
def create_superuser(self, email, password, **extra_fields):
user = self.create_user(email, password, **extra_fields)
user.is_staff = True
user.is_active = True
user.is_superuser = True
user.save(using=self._db, update_fields=['is_staff', 'is_active',
'is_superuser'])
return user

objects = UserManager()

class User(AbstractBaseUser):
"""A user with email as identifier"""
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
email = models.EmailField(max_length=255, unique=True, db_index=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
date_joined = models.DateTimeField(default=timezone.now)

objects = UserManager()
14 changes: 1 addition & 13 deletions tests/test_backends.py
@@ -1,22 +1,12 @@
# -*- coding: utf-8 -*-
import warnings

import django

from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.utils import override_settings
try:
from unittest import skipIf
except ImportError:
from django.utils.unittest import skipIf

try:
from django.contrib.auth import get_user_model
except ImportError:
pass


class RateLimitTests(TestCase):
Expand All @@ -28,7 +18,6 @@ def assertRateLimited(self, response): # noqa
status_code=403)

@override_settings(AUTH_USER_MODEL='tests.User')
@skipIf(django.VERSION[:2] < (1, 5), "Swappable user not available")
def test_ratelimit_login_attempt_swapped_user(self):
url = reverse('login')
response = self.client.get(url)
Expand Down Expand Up @@ -78,7 +67,6 @@ def test_ratelimit_login_attempt(self):
self.assertRateLimited(response)

@override_settings(AUTH_USER_MODEL='tests.User')
@skipIf(django.VERSION[:2] < (1, 5), "Swappable user not available")
def test_ratelimit_login_attempt_swapped(self):
url = reverse('login')
response = self.client.get(url)
Expand Down
16 changes: 1 addition & 15 deletions tox.ini
@@ -1,28 +1,14 @@
[tox]
# Matrix looks like:
#
# py26-1.4, py26-1.5, py26-1.6,
# py27-1.4, py27-1.5, py27-1.6, py27-1.7, py27-1.8, py27-1.9,
# py33-1.5, py33-1.6, py33-1.7, py33-1.8,
# py34-1.5, py34-1.6, py34-1.7, py34-1.8, py34-1.9,
# py35-1.9,
envlist =
py2{6,7}-django14,
py26-django1{5,6},
py{27,33,34}-django1{5,6,7,8},
py{27,33,34}-django18,
py{27,34,35}-django19,
docs, lint

[testenv]
commands = python -Wall setup.py test
deps =
django14: Django>=1.4,<1.5
django15: Django>=1.5,<1.6
django16: Django>=1.6,<1.7
django17: Django>=1.7,<1.8
django18: Django>=1.8,<1.9
django19: Django>=1.9,<1.10
django14,django15: django-discover-runner
install_command = pip install --no-binary Django {opts} {packages}

[testenv:docs]
Expand Down

0 comments on commit 16a608d

Please sign in to comment.