I didn't immediately see one so I wrote django-user-registry
I have since deprecated this for function that looks through the stack to find the request signature.
If it finds the request it uses the user from there.
---- Deprecated (Does't work on multithreaded (read all) servers :P ----
The concept is simple create a small middleware class that registers an authenticated user with with a registry class on request and unregisters the user on result.
Then in your model/signal code you can check for and access a user if one exists in the current context.
This code is little naive... it definitely would fall down in a multithreaded environment. Any ideas? The only other way that I can think of implementing it is to walk back up the stack looking for a request...
Example
(in settings.py)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# 3rd party
# allow access to authenticated users in models and signals
'user_registry.middleware.UserRegistryMiddleware',
)
(in foo/signals.py)
from django.db.models.signals import post_save, post_delete, m2m_changed
from django.dispatch import receiver
from user_registry import UserRegistry
def alert_on_user():
print "alert_on_user"
if UserRegistry.has_user():
user = UserRegistry.get_user()
print user
@receiver(post_save)
def post_save_audit(sender,**kwargs):
alert_on_user()
@receiver(post_delete)
def post_delete_audit(sender,**kwargs):
alert_on_user()
@receiver(m2m_changed)
def m2m_changed_audit(sender,**kwargs):
alert_on_user()