Caches the groups a user is in so requests don't have to make calls to the database to check group status.
Currently supporting Django 1.8, 1.11, and 2.0 with Python 2.7/Python 3 where support for versions aligns with Django's support. See the tox.ini file for specific Python and Django version pairings.
The full documentation is at https://django-groups-cache.readthedocs.io.
Install django-groups-cache:
pip install django-groups-cache
Add it to your INSTALLED_APPS:
INSTALLED_APPS = (
...
'groups_cache.apps.GroupsCacheConfig',
...
)
Add the middleware to your MIDDLEWARE_CLASSES:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
...
'groups_cache.middleware.GroupsCacheMiddleware',
)
Checking in a Django Template if the user is in a group name:
{% if "admins" in request.groups_cache %}
<a href="/admin">Admin Panel</a>
{% endif %}
# or use templatetag, note that templatetag is slower
{% load has_group %}
{% if request.user|has_group:"admins" %}
<a href="/admin">Admin Panel</a>
{% endif %}
Turn on caching:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
See https://docs.djangoproject.com/en/1.10/topics/cache/#memcached for more details on setting up memcached.
Note**
Using django.core.cache.backends.locmem.LocMemCache is not safe for production unless you are only running a single process (and odds are you aren't).
See https://docs.djangoproject.com/en/1.10/topics/cache/#local-memory-caching for more details.
- Adds groups_cache property to request object
- Provides templatetag has_group
- Invalidates cache on User or Group model changes and on m2m groups ManyToManyField changes
- Fully tested with high coverage
source <YOURVIRTUALENV>/bin/activate (myenv) $ pip install tox (myenv) $ tox
Tools used in rendering this package: