Code

Ticket #159: ticket159.diff

File ticket159.diff, 2.5 KB (added by ashchristopher, 3 years ago)

Needs review, but couldn't think of a nicer way without major changes to the admin views.

Line 
1diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py
2old mode 100644
3new mode 100755
4index 3a5c12b..03deab2
5--- a/django/contrib/admin/sites.py
6+++ b/django/contrib/admin/sites.py
7@@ -14,6 +14,7 @@ from django.utils.text import capfirst
8 from django.utils.translation import ugettext as _
9 from django.views.decorators.cache import never_cache
10 from django.conf import settings
11+from django.http import HttpResponseRedirect
12 
13 LOGIN_FORM_KEY = 'this_is_the_login_form'
14 
15@@ -188,7 +189,10 @@ class AdminSite(object):
16         """
17         def inner(request, *args, **kwargs):
18             if not self.has_permission(request):
19-                return self.login(request)
20+                extra_context = {}
21+                if request.path.endswith(reverse('admin:logout')):
22+                    extra_context[REDIRECT_FIELD_NAME] = reverse('admin:index')
23+                return self.login(request, extra_context)
24             return view(request, *args, **kwargs)
25         if not cacheable:
26             inner = never_cache(inner)
27diff --git a/django/contrib/auth/views.py b/django/contrib/auth/views.py
28old mode 100644
29new mode 100755
30diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
31old mode 100644
32new mode 100755
33index b6e7b9e..95d935d
34--- a/tests/regressiontests/admin_views/tests.py
35+++ b/tests/regressiontests/admin_views/tests.py
36@@ -3255,3 +3255,25 @@ class AdminCustomSaveRelatedTests(TestCase):
37 
38         self.assertEqual('Josh Stone', Parent.objects.latest('id').name)
39         self.assertEqual([u'Catherine Stone', u'Paul Stone'], children_names)
40+
41+
42+class AdminViewLogoutTest(TestCase):
43+    urls = "regressiontests.admin_views.urls"
44+    fixtures = ['admin-views-users.xml', ]
45+
46+    def setUp(self):
47+        self.client.login(username='super', password='secret')
48+
49+    def tearDown(self):
50+        self.client.logout()
51+
52+    def test_client_logout_url_can_be_used_to_login(self):
53+        response = self.client.get('/test_admin/admin/logout/')
54+        self.assertEqual(response.status_code, 200)
55+        self.assertEqual(response.template_name, 'registration/logged_out.html')
56+        self.assertEqual(response.request['PATH_INFO'], '/test_admin/admin/logout/')
57+
58+        # we are now logged out - issue a GET request to `/test_admin/admin/logout/`
59+        response = self.client.get('/test_admin/admin/logout/')
60+        self.assertTrue(
61+            '<input type="hidden" name="next" value="/test_admin/admin/" />' in response.content)