Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add settings to control redirect #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,10 @@ To redirect to a custom URL define the following setting:
```python
SESSION_TIMEOUT_REDIRECT = 'your_redirect_url_here/'
```


To disable redirect add :

```python
SESSION_TIMEOUT_TO_REDIRECT = False
```
8 changes: 6 additions & 2 deletions src/django_session_timeout/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ def process_request(self, request):
if session_is_expired:
request.session.flush()
redirect_url = getattr(settings, "SESSION_TIMEOUT_REDIRECT", None)
if redirect_url:
to_redirect = getattr(settings, "SESSION_TIMEOUT_TO_REDIRECT", True)

if redirect_url and to_redirect:
return redirect(redirect_url)
else:
elif to_redirect:
return redirect_to_login(next=request.path)
else:
return

expire_since_last_activity = getattr(
settings, "SESSION_EXPIRE_AFTER_LAST_ACTIVITY", False
Expand Down
35 changes: 35 additions & 0 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,38 @@ def test_session_expire_last_activity_grace_not_update(r, settings):
response = middleware.process_request(r)
assert SESSION_TIMEOUT_KEY not in r.session
assert response["location"] == "/accounts/login/?next=/"


def test_session_expire_to_redirect(r, settings):
settings.SESSION_EXPIRE_SECONDS = 3600
settings.SESSION_TIMEOUT_REDIRECT = "/foobar/"
settings.SESSION_TIMEOUT_TO_REDIRECT = True
middleware = SessionTimeoutMiddleware()

with freeze_time("2017-08-31 21:46:00"):
assert middleware.process_request(r) is None

with freeze_time("2017-08-31 22:46:01"):
response = middleware.process_request(r)
assert response["location"] == "/foobar/"

settings.SESSION_TIMEOUT_TO_REDIRECT = False
response = middleware.process_request(r)
assert response is None

def test_session_expire_to_redirect_login(r, settings):
settings.SESSION_EXPIRE_SECONDS = 3600
settings.SESSION_TIMEOUT_TO_REDIRECT = True
settings.SESSION_TIMEOUT_REDIRECT = None
middleware = SessionTimeoutMiddleware()

with freeze_time("2017-08-31 21:46:00"):
assert middleware.process_request(r) is None

with freeze_time("2017-08-31 22:46:01"):
response = middleware.process_request(r)
assert response["location"] == "/accounts/login/?next=/"

settings.SESSION_TIMEOUT_TO_REDIRECT = False
response = middleware.process_request(r)
assert response is None