GithubHelp home page GithubHelp logo

django-author's Introduction

django-author

image

image

Update author and updated_by fields of models automatically

This library is used for updating author and updated_by fields automatically with request.user when the model has created/changed.

Also if you are too lazy to write author = models.ForeignKey(User, _('author'), related_name ...) to every model, just add @with_author decorator to the top of class makes you happy.

Install

This library is on PyPI so you can install it with:

pip install django-author

or from github:

pip install git+https://github.com/lambdalisue/django-author.git

Usage

  1. Add 'author' to your INSTALLED_APPS on settings.py
  2. Add 'author.middlewares.AuthorDefaultBackendMiddleware' to your MIDDLEWARE_CLASSES if you use default author backend
  3. Add author and updated_by field to models which you want to have author and updated_by fields manually or use @with_author decorator like below:

    from django.db import models
    from author.decorators import with_author
    
    @with_author
    class Entry(models.Model):
        title = models.CharField('title', max_length=50)
        body = models.TextField('body')
  4. Done. Now you have automatically updated author and updated_by fields

    If you are in truble, see author_test directory for usage sample.

  5. If you want to forbid modification of the updated_by field for some actions, just set instance._change_updated_by = False before calling save().

Settings

AUTHOR_BACKEND

Class or string path of backend. the backend is used to determine user when object is created/updated.

AUTHOR_CREATED_BY_FIELD_NAME

A name of field. the setting also interfer the name of field created by @with_author decorator. default is 'author'

AUTHOR_UPDATED_BY_FIELD_NAME

A name of field. the setting also interfer the name of field created by @with_author decorator. default is 'updated_by'

AUTHOR_DO_NOT_UPDATE_WHILE_USER_IS_NONE

Do not update author or updated_by field when detected user is None. default is True

AUTHOR_MODELS

Check signals for only these models. default is None

AUTHOR_IGNORE_MODELS

Do not check signals for these models. default is ['auth.user', 'auth.group', 'auth.permission', 'contenttype.contenttype']

Backend

The default backend use thread_locals storategy to get current request in signal call.

If you want to change the strategy or whatever, create your own backend.

A backend is a class which have get_user method to determine current user.

AuthorDefaultBackend

Default backend. This backend return None when no request found or AnonymousUser create/update object.

AuthorSystemUserBackend

System user backend. This backend return system user when no request found or AnonymousUser create/update object.

system user is determined with get_system_user method and default is User.objects.get(pk=1)

django-author's People

Contributors

gbezyuk avatar lambdalisue avatar luzfcb avatar marksweb avatar petrdlouhy avatar scotteadams avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

django-author's Issues

Bug when using django-author with new MIDDLEWARE

Hi there.

I noticed that there is a problem when using django-author with the new Django MIDDLEWARE setting:

middlewares.py defines an AuthorDefaultBackendMiddlewareNewStyle, however the AuthorDefaultBackend only checks for the presence of AuthorDefaultBackendMiddleware and raises ImproperlyConfigured when it's not present in either settings.MIDDLWARE_CLASSES or settings.MIDDLEWARE.

The solution should be simply checking for the presence of AuthorDefaultBackendMiddlewareNewStyle in settings.MIDDLEWARE.

Best,
zngr

Error while loading the middleware

Hello,
According to the post below, the middleware needs a 2 more functions to start correctly.
https://stackoverflow.com/questions/42232606/django-exception-middleware-typeerror-object-takes-no-parameters
see also : https://docs.djangoproject.com/en/1.10/topics/http/middleware/#writing-your-own-middleware

Else I get :
mw_instance = middleware(handler)
TypeError: object() takes no parameters

in middle, just add :

class AuthorDefaultBackendMiddleware(object):
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        return self.get_response(request)

Thanks for your project :-)

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.