Giter VIP home page Giter VIP logo

django-cuser's Introduction

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

travis-cli tests status for django-cuser

PyPI Wheel

PyPI Status

PyPI License

PyPI Format

PyPI Py_versions

PyPI Downloads

PyPI Implementation

PyPI Version

PyPI Egg

Table of contents

Overview

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

Installing

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:

CuserMiddleware.set_user(some_user)

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:

CuserMiddleware.del_user()

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

CurrentUserField

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.

Tests

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:

https://travis-ci.org/Alir3z4/django-cuser/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.