From f7ffe2579cfe1728cfd408909b4a60b422e26bfd Mon Sep 17 00:00:00 2001 From: Denis Shatov Date: Wed, 21 Aug 2024 14:48:27 +0700 Subject: [PATCH 1/3] Remove unused 'middlewares.UserModel' --- django_auto_logout/middleware.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/django_auto_logout/middleware.py b/django_auto_logout/middleware.py index 60e7c9c..2921a11 100644 --- a/django_auto_logout/middleware.py +++ b/django_auto_logout/middleware.py @@ -2,12 +2,11 @@ from typing import Callable from django.conf import settings from django.http import HttpRequest, HttpResponse -from django.contrib.auth import get_user_model, logout +from django.contrib.auth import logout from django.contrib.messages import info from .utils import now, seconds_until_idle_time_end, seconds_until_session_end -UserModel = get_user_model() logger = logging.getLogger(__name__) From 6ce8c392d74a4e880ea223f24ba37397eb39e80e Mon Sep 17 00:00:00 2001 From: Denis Shatov Date: Wed, 21 Aug 2024 15:32:23 +0700 Subject: [PATCH 2/3] Extract 'middleware._do_logout()' --- django_auto_logout/middleware.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/django_auto_logout/middleware.py b/django_auto_logout/middleware.py index 2921a11..abb9e78 100644 --- a/django_auto_logout/middleware.py +++ b/django_auto_logout/middleware.py @@ -1,5 +1,5 @@ import logging -from typing import Callable +from typing import Callable, Optional from django.conf import settings from django.http import HttpRequest, HttpResponse from django.contrib.auth import logout @@ -10,7 +10,14 @@ logger = logging.getLogger(__name__) -def _auto_logout(request: HttpRequest, options): +def _do_logout(request: HttpRequest, options) -> None: + logout(request) + + if 'MESSAGE' in options: + info(request, options['MESSAGE']) + + +def _auto_logout(request: HttpRequest, options) -> Optional[HttpResponse]: should_logout = False current_time = now() @@ -31,16 +38,14 @@ def _auto_logout(request: HttpRequest, options): if should_logout: logger.debug('Logout user %s', request.user) - logout(request) - - if 'MESSAGE' in options: - info(request, options['MESSAGE']) + return _do_logout(request, options) def auto_logout(get_response: Callable[[HttpRequest], HttpResponse]) -> Callable: def middleware(request: HttpRequest) -> HttpResponse: + response = None if not request.user.is_anonymous and hasattr(settings, 'AUTO_LOGOUT'): - _auto_logout(request, settings.AUTO_LOGOUT) + response = _auto_logout(request, settings.AUTO_LOGOUT) - return get_response(request) + return response or get_response(request) return middleware From 82fccef691036ef19d952945a6892961a9225924 Mon Sep 17 00:00:00 2001 From: Denis Shatov Date: Wed, 21 Aug 2024 15:54:16 +0700 Subject: [PATCH 3/3] Allow to specify custom user logout function --- django_auto_logout/middleware.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/django_auto_logout/middleware.py b/django_auto_logout/middleware.py index abb9e78..a21320a 100644 --- a/django_auto_logout/middleware.py +++ b/django_auto_logout/middleware.py @@ -1,9 +1,11 @@ import logging -from typing import Callable, Optional +from typing import Any, Callable, Optional from django.conf import settings +from django.core.exceptions import ImproperlyConfigured from django.http import HttpRequest, HttpResponse from django.contrib.auth import logout from django.contrib.messages import info +from django.utils.module_loading import import_string from .utils import now, seconds_until_idle_time_end, seconds_until_session_end @@ -38,14 +40,27 @@ def _auto_logout(request: HttpRequest, options) -> Optional[HttpResponse]: if should_logout: logger.debug('Logout user %s', request.user) - return _do_logout(request, options) + logout_func = options.get('CUSTOM_LOGOUT_FUNC', _do_logout) + return logout_func(request, options) def auto_logout(get_response: Callable[[HttpRequest], HttpResponse]) -> Callable: + options: Optional[dict[str, Any]] = getattr(settings, 'AUTO_LOGOUT', None) + if not options: + logger.warning('Auto logout settings are not specified') + elif 'CUSTOM_LOGOUT_FUNC' in options: + custom_logout_func_value = options['CUSTOM_LOGOUT_FUNC'] + if isinstance(custom_logout_func_value, str): + options['CUSTOM_LOGOUT_FUNC'] = import_string(custom_logout_func_value) + + if not callable(options['CUSTOM_LOGOUT_FUNC']): + raise ImproperlyConfigured("CUSTOM_LOGOUT_FUNC should be a function") + def middleware(request: HttpRequest) -> HttpResponse: response = None - if not request.user.is_anonymous and hasattr(settings, 'AUTO_LOGOUT'): - response = _auto_logout(request, settings.AUTO_LOGOUT) + if not request.user.is_anonymous and options is not None: + response = _auto_logout(request, options) return response or get_response(request) + return middleware