Subject: [PATCH django-impersonate] clean up all warnings. mostly Django 4.0 deprecation

From: Leonhard Kuboschek <[email-masked]>
Date: Thu, 19 Nov 2020 10:42:52 +0100

# HG changeset patch
# User Leonhard Kuboschek <[email-masked]>
# Date 1605778760 -3600
#      Thu Nov 19 10:39:20 2020 +0100
# Node ID 1553f65e1488b9a3ef560fd8aad0c0c04a2722a5
# Parent  926d80a74d429670db210b36fe9815462f6e4a31
clean up all warnings. mostly Django 4.0 deprecation

diff --git a/impersonate/decorators.py b/impersonate/decorators.py
--- a/impersonate/decorators.py
+++ b/impersonate/decorators.py
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
+from urllib.parse import quote
 from django.conf import settings
-from django.utils.http import urlquote
 from django.utils.encoding import force_str
 from django.shortcuts import redirect, resolve_url
 from django.contrib.auth import REDIRECT_FIELD_NAME
@@ -18,7 +18,7 @@
             return redirect(u'{0}?{1}={2}'.format(
                 get_login_url(),
                 REDIRECT_FIELD_NAME,
-                urlquote(request.get_full_path()),
+                quote(request.get_full_path()),
             ))
 
         if getattr(request.user, 'is_impersonate', False):
diff --git a/impersonate/helpers.py b/impersonate/helpers.py
--- a/impersonate/helpers.py
+++ b/impersonate/helpers.py
@@ -69,7 +69,7 @@
         return custom_queryset_func(request)
     else:
         qs = User.objects.all()
-        if not User._meta.ordering:
+        if not qs.query.order_by:
             qs = qs.order_by('pk')
         return qs
 
diff --git a/impersonate/signals.py b/impersonate/signals.py
--- a/impersonate/signals.py
+++ b/impersonate/signals.py
@@ -11,19 +11,18 @@
 logger = logging.getLogger(__name__)
 
 # signal sent when an impersonation session begins
-session_begin = Signal(
-    providing_args=['impersonator', 'impersonating', 'request']
-)
+# providing_args=['impersonator', 'impersonating', 'request']
+session_begin = Signal()
 
 # signal sent when an impersonation session ends
-session_end = Signal(
-    providing_args=['impersonator', 'impersonating', 'request']
-)
+# providing_args=['impersonator', 'impersonating', 'request']
+session_end = Signal()
 
+ID_LENGTH = 12
 
 def gen_unique_id():
     return hashlib.sha1(
-        u'{0}:{1}'.format(get_random_string(), tz_now()).encode('utf-8')
+        u'{0}:{1}'.format(get_random_string(ID_LENGTH), tz_now()).encode('utf-8')
     ).hexdigest()

diff --git a/impersonate/tests.py b/impersonate/tests.py
--- a/impersonate/tests.py
+++ b/impersonate/tests.py
@@ -24,7 +24,7 @@
 from urllib.parse import urlencode, urlsplit
 
 import django
-from django.conf.urls import include, url
+from django.urls import include, path
 from django.contrib.admin.sites import AdminSite
 from django.contrib.auth import get_user_model
 from django.http import HttpResponse
@@ -54,9 +54,9 @@

 urlpatterns = [
-    url(r'^test-view/$', test_view, name='impersonate-test'),
-    url(r'^another-view/$', test_view, name='another-test-view'),
-    url('^', include('impersonate.urls')),
+    path('test-view/', test_view, name='impersonate-test'),
+    path('another-view/', test_view, name='another-test-view'),
+    path('', include('impersonate.urls')),
 ]

@@ -78,7 +78,7 @@
     ''' Used via the IMPERSONAT['CUSTOM_USER_QUERYSET'] setting.
         Simple function to return all users.
     '''
-    return User.objects.all()
+    return User.objects.all().order_by('pk')

 class UserFactory(object):
@@ -103,8 +103,12 @@
             username='superuser', is_superuser=True,
         )
         self.user = UserFactory.create(username='regular')
+
         self.factory = RequestFactory()
-        self.middleware = ImpersonateMiddleware()
+
+        def dummy_get_response(request):
+            return None
+        self.middleware = ImpersonateMiddleware(dummy_get_response)
 
     def _impersonated_request(self, use_id=True):
         request = self.factory.get('/')
diff --git a/impersonate/urls.py b/impersonate/urls.py
--- a/impersonate/urls.py
+++ b/impersonate/urls.py
@@ -1,22 +1,22 @@
 # -*- coding: utf-8 -*-
-from django.conf.urls import url
+from django.urls import path
 
 from . import views

 urlpatterns = [
-    url(r'^stop/$',
+    path('stop/',
         views.stop_impersonate,
         name='impersonate-stop'),
-    url(r'^list/$',
+    path('list/',
         views.list_users,
         {'template': 'impersonate/list_users.html'},
         name='impersonate-list'),
-    url(r'^search/$',
+    path('search/',
         views.search_users,
         {'template': 'impersonate/search_users.html'},
         name='impersonate-search'),
-    url(r'^(?P<uid>.+)/$',
+    path('<path:uid>/',
         views.impersonate,
         name='impersonate-start'),
 ]