django-auto-logout


Namedjango-auto-logout JSON
Version 0.5.1 PyPI version JSON
download
home_pagehttps://github.com/bugov/django-auto-logout
SummaryAuto logout a user after specific time in Django
upload_time2022-12-26 15:22:59
maintainer
docs_urlNone
authorGeorgy Bazhukov
requires_python
licenseBSD
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            django-auto-logout
==================

.. image:: https://app.travis-ci.com/bugov/django-auto-logout.svg?branch=master
    :target: https://app.travis-ci.com/bugov/django-auto-logout

Auto logout a user after specific time in Django.

Works with Python >= 3.7, Django >= 3.0.

Installation
------------

.. code:: bash

    pip install django-auto-logout


Append to `settings` middlewares:

.. code:: python

    MIDDLEWARE = [
    ...
        'django_auto_logout.middleware.auto_logout',
    ]

.. note::

    Make sure that the following middlewares are used before doing this:

    - `django.contrib.sessions.middleware.SessionMiddleware`
    - `django.contrib.auth.middleware.AuthenticationMiddleware`
    - `django.contrib.messages.middleware.MessageMiddleware`

Logout in case of idle
----------------------

Logout a user if there are no requests for a long time.

Add to `settings`:

.. code:: python

    AUTO_LOGOUT = {'IDLE_TIME': 600}  # logout after 10 minutes of downtime

or the same, but with `datetime.timedelta` (more semantically):

.. code:: python

    AUTO_LOGOUT = {'IDLE_TIME': timedelta(minutes=10)}

The user will log out the next time the page is requested.
See `REDIRECT_TO_LOGIN_IMMEDIATELY` to log out right after the idle-time has expired
(and redirect to login page).

REDIRECT_TO_LOGIN_IMMEDIATELY after the idle-time has expired
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Use the `REDIRECT_TO_LOGIN_IMMEDIATELY` option
if you want to redirect the user to the login page
immediately after the idle-time expires:

.. code:: python

    from datetime import timedelta
    AUTO_LOGOUT = {
        'IDLE_TIME': timedelta(minutes=10),
        'REDIRECT_TO_LOGIN_IMMEDIATELY': True,
    }

This requires a client-side script, so you should
modify your `context_processors` in `settings.py`:

.. code:: python

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                    # !!! Add this !!!
                    'django_auto_logout.context_processors.auto_logout_client',
                ],
            },
        },
    ]

And add this to your templates (will add a redirect script to your html):

.. code:: bash

    {{ redirect_to_login_immediately }}

If you want to use this in your JavaScript code, following template variables may be useful:

.. code:: javascript

    var sessionEnd = {{ seconds_until_session_end }};
    var idleEnd = {{ seconds_until_idle_end }};

`REDIRECT_TO_LOGIN_IMMEDIATELY` works with `SESSION_TIME` too.

Limit session time
------------------

Logout a user after 3600 seconds (hour) from the last login.

Add to `settings`:

.. code:: python

    AUTO_LOGOUT = {'SESSION_TIME': 3600}

or the same, but with `datetime.timedelta` (more semantically):

.. code:: python

    AUTO_LOGOUT = {'SESSION_TIME': timedelta(hours=1)}

.. note::

    See `REDIRECT_TO_LOGIN_IMMEDIATELY` option
    if you want to redirect user to the login page
    right after the idle-time has expired.


Show messages when logging out automatically
--------------------------------------------

Set the message that will be displayed after the user automatically logs out of the system:

.. code:: python

    AUTO_LOGOUT = {
        'SESSION_TIME': 3600,
        'MESSAGE': 'The session has expired. Please login again to continue.',
    }

It uses `django.contrib.messages`. Don't forget to display messages in templates:

.. code:: html

    {% for message in messages %}
        <div class="message {{ message.tags }}">
            {{ message }}
        </div>
    {% endfor %}

.. note::

    `messages` template variable provides by `django.contrib.messages.context_processors.messages`
    context processor.

    See `TEMPLATES` - `OPTIONS` - `context_processors` in your `settings.py` file.

