diff --git a/open_api_framework/admin.py b/open_api_framework/admin.py new file mode 100644 index 0000000..14b99c2 --- /dev/null +++ b/open_api_framework/admin.py @@ -0,0 +1,41 @@ +from importlib import import_module + +from django.conf import settings +from django.contrib import admin + +from sessionprofile.models import SessionProfile + + +@admin.register(SessionProfile) +class SessionProfileAdmin(admin.ModelAdmin): + list_display = ["session_key", "user", "expiration_time", "exists"] + + def __init__(self, model, admin_site): + engine = import_module(settings.SESSION_ENGINE) + self.SessionStore = engine.SessionStore + super().__init__(model, admin_site) + + def get_queryset(self, request): + qs = super().get_queryset(request) + return qs.filter(user=request.user) + + def has_add_permission(self, request, obj=None): + return False + + def has_change_permission(self, request, obj=None): + return False + + @admin.display(boolean=True) + def exists(self, obj): + return self.SessionStore().exists(obj.session_key) + + def expiration_time(self, obj): + + if self.SessionStore().exists(obj.session_key): + session = self.SessionStore(obj.session_key) + return session.get_expiry_date() + return None + + def delete_model(self, request, obj): + self.SessionStore().flush(obj.session_key) + super().delete_model(request, obj) diff --git a/open_api_framework/conf/base.py b/open_api_framework/conf/base.py index fe2ffde..523772c 100644 --- a/open_api_framework/conf/base.py +++ b/open_api_framework/conf/base.py @@ -539,8 +539,8 @@ SESSION_ENGINE = "django.contrib.sessions.backends.cache" SESSION_COOKIE_AGE = config( "SESSION_COOKIE_AGE", - default=1209600, - help_text="For how long, in seconds, the session cookie will be valid." + default=1209600, + help_text="For how long, in seconds, the session cookie will be valid.", ) diff --git a/pyproject.toml b/pyproject.toml index d21574b..e8efd8b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -56,6 +56,7 @@ dependencies = [ "flower>=2.0.1", "maykin-2fa>=1.0.1", "django-setup-configuration>=0.1.0", + "django-sessionprofile>=3.0.0", ] [project.urls] diff --git a/testapp/settings.py b/testapp/settings.py index 9aecf64..ab43b01 100644 --- a/testapp/settings.py +++ b/testapp/settings.py @@ -44,11 +44,13 @@ "django.contrib.messages", "django.contrib.admin", "open_api_framework", + "sessionprofile", "testapp", ] MIDDLEWARE = [ "django.middleware.security.SecurityMiddleware", + "sessionprofile.middleware.SessionProfileMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware",