We have a legacy application written in python 2.7 and django 1.11 (and no resources to migrate). Also it uses grappelli for authorization. We tried to add Edit links for some pages (each of the pages displays detailed info on a Round object) that should be visible only for authorized users with rights to edit a Round ( APPNAME | round | Can change round in the grappelli web interface). In the template, the permission is checked like so:
{% if perms.round.can_change_round %}
 <a href="{{link_to_change_round}}" class="stuff-only-link">{% trans 'Edit' %}</a>
{% endif %}
The problem occurs when the following events take place in a short time interval:
- A user which has the permission to edit a Round visits a page - and sees the
Editlink. - A user which has no permission to edit a Round (e.g. anonymous user) visits the same page - and also sees the link!
Revelant settings (settings.py) are:
CACHES = {
'default': {
# 'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
}
}
SOLO_CACHE = 'default'
SOLO_CACHE_TIMEOUT = 5*60
When I change cache to dummy, the problem disappears. Thus, it seems to be an obvious solution to totally disable caching for authorized users. To be more precise:
a) If a user is anonymous (the most of real site users) - the requested page can be written to the cache and can be readed from the cache;
b) If a user is authorized (about 5-7 users) - the requested page can NOT be written to the cache and can NOT be readed from the cache.
How do I achieve this?
Many thanks to @Melvin for the links to the documentation. After an hour of googling, an answer was found and adapted. The code is:
EDIT: Originally, the cache was function-based. So, "/rounds/1" gave the same (cached) value as "/rounds/2". We add the full URL to the cache key to fix the problem.
and then in
views.py: