Package: django-token-user-visit
Django Token User VisitDjango app for recording daily user visitsProject DescriptionThis app consists of middleware to record user visits, and a singleTokenUserVisitmodel to capture that data.There are two Middleware components available:TokenUserVisitMiddlewaredoes the work of logging user visit. However, it depends on therequest.userbeing established. This is not the case if the user is using token based authentication.RequestUserSetterMiddlewareestablishes therequest.userinstance if it has not yet been set.The principal behind this isnotto 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).RequirementsPython 3.8+Django 3.2, 4.1, 4.2Python User Agents 2.1+InstallationInstall with pip:pipinstalldjango-token-user-visitAdddjango-token-user-visitto yourINSTALLED_APPSINSTALLED_APPS = [
    ...
    "token_user_visit",
]Add the Django Token User Visit Middleware:MIDDLEWARE = [
    ...
    "token_user_visit.middleware.RequestUserSetterMiddleware",
    ...
    "token_user_visit.middleware.TokenUserVisitMiddleware",
]NOTE:TheRequestUserSetterMiddlewaremust come before theTokenUserVisitMiddlewareand must follow theSessionMiddlewareand any authentication middleware you are using.SettingsTOKEN_USER_VISIT_RECORDING_DISABLEDDefault:FalseThis can be set as an environment variable or in yoursettings.pyand if set toTrueuser visits are not recorded.TOKEN_USER_VISIT_RQUEST_CONTEXT_EXTRACTORDefault:{}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_ENCODERDefault:DjangoJSONEncoderThis can override the JSON encoder used for context JSON fieldsTOKEN_USER_VISIT_RECORDING_BYPASSDefault:FalseThis 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 tosettings.pyTOKEN_USER_VISIT_RECORDING_BYPASS=lambdarequest:request.path.startswith("/admin/")TOKEN_USER_VISIT_DUPLICATE_LOG_LEVELDefault:warningThe 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_ACTIVATORDefault:FalseBy 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_CLASSDefault:NoneRequiredThe authentication class used for Token based authentication.TOKEN_USER_VISIT_KEYWORDDefault:BearerThis allows you to modify the Keyword identifying the token to something else. Generally this will beBearerPreviewsAdmin list view:Admin edit view: