Skip to main content

Django Token User Visit

Code style: black CI badge

Django app for recording daily user visits

Project Description

This app consists of middleware to record user visits, and a single TokenUserVisit model to capture that data.

There are two Middleware components available: TokenUserVisitMiddleware does the work of logging user visit. However, it depends on the request.user being established. This is not the case if the user is using token based authentication. RequestUserSetterMiddleware establishes the request.user instance if it has not yet been set.

The principal behind this is not to record every single request made by a user. It is to record each daily visit to a site.

This app is configured to track user visits using both Sessions and Authentication tokens. Since the goal is to record a daily visit per ip per device, only the first visit from each device and IP and login will be recorded each day. However, if a user logs out, causing the session or authentication token to be purged, a new daily visit may be recorded on the same day. Likewise, if a user logs in from several different devices and different IP addresses, a new user visit will be recorded for each device.

The goal is to record unique daily visits per user 'context' ( where context is the location / device combo).

Requirements

  • Python 3.8+
  • Django 3.2, 4.1, 4.2
  • Python User Agents 2.1+

Installation

Install with pip:

pip install django-token-user-visit

Add django-token-user-visit to your INSTALLED_APPS

INSTALLED_APPS = [
    ...
    "token_user_visit",
]

Add the Django Token User Visit Middleware:

MIDDLEWARE = [
    ...
    "token_user_visit.middleware.RequestUserSetterMiddleware",
    ...
    "token_user_visit.middleware.TokenUserVisitMiddleware",
]

NOTE: The RequestUserSetterMiddleware must come before the TokenUserVisitMiddleware and must follow the SessionMiddleware and any authentication middleware you are using.

Settings

TOKEN_USER_VISIT_RECORDING_DISABLED

Default: False

This can be set as an environment variable or in your settings.py and if set to True user visits are not recorded.

TOKEN_USER_VISIT_RQUEST_CONTEXT_EXTRACTOR

Default: {}

This is set to a function that returns a dictionary of additional information to be stored. An example might be GeoIP information.

TOKEN_USER_VISIT_CONTEXT_ENCODER

Default: DjangoJSONEncoder

This can override the JSON encoder used for context JSON fields

TOKEN_USER_VISIT_RECORDING_BYPASS

Default: False

This setting is a function that takes an HttpRequest and returns a boolean. If it evaluates to true, the user visit is not recorded. For instance if you want to ignore visits to Django Admin, you could add this to settings.py

TOKEN_USER_VISIT_RECORDING_BYPASS = lambda request: request.path.startswith("/admin/")

TOKEN_USER_VISIT_DUPLICATE_LOG_LEVEL

Default: warning

The log level to use when logging duplicate hashes. This is WARNING by default, but if it's noisy you can turn this down by setting this value. Must be one of "debug", "info", "warning", "error"

TOKEN_USER_VISIT_SESSION_ACTIVATOR

Default: False

By default, the Middleware logs a token first even if there is an active session. This setting is a function that takes an HttpRequest and returns a boolean. If True, it logs the session instead of the token.

TOKEN_USER_VISIT_AUTHENTICATION_CLASS

Default: None Required

The authentication class used for Token based authentication.

TOKEN_USER_VISIT_KEYWORD

Default: Bearer

This allows you to modify the Keyword identifying the token to something else. Generally this will be Bearer

Previews

Admin list view:

UserVisit list view

Admin edit view:

UserVisit edit view

Metadata

Release files for django-token-user-visit 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-token-user-visit 0.1.2
File Size Uploaded Provenance Actions
django_token_user_visit-0.1.2.tar.gz 10.0 kB Without provenance Details for django_token_user_visit-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-token-user-visit 0.1.2
File Interpreter ABI Platform Actions
django_token_user_visit-0.1.2-py3-none-any.whl Python 3 none any Details for django_token_user_visit-0.1.2-py3-none-any.whl

Total release size: 21.5 kB

Release files / django_token_user_visit-0.1.2.tar.gz

Download URL django_token_user_visit-0.1.2.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f87bc5f72ebf2ee5215721c38d34c1dcbd034ae743ec26c131fd585651a4ddfc
BLAKE2b-256 checksum
How to use checksums
56947e43478c36ba85e8cec132408a8d834c05e1cbaae55762abb396439bd024
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.2 Darwin/23.0.0

Release files / django_token_user_visit-0.1.2-py3-none-any.whl

Download URL django_token_user_visit-0.1.2-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3ddb7917898714d044e525442ba30c332c8d41fb2529141884a2201963cad07c
BLAKE2b-256 checksum
How to use checksums
73d8a13387b47e2dc7e9f3f442d07024326d6d1ef1b62bf71282decf6d340e73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.2 Darwin/23.0.0

Security for django-token-user-visit 0.1.2

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page