Package: django-qsessions
django-qsessionsoffers two extended session backends for Django.
They extend Django’sdbandcached_dbbackends (andSessionmodel)
with following extra features:Sessions have a foreign key to UserSessions store IP and User AgentThese features help you implement “Session Management” and show a list of active sessions to the user.
You can display IP, location and user agent for each session and add an option to revoke sessions.ComparisonHere is a brief comparison between Django’s session backends (db, cache, cached_db), and django-qsessions.djangoqsessionscachedbcached_dbdbcached_dbPerformance✔✔✔✔Persistence✔✔✔✔Foreign Key to User✔✔Store IP and User Agent✔✔CompatibilityPython:3.7,3.8,3.9,3.10Django:3.2,4.0,4.1InstallationIf your system is in production and there are active sessions using another session backend,
you need to migrate them manually. We have no migration script.If you want to use thecached_dbbackend, make sure you’veconfigured your cache. If you have multiple caches defined inCACHES, Django
will use the default cache. To use another cache, setSESSION_CACHE_ALIASto the
name of that cache.Install the latest version from PyPI:pipinstalldjango-qsessionsIn settings:InINSTALLED_APPSreplace'django.contrib.sessions'with'qsessions'.InMIDDLEWAREorMIDDLEWARE_CLASSESreplace'django.contrib.sessions.middleware.SessionMiddleware'with'qsessions.middleware.SessionMiddleware'.SetSESSION_ENGINEto:'qsessions.backends.cached_db'if you want to usecached_dbbackend.'qsessions.backends.db'if you want to usedbbackend.Run migrations to createqsessions.models.Sessionmodel.pythonmanage.pymigrateqsessionsTo enable location detection using GeoIP2 (optional):Installgeoip2package:pipinstallgeoip2SetGEOIP_PATHto a directory for storing GeoIP2 database.Run the following command to download latest GeoIP2 database. You can add this
command to a cron job to update GeoIP2 DB automatically.
Due toMaxmind license changesyou will need to acquire and use a license key for
downloading the databases.  You can pass the key on the command line, or in theMAXMIND_LICENSE_KEYenvironment variable.pythonmanage.pydownload_geoip_db-kmykeyUsagedjango-qsessions has a customSessionmodel with following extra fields:user,user_agent,created_at,updated_at,ip.Getting a user’s sessions:user.session_set.filter(expire_date__gt=timezone.now())Deleting a session:# Deletes session from both DB and cachesession.delete()Logout a user:user.session_set.all().delete()Session creation time (user login time):>>>session.created_atdatetime.datetime(2018,6,12,17,9,17,443909,tzinfo=<UTC>)IP and user agent:>>>session.ip'127.0.0.1'>>>session.user_agent'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'And if you have configured GeoIP2, you can call.location(),.location_info():>>>session.location()'Tehran, Iran'>>>session.location_info(){'city':'Tehran','continent_code':'AS','continent_name':'Asia','country_code':'IR','country_name':'Iran','time_zone':'Asia/Tehran',...}Admin page:Caveatssession.updated_atis not the session’s last activity. It’s updated each time the session
object in DB is saved. (e.g. when user logs in, or when ip, user agent, or session data changes)Why notdjango-user-sessions?django-user-sessionshas the same functionality,
but only extends thedbbackend. Using a cache can improve performance.We got ideas and some codes from django-user-sessions.
Many thanks toBouke Haarsmafor writing django-user-sessions.DevelopmentCreate and activate a python virtualenv.Install development dependencies in your virtualenv withpip install-e'.[dev]'Install pre-commit hooks withpre-commitinstallRun tests with coverage:py.test--cov--dstests.settings_dbpy.test--cov--dstests.settings_cached_dbTODOWrite better documentation.Explain how it works (in summary)Add more details to existing documentation.Write more testsPerformance benchmark (and compare with Django’scached_db)Contributions are welcome!LicenseMIT