Middleware to make authentication data easily available to all parts of a django project

Alireza Savand ad0b5663d6 Update version number 4 years ago
cuser ad0b5663d6 Update version number 4 years ago
.gitignore 402011ef06 add new dir to ignore ;) 7 years ago
.travis.yml ba38816235 Add .travis.yml conf file 5 years ago
AUTHORS.rst 238840f296 Update authors and changelog 4 years ago
ChangeLog.rst 8595dabec2 Update changlog for version number 4 years ago
LICENSE 1d9bbe1d04 Add LICENSE file 5 years ago
MANIFEST.in ac73a7e23c Add manifest.in file 5 years ago
README.rst 252f207bd5 Correct typo "urrent" -> "current" 4 years ago
requirements.txt 641dd91d55 Rocking on django>=1.5 5 years ago
run_tests.py 64f3081a18 Add run_tests.py to run the tests with no django thing around 5 years ago
setup.py 641dd91d55 Rocking on django>=1.5 5 years ago


django-cuser - Take care of current user in silent way.

.. image:: https://travis-ci.org/Alir3z4/django-cuser.png
:alt: travis-cli tests status for django-cuser
:target: https://travis-ci.org/Alir3z4/django-cuser

.. contents:: Table of contents

Copyright (c)
* 2009-2011 Dennis Kaarsemaker
* 2011 Atamert Ölçgun
* 2012 Alireza Savand


cuser will bring you Current user of your django application from anywehere in your code.
I know, sounds fantastic ;)


django-cuser is also avilable at http://pypi.python.org/pypi/django-cuser
So it can be install it by pip or easy_install::

$ python pip install django-cuser

Or you can grab the latest version tarball::

$ python setup.py install

To enable django-cuser in your project

* Add ``cuser`` to ``INSTALLED_APPS`` in your ``settings.py``
* Add ``cuser.middleware.CuserMiddleware`` to ``MIDDLEWARE_CLASSES`` after the
authentication and session middleware.

Who is the current user

To set/get the user info, there is the following API::

from cuser.middleware import CuserMiddleware

Set the current user for this thread. Accepts user objects and login names::


Get the current user or None::

user = CuserMiddleware.get_user()

This will return some_user if there is no current user::

user = CuserMiddleware.get_user(some_user)

Forget the current user. It is always safe to call this, even if there is no current user::


The middleware automatically sets/deletes the current user for HTTP requests.
For other uses (management commands, scripts), you will need to do this


``cuser`` also provides a ``CurrentUserField``, which can be used for auditing
purposes. Use it as follows:

from cuser.fields import CurrentUserField

class MyModel(models.Model):
creator = CurrentUserField(add_only=True, related_name="created_mymodels")
last_editor = CurrentUserField(related_name="last_edited_mymodels")

This field is a ``ForeignKey`` to the ``django.contrib.auth.models.User`` model and you
can treat it as such.