Skip to main content

Django sessions with a foreign key to the user

Project description

Build Status Test Coverage PyPI

Django includes excellent built-in sessions, however all the data is hidden away into base64 encoded data. This makes it very difficult to run a query on all active sessions for a particular user. django-user-sessions fixes this and makes session objects a first class citizen like other ORM objects.

Also, have a look at the online example app, hosted by Heroku. It also contains the package django-two-factor-auth, but that application is not a dependency for this package. Also have a look at the bundled example templates and views to see how you can integrate the application into your project.

Compatible with Django 1.8 and 1.9 on Python 2.7, 3.2, 3.3, 3.4 and 3.5. Documentation is available at readthedocs.org.

Features

To get the list of a user’s sessions:

user.session_set.filter(expire_date__gt=now())

Or logout the user everywhere:

user.session_set.all().delete()

The user’s IP address and user agent are also stored on the session. This allows to show a list of active sessions to the user in the admin:

http://i.imgur.com/YV9Nx3f.png

And also in a custom layout:

http://i.imgur.com/d7kZtr9.png

Installation

  1. pip install django-user-sessions

  2. In INSTALLED_APPS replace 'django.contrib.sessions' with 'user_sessions'.

  3. In MIDDLEWARE_CLASSES replace 'django.contrib.sessions.middleware.SessionMiddleware' with 'user_sessions.middleware.SessionMiddleware'.

  4. Add SESSION_ENGINE = 'user_sessions.backends.db'.

  5. Add url(r'', include('user_sessions.urls', 'user_sessions')), to your urls.py.

  6. Run python manage.py syncdb (or migrate) and browse to /account/sessions/.

GeoIP

You need to setup GeoIP for the location detection to work. See the Django documentation on installing GeoIP.

Getting help

For general questions regarding this package, please hop over to Stack Overflow. If you think there is an issue with this package; check if the issue is already listed (either open or closed), and file an issue if it’s not.

Development

How to contribute

  • Fork the repository on GitHub and start hacking.

  • Run the tests.

  • Send a pull request with your changes.

  • Provide a translation using Transifex.

Running tests

This project aims for full code-coverage, this means that your code should be well-tested. Also test branches for hardened code. You can run the full test suite with:

make test

Or run a specific test with:

make test TARGET=tests.tests.MiddlewareTest

For Python compatibility, tox is used. You can run the full test suite with:

tox

Releasing

The following actions are required to push a new version:

python example/manage.py makemigrations user_sessions
git commit -am "Added migrations"

bumpversion [major|minor|patch]
git commit -am "Released [version]"
git tag [version]
python setup.py sdist bdist_wheel upload

License

This project is licensed under the MIT license.

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-user-sessions-1.3.1.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

django_user_sessions-1.3.1-py2.py3-none-any.whl (37.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-user-sessions-1.3.1.tar.gz.

File metadata

File hashes

Hashes for django-user-sessions-1.3.1.tar.gz
Algorithm Hash digest
SHA256 9d6bad44e80eb0dbad68daa0f17a79c942fd42ff7e0f88a7bad87f07e11f9076
MD5 78f4f9fa8bc1fd0892ab4902d00e90b7
BLAKE2b-256 a67cf7a594b9f0fd4e755bb66667f7de649148c91fc2ad613b8be9dc86689434

See more details on using hashes here.

File details

Details for the file django_user_sessions-1.3.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_user_sessions-1.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6d8fb630be5e93f329e7f33128128b399d3d15e81e79827dba0b19389bc2da2d
MD5 b3c155fb656e22c639ca5bcb23aebd97
BLAKE2b-256 c78c4cec83aaed76397d2c000032fcbb7eb03b737ae514921c0bf058b866298e

See more details on using hashes here.

Supported by

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