GithubHelp home page GithubHelp logo

papercapp / django-cached_authentication_middleware Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ui/django-cached_authentication_middleware

0.0 2.0 0.0 27 KB

A drop in replacement for django's built in AuthenticationMiddleware that utilizes caching.

License: MIT License

Python 100.00%

django-cached_authentication_middleware's Introduction

django-cached_authentication_middleware is a drop in replacement for django.contrib.auth's built in AuthenticationMiddleware. It tries to populate request.user by fetching user data from cache before falling back to the database.

Installation

Build Status

  • Install via pypi:

    pip install django-cached_authentication_middleware
    
  • Configure CACHES in django's settings.py:

    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
            'LOCATION': '127.0.0.1:11211',
            'TIMEOUT': 36000,
        }
    }
    
  • Replace django.contrib.auth.middleware.AuthenticationMiddleware with cached_auth.Middleware in settings.py:

    MIDDLEWARE_CLASSES = (
        'django.middleware.common.CommonMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        #'django.contrib.auth.middleware.AuthenticationMiddleware'
        'cached_auth.Middleware',
    )
    

And you're done!

Cached Auth Preprocessor

Sometimes you want to preprocess to User instance before storing it into cache. cached_auth allows you to define settings.CACHED_AUTH_PREPROCESSOR, a callable that takes two arguments, user & request and returns a User instance.

A classic example of this would be to attach Profile data to User object so calling request.user.profile does not incur a database hit. Here's how we can implement it.

def attach_profile(user, request):
    try:
        user.get_profile()
    # Handle exception for user with no profile and AnonymousUser
    except (Profile.DoesNotExist, AttributeError):
        pass
    return user


# In settings.py:
CACHED_AUTH_PREPROCESSOR = 'path.to.module.attach_profile'

Running Tests

To run the test suite:

python tests/runtests.py

To run the test suite with Django custom user (this will run only on Django 1.5):

python tests/runtests_custom_user.py

Changelog

Version 0.2.1

  • Better Django 1.8 compatibility.

Version 0.2.0

  • Added support for Django 1.5's customer user model
  • Added CACHED_AUTH_PREPROCESSOR setting

Version 0.1.1

  • Fixed an error where middleware tries to call "get_profile" on AnonymousUser

Version 0.1

  • Initial release

django-cached_authentication_middleware's People

Contributors

eltismerino avatar gchandrasa avatar hwkns avatar selwin avatar tclancy avatar thedrow avatar

Watchers

 avatar  avatar

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.