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.
.. code-block:: python
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.2 (2017-04-29)
==================
* Added compatibility with Django 1.11. Thanks @ondrejsika!
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
.. |Build Status| image:: https://travis-ci.org/ui/django-cached_authentication_middleware.png?branch=master
:target: https://travis-ci.org/ui/django-cached_authentication_middleware
``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.
.. code-block:: python
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.2 (2017-04-29)
==================
* Added compatibility with Django 1.11. Thanks @ondrejsika!
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
.. |Build Status| image:: https://travis-ci.org/ui/django-cached_authentication_middleware.png?branch=master
:target: https://travis-ci.org/ui/django-cached_authentication_middleware
Release files for django-cached_authentication_middleware 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-cached_authentication_middleware-0.2.2.tar.gz | 4.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_cached_authentication_middleware-0.2.2-py2-none-any.whl | Python 2 | none | any | Details |
Total release size:11.2 kB
Release files / django-cached_authentication_middleware-0.2.2.tar.gz
| Download URL | django-cached_authentication_middleware-0.2.2.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bdf664b211256391ba0b57783c78123183d35e62caef18913e892b190103dc51
|
|
BLAKE2b-256 checksum How to use checksums |
f122090054809937f6a4e47293fe17a4c6d65056a40f3fcfdda885f4720482e7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_cached_authentication_middleware-0.2.2-py2-none-any.whl
| Download URL | django_cached_authentication_middleware-0.2.2-py2-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 2 |
|
SHA-256 checksum How to use checksums |
fc10fb2859d1f3245f22dec4b24a0e41520639783e24bbcb77d19d33ee3079f5
|
|
BLAKE2b-256 checksum How to use checksums |
d68f43e23b6f2608ee5967fca887735048f4275a6bf2d75dc094835232a19aff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |