from .audit_context import set_current_user


# ---------------------------------------------------------------------------
# AuditUserMiddleware
# ---------------------------------------------------------------------------
class AuditUserMiddleware:
    """
    Middleware to store the current request.user in thread-local storage.
    Used for auditing (_changed_by, etc.).
    """
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        # Save current user for this request in thread-local storage
        set_current_user(getattr(request, "user", None))
        return self.get_response(request)
