From 7fb888cbcf486d1d330b64c7064edc676449eb65 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Wed, 18 Dec 2019 10:41:17 +0100
Subject: [PATCH] general: remove compatibility with django < 1.11 (#38616)

---
 debian/control        |  4 ++--
 mellon/compat.py      | 27 ---------------------------
 mellon/middleware.py  |  7 ++++---
 mellon/urls.py        |  3 ---
 mellon/utils.py       |  2 +-
 mellon/views.py       |  4 ++--
 setup.py              |  4 ++--
 tests/test_sso_slo.py |  3 +--
 tests/test_views.py   |  3 +--
 testsettings.py       | 15 +++++++--------
 tox.ini               |  4 +---
 11 files changed, 21 insertions(+), 55 deletions(-)
 delete mode 100644 mellon/compat.py

diff --git a/debian/control b/debian/control
index 340f7a5..586ad66 100644
--- a/debian/control
+++ b/debian/control
@@ -13,7 +13,7 @@ Package: python-django-mellon
 Architecture: all
 Depends: ${misc:Depends}, ${python:Depends},
     python (>= 2.7),
-    python-django (>= 1.5),
+    python-django (>= 1:1.11),
     python-isodate,
     python-lasso,
     python-atomicwrites
@@ -23,7 +23,7 @@ Description: SAML authentication for Django
 Package: python3-django-mellon
 Architecture: all
 Depends: ${misc:Depends}, ${python:Depends},
-    python3-django (>= 1.5),
+    python3-django (>= 1:1.11),
     python3-isodate,
     python3-lasso,
     python3-atomicwrites
diff --git a/mellon/compat.py b/mellon/compat.py
deleted file mode 100644
index 3ec9e6e..0000000
--- a/mellon/compat.py
+++ /dev/null
@@ -1,27 +0,0 @@
-# django-mellon - SAML2 authentication for Django
-# Copyright (C) 2014-2019 Entr'ouvert
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as
-# published by the Free Software Foundation, either version 3 of the
-# License, or (at your option) any later version.
-
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU Affero General Public License for more details.
-
-# You should have received a copy of the GNU Affero General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-import django
-if django.VERSION < (1, 11, 0):
-    from django.core.urlresolvers import reverse
-    MiddlewareClass = object
-
-    is_authenticated = lambda user: user.is_authenticated()
-else:
-    from django.urls import reverse
-    from django.utils.deprecation import MiddlewareMixin
-    MiddlewareClass = MiddlewareMixin
-
-    is_authenticated = lambda user: user.is_authenticated
diff --git a/mellon/middleware.py b/mellon/middleware.py
index 6dbc584..9d07cb7 100644
--- a/mellon/middleware.py
+++ b/mellon/middleware.py
@@ -17,14 +17,15 @@ from __future__ import unicode_literals
 
 from django.utils.http import urlencode
 from django.http import HttpResponseRedirect
+from django.utils.deprecation import MiddlewareMixin
+from django.urls import reverse
 
 from . import app_settings, utils
-from .compat import reverse, MiddlewareClass, is_authenticated
 
 PASSIVE_TRIED_COOKIE = 'MELLON_PASSIVE_TRIED'
 
 
-class PassiveAuthenticationMiddleware(MiddlewareClass):
+class PassiveAuthenticationMiddleware(MiddlewareMixin):
     def process_response(self, request, response):
         # When unlogged remove the PASSIVE_TRIED cookie
         if app_settings.OPENED_SESSION_COOKIE_NAME \
@@ -50,7 +51,7 @@ class PassiveAuthenticationMiddleware(MiddlewareClass):
             return
         if not app_settings.OPENED_SESSION_COOKIE_NAME:
             return
-        if hasattr(request, 'user') and is_authenticated(request.user):
+        if hasattr(request, 'user') and request.user.is_authenticated:
             return
         if PASSIVE_TRIED_COOKIE in request.COOKIES:
             return
diff --git a/mellon/urls.py b/mellon/urls.py
index db4f58a..0d278ba 100644
--- a/mellon/urls.py
+++ b/mellon/urls.py
@@ -14,6 +14,3 @@ urlpatterns = [
     url('metadata/$', views.metadata,
         name='mellon_metadata')
 ]
-if django.VERSION < (1, 8):
-    from django.conf.urls import patterns
-    urlpatterns = patterns('', *urlpatterns)
diff --git a/mellon/utils.py b/mellon/utils.py
index d0ddc91..64aeb49 100644
--- a/mellon/utils.py
+++ b/mellon/utils.py
@@ -25,13 +25,13 @@ from xml.parsers import expat
 import django
 from django.contrib import auth
 from django.template.loader import render_to_string
+from django.urls import reverse
 from django.utils.timezone import make_aware, now, make_naive, is_aware, get_default_timezone
 from django.conf import settings
 from django.utils.six.moves.urllib.parse import urlparse
 import lasso
 
 from . import app_settings
-from .compat import reverse
 
 
 def create_metadata(request):
diff --git a/mellon/views.py b/mellon/views.py
index c2c2573..15f5a86 100644
--- a/mellon/views.py
+++ b/mellon/views.py
@@ -30,6 +30,7 @@ from django.contrib import auth
 from django.conf import settings
 from django.views.decorators.csrf import csrf_exempt
 from django.shortcuts import render, resolve_url
+from django.urls import reverse
 from django.utils.http import urlencode
 from django.utils import six
 from django.utils.encoding import force_text
@@ -38,7 +39,6 @@ from django.db import transaction
 from django.utils.translation import ugettext as _
 
 from . import app_settings, utils
-from .compat import reverse, is_authenticated
 
 RETRY_LOGIN_COOKIE = 'MELLON_RETRY_LOGIN'
 
@@ -529,7 +529,7 @@ class LogoutView(ProfileMixin, LogMixin, View):
         next_url = request.GET.get(REDIRECT_FIELD_NAME)
         referer = request.META.get('HTTP_REFERER')
         if not referer or utils.same_origin(referer, request.build_absolute_uri()):
-            if hasattr(request, 'user') and is_authenticated(request.user):
+            if hasattr(request, 'user') and request.user.is_authenticated:
                 logout = None
                 try:
                     issuer = request.session.get('mellon_session', {}).get('issuer')
diff --git a/setup.py b/setup.py
index 7aae977..053408d 100755
--- a/setup.py
+++ b/setup.py
@@ -91,13 +91,13 @@ setup(name="django-mellon",
       include_package_data=True,
       packages=find_packages(),
       install_requires=[
-          'django>=1.7,<2.3',
+          'django>=1.11,<2.3',
           'requests',
           'isodate',
           'atomicwrites',
       ],
       setup_requires=[
-          'django>=1.7,<2',
+          'django>=1.10,<2.3',
       ],
       tests_require=[
           'nose>=0.11.4',
diff --git a/tests/test_sso_slo.py b/tests/test_sso_slo.py
index abcf9ed..83a50cc 100644
--- a/tests/test_sso_slo.py
+++ b/tests/test_sso_slo.py
@@ -26,6 +26,7 @@ import lasso
 import pytest
 from pytest import fixture
 
+from django.urls import reverse
 from django.utils import six
 from django.utils.six.moves.urllib import parse as urlparse
 from django.utils.encoding import force_str
@@ -34,8 +35,6 @@ from mellon.utils import create_metadata
 
 from httmock import all_requests, HTTMock, response as mock_response
 
-from mellon.compat import reverse
-
 from utils import reset_caplog
 
 
diff --git a/tests/test_views.py b/tests/test_views.py
index 245e58c..c9fd253 100644
--- a/tests/test_views.py
+++ b/tests/test_views.py
@@ -25,13 +25,12 @@ import hashlib
 from httmock import HTTMock
 
 import django
+from django.urls import reverse
 from django.utils.encoding import force_text
 from django.utils.http import urlencode
 
 from xml_utils import assert_xml_constraints
 
-from mellon.compat import reverse
-
 from utils import error_500, html_response
 
 pytestmark = pytest.mark.django_db
diff --git a/testsettings.py b/testsettings.py
index fc24f09..e928da5 100644
--- a/testsettings.py
+++ b/testsettings.py
@@ -38,11 +38,10 @@ ROOT_URLCONF = 'urls_tests'
 TEMPLATE_DIRS = [
     'tests/templates/',
 ]
-if django.VERSION >= (1, 8):
-    TEMPLATES = [
-        {
-            'BACKEND': 'django.template.backends.django.DjangoTemplates',
-            'APP_DIRS': True,
-            'DIRS': TEMPLATE_DIRS,
-        },
-    ]
+TEMPLATES = [
+    {
+        'BACKEND': 'django.template.backends.django.DjangoTemplates',
+        'APP_DIRS': True,
+        'DIRS': TEMPLATE_DIRS,
+    },
+]
diff --git a/tox.ini b/tox.ini
index f04c621..3c594a9 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
 [tox]
-envlist = coverage-py2-{dj18,dj111}-{pg,sqlite},coverage-py3-dj{111,22}-{pg,sqlite}
+envlist = coverage-py2-dj111-{pg,sqlite},coverage-py3-dj{111,22}-{pg,sqlite}
 toxworkdir = {env:TMPDIR:/tmp}/tox-{env:USER}/django-mellon/
 
 [testenv]
@@ -14,7 +14,6 @@ setenv =
 usedevelop =
     coverage: true
 deps =
-  dj18: django>=1.8,<1.9
   dj111: django>=1.11,<1.12
   dj22: django>=2.2,<2.3
   pg: psycopg2
@@ -30,7 +29,6 @@ deps =
   pytz
   lxml
   cssselect
-  dj18: django-webtest<1.9.3
   dj111: django-webtest<1.9.3
   dj22: django-webtest>1.9.3
   WebTest
-- 
2.24.1

