From: Guillaume Pellerin Date: Mon, 18 May 2026 08:24:13 +0000 (+0200) Subject: add LastUserActivityMiddleware to track user activity X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=53a3e366e3de9af158a88f602d6d86331edf6224;p=teleforma.git add LastUserActivityMiddleware to track user activity --- diff --git a/app/settings.py b/app/settings.py index be381fc8..193a94d2 100644 --- a/app/settings.py +++ b/app/settings.py @@ -308,6 +308,7 @@ MIDDLEWARE = (('debug_toolbar.middleware.DebugToolbarMiddleware',) if DEBUG_TOOL 'django.middleware.locale.LocaleMiddleware', 'dj_pagination.middleware.PaginationMiddleware', 'teleforma.middleware.XsSharing', + 'teleforma.middleware.LastUserActivityMiddleware', 'django_user_agents.middleware.UserAgentMiddleware', "django.middleware.common.CommonMiddleware", ) @@ -617,6 +618,8 @@ DATA_UPLOAD_MAX_NUMBER_FIELDS = 10240 #THUMBNAIL_FORCE_OVERWRITE = True +LAST_ACTIVITY_INTERVAL_SECS = 3600 + ######################## # EMAIL diff --git a/teleforma/middleware.py b/teleforma/middleware.py index 898bf3d9..6fe9b45b 100644 --- a/teleforma/middleware.py +++ b/teleforma/middleware.py @@ -1,7 +1,9 @@ - - -from django import http +from datetime import timedelta +from django.utils import timezone from django.conf import settings +from django.db.models.expressions import F +from django import http + try: XS_SHARING_ALLOWED_ORIGINS = settings.XS_SHARING_ALLOWED_ORIGINS XS_SHARING_ALLOWED_METHODS = settings.XS_SHARING_ALLOWED_METHODS @@ -52,3 +54,26 @@ class XsSharing: XS_SHARING_ALLOWED_HEADERS) return self.get_response(request) + + +class LastUserActivityMiddleware: + KEY = "last-activity" + + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + if request.user.is_authenticated: + last_activity = request.session.get(self.KEY) + too_old_time = timezone.now() - timedelta(seconds=settings.LAST_ACTIVITY_INTERVAL_SECS) + + # Update database only if the session timestamp is old enough + if not last_activity or last_activity < too_old_time: + # Update your custom UserActivity or UserProfile model + request.user.useractivity.last_activity_date = timezone.now() + request.user.useractivity.save() + + # Update the session timestamp + request.session[self.KEY] = timezone.now().isoformat() + + return self.get_response(request) \ No newline at end of file diff --git a/teleforma/views/crfpa.py b/teleforma/views/crfpa.py index 0bb287db..9cf737f8 100644 --- a/teleforma/views/crfpa.py +++ b/teleforma/views/crfpa.py @@ -1182,6 +1182,7 @@ class WriteView(PostmanWriteView): return context + class CRFPAProfileView(ProfileView): """Provide Collections web UI methods"""