Combine configurations
----------------------

You can combine previous configurations. For example, you may want to logout a user
in case of downtime (5 minutes or more) and not allow working within one session
for more than half an hour:


.. code:: python

    from datetime import timedelta

    AUTO_LOGOUT = {
        'IDLE_TIME': timedelta(minutes=5),
        'SESSION_TIME': timedelta(minutes=30),
        'MESSAGE': 'The session has expired. Please login again to continue.',
        'REDIRECT_TO_LOGIN_IMMEDIATELY': True,
    }



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/bugov/django-auto-logout",
    "name": "django-auto-logout",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Georgy Bazhukov",
    "author_email": "georgy.bazhukov@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/39/7f/dc29705c97b87113a86269ce25e1e652ff50a0cf6e5cb05ed7d4d338dcac/django-auto-logout-0.5.1.tar.gz",
    "platform": null,
    "description": "django-auto-logout\n==================\n\n.. image:: https://app.travis-ci.com/bugov/django-auto-logout.svg?branch=master\n    :target: https://app.travis-ci.com/bugov/django-auto-logout\n\nAuto logout a user after specific time in Django.\n\nWorks with Python >= 3.7, Django >= 3.0.\n\nInstallation\n------------\n\n.. code:: bash\n\n    pip install django-auto-logout\n\n\nAppend to `settings` middlewares:\n\n.. code:: python\n\n    MIDDLEWARE = [\n    ...\n        'django_auto_logout.middleware.auto_logout',\n    ]\n\n.. note::\n\n    Make sure that the following middlewares are used before doing this:\n\n    - `django.contrib.sessions.middleware.SessionMiddleware`\n    - `django.contrib.auth.middleware.AuthenticationMiddleware`\n    - `django.contrib.messages.middleware.MessageMiddleware`\n\nLogout in case of idle\n----------------------\n\nLogout a user if there are no requests for a long time.\n\nAdd to `settings`:\n\n.. code:: python\n\n    AUTO_LOGOUT = {'IDLE_TIME': 600}  # logout after 10 minutes of downtime\n\nor the same, but with `datetime.timedelta` (more semantically):\n\n.. code:: python\n\n    AUTO_LOGOUT = {'IDLE_TIME': timedelta(minutes=10)}\n\nThe user will log out the next time the page is requested.\nSee `REDIRECT_TO_LOGIN_IMMEDIATELY` to log out right after the idle-time has expired\n(and redirect to login page).\n\nREDIRECT_TO_LOGIN_IMMEDIATELY after the idle-time has expired\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nUse the `REDIRECT_TO_LOGIN_IMMEDIATELY` option\nif you want to redirect the user to the login page\nimmediately after the idle-time expires:\n\n.. code:: python\n\n    from datetime import timedelta\n    AUTO_LOGOUT = {\n        'IDLE_TIME': timedelta(minutes=10),\n        'REDIRECT_TO_LOGIN_IMMEDIATELY': True,\n    }\n\nThis requires a client-side script, so you should\nmodify your `context_processors` in `settings.py`:\n\n.. code:: python\n\n    TEMPLATES = [\n        {\n            'BACKEND': 'django.template.backends.django.DjangoTemplates',\n            'DIRS': [],\n            'APP_DIRS': True,\n            'OPTIONS': {\n                'context_processors': [\n                    'django.template.context_processors.debug',\n                    'django.template.context_processors.request',\n                    'django.contrib.auth.context_processors.auth',\n                    'django.contrib.messages.context_processors.messages',\n                    # !!! Add this !!!\n                    'django_auto_logout.context_processors.auto_logout_client',\n                ],\n            },\n        },\n    ]\n\nAnd add this to your templates (will add a redirect script to your html):\n\n.. code:: bash\n\n    {{ redirect_to_login_immediately }}\n\nIf you want to use this in your JavaScript code, following template variables may be useful:\n\n.. code:: javascript\n\n    var sessionEnd = {{ seconds_until_session_end }};\n    var idleEnd = {{ seconds_until_idle_end }};\n\n`REDIRECT_TO_LOGIN_IMMEDIATELY` works with `SESSION_TIME` too.\n\nLimit session time\n------------------\n\nLogout a user after 3600 seconds (hour) from the last login.\n\nAdd to `settings`:\n\n.. code:: python\n\n    AUTO_LOGOUT = {'SESSION_TIME': 3600}\n\nor the same, but with `datetime.timedelta` (more semantically):\n\n.. code:: python\n\n    AUTO_LOGOUT = {'SESSION_TIME': timedelta(hours=1)}\n\n.. note::\n\n    See `REDIRECT_TO_LOGIN_IMMEDIATELY` option\n    if you want to redirect user to the login page\n    right after the idle-time has expired.\n\n\nShow messages when logging out automatically\n--------------------------------------------\n\nSet the message that will be displayed after the user automatically logs out of the system:\n\n.. code:: python\n\n    AUTO_LOGOUT = {\n        'SESSION_TIME': 3600,\n        'MESSAGE': 'The session has expired. Please login again to continue.',\n    }\n\nIt uses `django.contrib.messages`. Don't forget to display messages in templates:\n\n.. code:: html\n\n    {% for message in messages %}\n        <div class=\"message {{ message.tags }}\">\n            {{ message }}\n        </div>\n    {% endfor %}\n\n.. note::\n\n    `messages` template variable provides by `django.contrib.messages.context_processors.messages`\n    context processor.\n\n    See `TEMPLATES` - `OPTIONS` - `context_processors` in your `settings.py` file.\n\nCombine configurations\n----------------------\n\nYou can combine previous configurations. For example, you may want to logout a user\nin case of downtime (5 minutes or more) and not allow working within one session\nfor more than half an hour:\n\n\n.. code:: python\n\n    from datetime import timedelta\n\n    AUTO_LOGOUT = {\n        'IDLE_TIME': timedelta(minutes=5),\n        'SESSION_TIME': timedelta(minutes=30),\n        'MESSAGE': 'The session has expired. Please login again to continue.',\n        'REDIRECT_TO_LOGIN_IMMEDIATELY': True,\n    }\n\n\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "Auto logout a user after specific time in Django",
    "version": "0.5.1",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "867acf7233f44a21f6f3e3b8462fc124",
                "sha256": "0a5f7b88b78fdcc14d7359654d61f1ad264d0028b7fdbd2cb64479ab05ddbb10"
            },
            "downloads": -1,
            "filename": "django_auto_logout-0.5.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "867acf7233f44a21f6f3e3b8462fc124",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6365,
            "upload_time": "2022-12-26T15:22:57",
            "upload_time_iso_8601": "2022-12-26T15:22:57.333304Z",
            "url": "https://files.pythonhosted.org/packages/7f/51/b9d796bd72132d3f142d82f8d213d40ba3cc8423af810a2a59200fbcdc57/django_auto_logout-0.5.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "393172ea0017007ad1ce821860dbe0e5",
                "sha256": "cf5622d60ae42c9e799ab32948b98ce007be570ec34e718e5c47340793dd76f9"
            },
            "downloads": -1,
            "filename": "django-auto-logout-0.5.1.tar.gz",
            "has_sig": false,
            "md5_digest": "393172ea0017007ad1ce821860dbe0e5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5346,
            "upload_time": "2022-12-26T15:22:59",
            "upload_time_iso_8601": "2022-12-26T15:22:59.513771Z",
            "url": "https://files.pythonhosted.org/packages/39/7f/dc29705c97b87113a86269ce25e1e652ff50a0cf6e5cb05ed7d4d338dcac/django-auto-logout-0.5.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-26 15:22:59",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "bugov",
    "github_project": "django-auto-logout",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "tox": true,
    "lcname": "django-auto-logout"
}
        
Elapsed time: 0.03317s