@checks.register(checks.Tags.compatibility)
def check_activity_middleware(
app_configs: list[AppConfig] | None,
**kwargs
) -> list[checks.CheckMessage]:
_ = app_configs
_ = kwargs
middleware = list(getattr(settings, 'MIDDLEWARE', None) or [])
if ACTIVITY_MIDDLEWARE_PATH not in middleware:
warning = checks.Warning(
'ActivityUserMiddleware is not in MIDDLEWARE, so no activity records '
'will be created for requests.',
hint=(
f"Add '{ACTIVITY_MIDDLEWARE_PATH}' to MIDDLEWARE "
f"after '{AUTHENTICATION_MIDDLEWARE_PATH}'."
),
id='django_spire_history_activity.W001',
)
return [warning]
if AUTHENTICATION_MIDDLEWARE_PATH not in middleware:
return []
activity_index = middleware.index(ACTIVITY_MIDDLEWARE_PATH)
authentication_index = middleware.index(AUTHENTICATION_MIDDLEWARE_PATH)
if activity_index < authentication_index:
warning = checks.Warning(
'ActivityUserMiddleware is listed before AuthenticationMiddleware, so '
'request.user is not populated when the activity user is captured.',
hint=(
f"Move '{ACTIVITY_MIDDLEWARE_PATH}' after "
f"'{AUTHENTICATION_MIDDLEWARE_PATH}' in MIDDLEWARE."
),
id='django_spire_history_activity.W002',
)
return [warning]
return []