Permalink
Browse files

Restore compatibility with Django<1.10 releases

  • Loading branch information...
jmaupetit committed Jun 8, 2017
1 parent be8b972 commit 1f32f021082cd8c91306e3f43f87687333fb3234
Showing with 45 additions and 5 deletions.
  1. +13 −2 sandbox/settings.py
  2. +1 −1 td_biblio/urls.py
  3. +31 −2 td_biblio/views.py
@@ -41,15 +41,26 @@
'td_biblio',
]
MIDDLEWARE = [
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',
]
)
# Django < 1.10 compat
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware'
)
ROOT_URLCONF = 'sandbox.urls'
@@ -12,7 +12,7 @@
name='entry_list'
),
url(
'^import$',
'^import/$',
views.EntryBatchImportView.as_view(),
name='import'
),
@@ -2,7 +2,7 @@
import datetime
from django.contrib import messages
from django.contrib.auth.mixins import UserPassesTestMixin
from django.contrib.auth.decorators import login_required, user_passes_test
from django.core.urlresolvers import reverse_lazy
from django.utils.translation import ugettext_lazy as _
from django.views.generic import FormView, ListView
@@ -12,6 +12,33 @@
from .utils.loaders import DOILoader, PubmedLoader
def superuser_required(function=None):
"""
Decorator for views that checks that the user is a super user redirecting
to the log-in page if necessary.
Inspired by Django 'login_required' decorator
"""
actual_decorator = user_passes_test(lambda u: u.is_superuser)
if function:
return actual_decorator(function)
return actual_decorator
class LoginRequiredMixin(object):
@classmethod
def as_view(cls, **initkwargs):
view = super(LoginRequiredMixin, cls).as_view(**initkwargs)
return login_required(view)
class SuperuserRequiredMixin(object):
@classmethod
def as_view(cls, **initkwargs):
view = super(SuperuserRequiredMixin, cls).as_view(**initkwargs)
return superuser_required(view)
class EntryListView(ListView):
"""Entry list view"""
model = Entry
@@ -116,7 +143,9 @@ def get_context_data(self, **kwargs):
return ctx
class EntryBatchImportView(UserPassesTestMixin, FormView):
class EntryBatchImportView(LoginRequiredMixin,
SuperuserRequiredMixin,
FormView):
form_class = EntryBatchImportForm
template_name = 'td_biblio/entry_import.html'

0 comments on commit 1f32f02

Please sign in to comment.