diff --git a/CHANGES b/CHANGES index 01da6fe..db1152b 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,8 @@ +0.0.3 (xxxx-xx-xx) +================== + - Redirect user to the login page after session timeout instead of the root page + + 0.0.2 (2017-11-10) ================== - Added setting to control if session should be expired X seconds after last diff --git a/src/django_session_timeout/middleware.py b/src/django_session_timeout/middleware.py index e1ca1d1..a8dc586 100644 --- a/src/django_session_timeout/middleware.py +++ b/src/django_session_timeout/middleware.py @@ -1,7 +1,7 @@ import time from django.conf import settings -from django.shortcuts import redirect +from django.contrib.auth.views import redirect_to_login try: from django.utils.deprecation import MiddlewareMixin @@ -26,7 +26,7 @@ def process_request(self, request): if session_is_expired: request.session.flush() - return redirect('/') + return redirect_to_login(next=request.path) expire_since_last_activity = getattr( settings, 'SESSION_EXPIRE_AFTER_LAST_ACTIVITY', False) diff --git a/tests/conftest.py b/tests/conftest.py index 12f9d55..c71ff96 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,6 +4,8 @@ def pytest_configure(): settings.configure( INSTALLED_APPS=[ + 'django.contrib.contenttypes', + 'django.contrib.auth', 'django.contrib.sessions' ], MIDDLEWARE_CLASSES=[], diff --git a/tests/test_middleware.py b/tests/test_middleware.py index f9c1ab2..25660f9 100644 --- a/tests/test_middleware.py +++ b/tests/test_middleware.py @@ -44,7 +44,7 @@ def test_session_expire(r, settings): with freeze_time('2017-08-31 22:46:01'): response = middleware.process_request(r) assert SESSION_TIMEOUT_KEY not in r.session - assert response['location'] == '/' + assert response['location'] == '/accounts/login/?next=/' def test_session_expire_no_expire_setting(r, settings): @@ -60,7 +60,7 @@ def test_session_expire_no_expire_setting(r, settings): with freeze_time('2017-08-31 22:46:01'): response = middleware.process_request(r) assert SESSION_TIMEOUT_KEY not in r.session - assert response['location'] == '/' + assert response['location'] == '/accounts/login/?next=/' def test_session_expire_last_activity(r, settings): @@ -80,4 +80,4 @@ def test_session_expire_last_activity(r, settings): with freeze_time('2017-08-31 23:46:02'): response = middleware.process_request(r) assert SESSION_TIMEOUT_KEY not in r.session - assert response['location'] == '/' \ No newline at end of file + assert response['location'] == '/accounts/login/?next=/' \ No newline at end of file