Skip to content

Commit

Permalink
✨[#46] add session profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Coperh committed Oct 11, 2024
1 parent 19839d7 commit 14afa0a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
41 changes: 41 additions & 0 deletions open_api_framework/admin.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions open_api_framework/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
)


Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 14afa0a

Please sign in to comment.