Middleware to make authentication data easily available to all parts of a django project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Alireza Savand 53689952b2 Merge pull request #21 from luzfcb/patch-2 2 years ago
cuser Bump version to latest 2 years ago
testproject refactoring test suite 2 years ago
.gitignore refactoring test suite 2 years ago
.travis.yml run test with django 1.11 final release 2 years ago
AUTHORS.rst refactoring test suite 2 years ago
ChangeLog.rst Update changelog with the latest changes 2 years ago
LICENSE Add LICENSE file 5 years ago
MANIFEST.in exclude nonsense stuff 5 years ago
README.rst refactoring test suite 2 years ago
manage.py refactoring test suite 2 years ago
requirements.txt test with django 1.8 to 1.11b1 and check coverage 2 years ago
setup.cfg use universal wheel package 2 years ago
setup.py Add python 3.6 to the classifiers 2 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

.. image:: http://badge.kloud51.com/pypi/w/django-cuser.svg
:target: https://pypi.python.org/pypi/django-cuser
:alt: PyPI Wheel

.. image:: http://badge.kloud51.com/pypi/s/django-cuser.svg
:target: https://pypi.python.org/pypi/django-cuser
:alt: PyPI Status

.. image:: http://badge.kloud51.com/pypi/l/django-cuser.svg
:target: https://pypi.python.org/pypi/django-cuser
:alt: PyPI License

.. image:: http://badge.kloud51.com/pypi/f/django-cuser.svg
:target: https://pypi.python.org/pypi/django-cuser
:alt: PyPI Format

.. image:: http://badge.kloud51.com/pypi/p/django-cuser.svg
:target: https://pypi.python.org/pypi/django-cuser
:alt: PyPI Py_versions

.. image:: http://badge.kloud51.com/pypi/d/django-cuser.svg
:target: https://pypi.python.org/pypi/django-cuser
:alt: PyPI Downloads

.. image:: http://badge.kloud51.com/pypi/i/django-cuser.svg
:target: https://pypi.python.org/pypi/django-cuser
:alt: PyPI Implementation

.. image:: http://badge.kloud51.com/pypi/v/django-cuser.svg
:target: https://pypi.python.org/pypi/django-cuser
:alt: PyPI Version

.. image:: http://badge.kloud51.com/pypi/e/django-cuser.svg
:target: https://pypi.python.org/pypi/django-cuser
:alt: PyPI Egg

.. contents:: Table of contents


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

Supported Python versions

``django-cuser`` currently can be run on multiple python versions:

* Python 2 (2.7)
* Python 3 (3.4, 3.5)
* PyPy


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

$ 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 ``settings.AUTH_USER_MODEL`` model and you
can treat it as such.


``django-cuser`` has been tested Django 1.8 and later. To run the the tests:


$ python manage.py test -v 2

It's also available on travis-ci: