Skip to main content

A friendly fork of django-user-visit that supports sessions and tokens

Project description

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

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_token_user_visit-0.1.2.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

django_token_user_visit-0.1.2-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file django_token_user_visit-0.1.2.tar.gz.

File metadata

  • Download URL: django_token_user_visit-0.1.2.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.2 Darwin/23.0.0

File hashes

Hashes for django_token_user_visit-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f87bc5f72ebf2ee5215721c38d34c1dcbd034ae743ec26c131fd585651a4ddfc
MD5 1be2b0c152c39b8e84b072eed216cfb1
BLAKE2b-256 56947e43478c36ba85e8cec132408a8d834c05e1cbaae55762abb396439bd024

See more details on using hashes here.

File details

Details for the file django_token_user_visit-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_token_user_visit-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3ddb7917898714d044e525442ba30c332c8d41fb2529141884a2201963cad07c
MD5 85c5f0a9f24192abdafec17fbde58793
BLAKE2b-256 73d8a13387b47e2dc7e9f3f442d07024326d6d1ef1b62bf71282decf6d340e73

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