The Django-Authenticated-Signals package provides several specialized model save and delete signals
with additional request
parameter.
The built-in set of Django model signals pre_save
, post_save
, pre_delete
, and post_delete
don't provide any information about a request context and particularly, the user who appears
to be the running operation initiator.
The signals provided by the package allow to authenticate model operations for different purposes.
For example, you can create a simple and efficient universal model operation log which works automatically for any model operation within request context.
Stable version from the PyPi package repository
pip install django-authenticated-signals
Last development version from the GitHub source version control system
pip install git+git://github.com/nnseva/django-authenticated-signals.git
Include the authenticated_signals
application into the INSTALLED_APPS
list, like:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
...
'authenticated_signals',
...
]
Insert the special packet middleware into your middleware pipe:
MIDDLEWARE = [
...
'django.contrib.auth.middleware.AuthenticationMiddleware',
'authenticated_signals.middleware.AuthenticatedSignalsMiddleware',
...
]
Use standard Django way to subscribe to the signal, like:
from authenticated_signals.signals import *
def q(*av,**kw):
print "The signal has been generated:",av,kw
authenticated_post_save.connect(q)
Note that the request parameter may be None if:
- you didn't insert the special packet middleware into your middleware pipe
- the request is not evaluated at all, in management commands for example
You can use the following signals:
authenticated_pre_save
- generated from built-in Djangopre_save
signalauthenticated_post_save
- generated from built-in Djangopost_save
signalauthenticated_pre_delete
- generated from built-in Djangopre_delete
signalauthenticated_post_delete
- generated from built-in Djangopost_delete
signal
also:
authenticated_save
- alias forauthenticated_post_save
authenticated_delete
- alias forauthenticated_pre_delete
The authenticated_signals.middleware.current_request()
function returns
a thread-specific current request object if it has been already catched
by the packet middleware.
You can check the request user
or any other request attribute to authenticate
a signal in the signal processing callback function as you wish.