diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b997526 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.log +*.pot +*.pyc +local_settings.py +django_session_idle_timeout.egg-info/ +build/ +dist/ diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..89d644b --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,2 @@ +include README.md +recursive-include django-session-idle-timeout/templates * diff --git a/README b/README deleted file mode 100644 index e3380c5..0000000 --- a/README +++ /dev/null @@ -1,15 +0,0 @@ -A Django middleware application to timeout a logged in user session after a specified time period. - -Installation instructions: -- Make sure the following Django apps/middlewares are enabled. - - Authentication (http://docs.djangoproject.com/en/dev/topics/auth/) - - Sessions (http://docs.djangoproject.com/en/dev/topics/http/sessions/) - - Messages framework (http://docs.djangoproject.com/en/dev/ref/contrib/messages/) -- Place the checked out 'sessions' directory in your project. -- Make the following entries in your 'settings.py' project module. - - Append an entry to the MIDDLEWARE_CLASSES as 'sessions.middleware.SessionIdleTimeout', at the bottom. - - Add 'sessions' to INSTALLED_APPS. (Optional) - - Add a entry named 'SESSION_IDLE_TIMEOUT' to specify the idle timeout period, in seconds. - -That's it, you will receive a session timeout message using the Django message framework in your templates. - diff --git a/README.md b/README.md new file mode 100644 index 0000000..dc5923c --- /dev/null +++ b/README.md @@ -0,0 +1,90 @@ +# django-session-idle-timeout + +A Django middleware application to timeout a logged in user +session after a specified time period. +A django message will be issued if the session gets timed out. + +## Requirements + +Make sure the following Django apps and middlewares are enabled: +* Authentication (http://docs.djangoproject.com/en/dev/topics/auth/) +* Sessions (http://docs.djangoproject.com/en/dev/topics/http/sessions/) +* Messages framework (http://docs.djangoproject.com/en/dev/ref/contrib/messages/) + +```python +INSTALLED_APPS += ( + 'django.contrib.auth', + 'django.contrib.sessions', + 'django.contrib.messages', +) +``` + +```python +MIDDLEWARE_CLASSES += ( + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', +) +``` + +## Installation instructions + +Install django-session-idle-timeout +```bash +easy_install django-session-idle-timeout +``` + +or + +```bash +pip install django-session-idle-timeout +``` + +### Installed apps + +```python +INSTALLED_APPS += ( + 'django-session-idle-timeout', +) +``` + +### Middleware + +```python +MIDDLEWARE_CLASSES += ( + 'django-session-idle-timeout.middleware.SessionIdleTimeout', +) +``` + +### Settings + +SESSION_IDLE_TIMEOUT defines the period after which the session gets timed out in seconds. +The default value is 30min. + +```python +SESSION_IDLE_TIMEOUT = 1800 +``` + +## Keepalive Ping + +Keeps the session alive as long as the browser window is opened using a javascript ping. + +### Urls + +Add to your urls.py e.g.: +```python +urlpatterns += ( + url(r'^django-session-idle-timeout/', include('django-session-idle-timeout.urls')), +) +``` + +### Template + +Load the keepalive template tag to start the javascript polling: +```django +{% session_keep_alive %} +{% load session_keep_alive %} +``` + +## Important +This is a fork of http://github.com/subhranath/django-session-idle-timeout diff --git a/sessions/__init__.py b/django-session-idle-timeout/__init__.py similarity index 100% rename from sessions/__init__.py rename to django-session-idle-timeout/__init__.py diff --git a/django-session-idle-timeout/middleware.py b/django-session-idle-timeout/middleware.py new file mode 100644 index 0000000..e73efff --- /dev/null +++ b/django-session-idle-timeout/middleware.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- + +import time +from distutils.version import StrictVersion + +import django +from django.contrib.auth import logout +from django.contrib import messages +from django.conf import settings +from django.utils.translation import ugettext as _ + +SESSION_IDLE_TIMEOUT = getattr(settings, 'SESSION_IDLE_TIMEOUT', 1800) + +if StrictVersion(django.get_version()) >= StrictVersion('1.10'): + from django.utils.deprecation import MiddlewareMixin +else: + MiddlewareMixin = object + + +class SessionIdleTimeout(MiddlewareMixin): + """Middleware class to timeout a session after a specified time period. + """ + def process_request(self, request): + # Timeout is done only for authenticated logged in users. + if request.user.is_authenticated(): + current_timestamp = int(time.time()) + + # Timeout if idle time period is exceeded. + if request.session.has_key('last_activity') and \ + (current_timestamp - request.session['last_activity']) > \ + SESSION_IDLE_TIMEOUT: + logout(request) + messages.add_message(request, messages.ERROR, + _('Your session has been timed out.')) + else: + request.session['last_activity'] = current_timestamp + return None diff --git a/django-session-idle-timeout/templates/sessionidletimeout/js.html b/django-session-idle-timeout/templates/sessionidletimeout/js.html new file mode 100644 index 0000000..1432dc9 --- /dev/null +++ b/django-session-idle-timeout/templates/sessionidletimeout/js.html @@ -0,0 +1,12 @@ +{% load url from future %} +{% load l10n %} + diff --git a/django-session-idle-timeout/templatetags/__init__.py b/django-session-idle-timeout/templatetags/__init__.py new file mode 100644 index 0000000..8d98fed --- /dev/null +++ b/django-session-idle-timeout/templatetags/__init__.py @@ -0,0 +1 @@ +# -*- coding: UTF-8 -*- diff --git a/django-session-idle-timeout/templatetags/session_keep_alive.py b/django-session-idle-timeout/templatetags/session_keep_alive.py new file mode 100644 index 0000000..08bfe5b --- /dev/null +++ b/django-session-idle-timeout/templatetags/session_keep_alive.py @@ -0,0 +1,10 @@ +# -*- coding: UTF-8 -*- +from django import template +from django.conf import settings + +register = template.Library() +@register.inclusion_tag('sessionidletimeout/js.html', takes_context=True) +def session_keep_alive(context): + return context.update({ + 'session_keepalive_interval': int(getattr(settings, 'SESSION_IDLE_TIMEOUT', 1800)) / 2 * 1000, + }) diff --git a/django-session-idle-timeout/urls.py b/django-session-idle-timeout/urls.py new file mode 100644 index 0000000..724ff5f --- /dev/null +++ b/django-session-idle-timeout/urls.py @@ -0,0 +1,8 @@ +# -*- coding: UTF-8 -*- + +from django.conf.urls import patterns, url +import views + +urlpatterns = patterns('', + url(r"^/?", views.PingView.as_view(), name="django-session-idle-timeout_ping"), +) diff --git a/django-session-idle-timeout/views.py b/django-session-idle-timeout/views.py new file mode 100644 index 0000000..f420872 --- /dev/null +++ b/django-session-idle-timeout/views.py @@ -0,0 +1,7 @@ +# -*- coding: UTF-8 -*- +from django.views.generic import View +from django.http import HttpResponse + +class PingView(View): + def get(self, request): + return HttpResponse('pong') diff --git a/sessions/middleware.py b/sessions/middleware.py deleted file mode 100644 index da9b024..0000000 --- a/sessions/middleware.py +++ /dev/null @@ -1,24 +0,0 @@ -from django.contrib.auth import logout -from django.contrib import messages -import datetime - -import settings - -class SessionIdleTimeout: - """Middleware class to timeout a session after a specified time period. - """ - def process_request(self, request): - # Timeout is done only for authenticated logged in users. - if request.user.is_authenticated(): - current_datetime = datetime.datetime.now() - - # Timeout if idle time period is exceeded. - if request.session.has_key('last_activity') and \ - (current_datetime - request.session['last_activity']).seconds > \ - settings.SESSION_IDLE_TIMEOUT: - logout(request) - messages.add_message(request, messages.ERROR, 'Your session has been timed out.') - # Set last activity time in current session. - else: - request.session['last_activity'] = current_datetime - return None diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..795b9ad --- /dev/null +++ b/setup.py @@ -0,0 +1,39 @@ +from setuptools import setup, find_packages + +setup( + name = "django-session-idle-timeout", + install_requires = [ + 'Django', + ], + packages = find_packages(), + include_package_data=True, + version = "1.4.2", + description = "Timeout a logged user after a period of time", + long_description=open('README.md').read(-1), + author = "Tomas Zulberti", + zip_safe = False, + author_email = "tzulberti@gmail.com", + url = "http://github.com/tzulberti/django-session-idle-timeout", + keywords = [ + "django contrib", + "session login expiration" + ], + classifiers = [ + 'Development Status :: 5 - Production/Stable', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: BSD License', + 'Framework :: Django', + 'Programming Language :: Python', + 'Programming Language :: Python :: 2.4', + 'Programming Language :: Python :: 2.5', + 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2.7', + 'Topic :: Software Development :: Libraries :: Python Modules', + 'Topic :: Internet :: WWW/HTTP :: WSGI', + 'Topic :: Software Development :: Libraries :: Application Frameworks', + 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', + 'Environment :: Web Environment', + 'Operating System :: OS Independent' + ], + license = 'License :: OSI Approved :: BSD License', +)