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):
...
Metadata
Release files for django-expiry 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-expiry-0.2.3.tar.gz | 3.9 kB | Without provenance | Details for django-expiry-0.2.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_expiry-0.2.3-py3-none-any.whl | Python 3 | none | any | Details for django_expiry-0.2.3-py3-none-any.whl |
Total release size: 9.7 kB
Release files / django-expiry-0.2.3.tar.gz
| Download URL | django-expiry-0.2.3.tar.gz |
|---|---|
| Size | 3.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
aeb856588ac92cc329357beba443f41d579b114eada58449e4787f0483c2a2d1
|
|
BLAKE2b-256 checksum How to use checksums |
0fa5e127b2c1168cf6a6d2ae14638a59cb02b80f4417205f1e48f324d55cd681
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1
|
Release files / django_expiry-0.2.3-py3-none-any.whl
| Download URL | django_expiry-0.2.3-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5a9f445639ec57457d392ee623ddab3f36b7de0502fadb2fb0c5afa96494447b
|
|
BLAKE2b-256 checksum How to use checksums |
5a8b70c5a146607f4fa3b89078b51800fe8e0203405ee858d6f7051595660dac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1
|
Security for django-expiry 0.2.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.