Skip to main content

Expiry rules for Django sessions.

Project description

django-expiry

Expiry rules for Django sessions.

Installation

Install using pip

pip install django-expiry

or Pipenv

pipenv install django-expiry

Add expiry to your INSTALLED_APPS setting

INSTALLED_APPS = (
    ...
    'expiry',
)

Add expiry.middleware.ExpirySessionMiddleware to your middleware setting

MIDDLEWARE = (
    ...
    'expiry.middleware.ExpirySessionMiddleware',
)

or to middleware classes if your Django is <= 1.9

MIDDLEWARE_CLASSES = (
    ...
    'expiry.middleware.ExpirySessionMiddleware',
)

The middleware will process rules and default ages for fresh sessions.

Usage

Ages

Default ages can be set for anonymous and authenticated users. When not set, the session age behaviour will default to Django.

EXPIRY_ANON_SESSION_AGE
Default: not set.

The default age of an anonymous session, in seconds.

EXPIRY_ANON_KEEP_ALIVE
Default: False

Keeps the authenticated session alive, refreshing its expiry for every request, according to its default value and rules.

EXPIRY_AUTH_SESSION_AGE
Default: not set.

The default age of an authenticated session, in seconds.

EXPIRY_AUTH_KEEP_ALIVE
Default: False

Keeps the anonymous session alive, refreshing its expiry for every request, according to its default value and rules.

Rules

A set of rules should be defined in your settings file. You can have rules for anonymous users and authenticated users, handled separately.

Expiry rules for authenticated users only

Processed whenever an user logs in. Its callable should always accept an user and a request object.

EXPIRY_AUTH_SESSION_RULES = (
    (lambda request, user: user.is_staff, 300),
    (lambda request, user: user.is_superuser, datetime.timedelta(weeks=2)),
    (lambda request, user: user.has_perms('hero'), 99999999),
)

Expiry rules for anonymous users only

Processed whenever a session is fresh. Rules are triggered in ExpirySessionMiddleware.

EXPIRY_ANON_SESSION_RULES = (
    (lambda request: request.META.get('REMOTE_ADDR') == '192.168.0.1', 999)
)

Rule composition

A rule is a tuple composed by:

  • A callable or the path to a callable that will validate it
  • An expiry (seconds, datetime, timedelta)

Note that, for datetime and timedelta expiries, serialization won't work unless you are using the PickleSerializer.
Read more about it here.

In the examples above, all rules are lambdas, but you can also send the path to a function that will validate it.

EXPIRY_AUTH_SESSION_RULES = (
    ('app.module.complex_rule', datetime.timedelta(days=64)),
)

Then define the rule in that specific module:

def complex_rule(user, request):
    ...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-expiry-0.2.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_expiry-0.2.1-py2-none-any.whl (4.6 kB view details)

Uploaded Python 2

File details

Details for the file django-expiry-0.2.1.tar.gz.

File metadata

  • Download URL: django-expiry-0.2.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.15

File hashes

Hashes for django-expiry-0.2.1.tar.gz
Algorithm Hash digest
SHA256 b37386f384d52b93f4ef91d4ec59c92c646e8e8813f94f2e674b35c024429131
MD5 9593352125e109e75da35e21f5151062
BLAKE2b-256 05269c396abd9a0aebe4fc2e5e13e9d7ed1c052f1935f245f08f0ef4bf29e7d3

See more details on using hashes here.

File details

Details for the file django_expiry-0.2.1-py2-none-any.whl.

File metadata

  • Download URL: django_expiry-0.2.1-py2-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.15

File hashes

Hashes for django_expiry-0.2.1-py2-none-any.whl
Algorithm Hash digest
SHA256 c22b9caa8400d044df4d67eb753281a5a823a0be1d37b8ca3d31e479c7c724f4
MD5 b1151eb83906c6ce4abede313da981c6
BLAKE2b-256 587acdd323d05112ab4cb14e3e692ca17404cacd886d4a5bde135673fbded64c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page