Closed magyarn closed 4 years ago
What cache backend are you using? Middleware install requires cache set up.
Thanks for your quick response, @nicholasserra. I didn't have a CACHES
setting before, so I added the following to my settings/base.py
:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake',
}
}
I encountered the same problem, though. So I tried creating a separate cache, like so:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake',
},
'tos': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake-2',
'NAME': 'tos-cache',
}
}
TOS_CACHE_NAME = 'tos'
But that also did not change anything. Thanks for your time and help.
At this point i'd be throwing logging into the check_tos
view within the package and see what's getting called.
After some digging around and changing to python-memcached
instead of LocMemCache
, I received this traceback error:
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/Users/nathanmagyar/Documents/why-you-matter/env/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/Users/nathanmagyar/Documents/why-you-matter/env/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/Users/nathanmagyar/Documents/why-you-matter/env/lib/python3.7/site-packages/django/utils/autoreload.py", line 77, in raise_last_exception
raise _exception[1]
File "/Users/nathanmagyar/Documents/why-you-matter/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 337, in execute
autoreload.check_errors(django.setup)()
File "/Users/nathanmagyar/Documents/why-you-matter/env/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/Users/nathanmagyar/Documents/why-you-matter/env/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/nathanmagyar/Documents/why-you-matter/env/lib/python3.7/site-packages/django/apps/registry.py", line 122, in populate
app_config.ready()
File "/Users/nathanmagyar/Documents/why-you-matter/whyyoumatter/tos/apps.py", line 27, in ready
invalidate_cached_agreements(TermsOfService, None)
File "/Users/nathanmagyar/Documents/why-you-matter/whyyoumatter/tos/signal_handlers.py", line 18, in invalidate_cached_agreements
cache.incr('django:tos:key_version')
File "/Users/nathanmagyar/Documents/why-you-matter/env/lib/python3.7/site-packages/django/core/cache/backends/memcached.py", line 110, in incr
raise ValueError("Key '%s' not found" % key)
ValueError: Key ':1:django:tos:key_version' not found
I looked in signal_handlers.py
and it seemed like the cache key wasn't getting set.
# signal_handlers.py
...
cache = get_cache(getattr(settings, 'TOS_CACHE_NAME', 'default'))
# Logging cache produces: <django.core.cache.backends.memcached.MemcachedCache object at 0x107a91198>
def invalidate_cached_agreements(TermsOfService, instance, **kwargs):
if kwargs.get('raw', False):
return
# Set the key version to 0 if it doesn't exist and leave it
# alone if it does
cache.get('django:tos:key_version', 0)
# Logging cache.get('django:tos:key_version') produces: None
# This key will be used to version the rest of the TOS keys
# Incrementing it will effectively invalidate all previous keys
cache.incr('django:tos:key_version')
I tried using cache.get_or_set('django:tos:key_version
, 0) (documentation) and that set the key, but ultimately didn't solve the issue because memcache.py is looking for ':1:django:tos:key_version'
.
How does the :1:
portion of the cache key get added? Is that the incrementing piece?
the :1:
is injected via django's internal tracking of cache VERSION. This is abstracted while using any of django's built in cache handling methods. If you zip up some kind of testable codebase I can take a better look. Sorry you're still having issues.
Ok, thanks. Here is a test project with Wagtail, Allauth, and TOS. Thanks for taking a look.
@magyarn Did you end up finding a solution to this?
@nicholasserra Unfortunately, no.
Turns out this was a cache invalidation issue. There was an incomplete PR for it a bit before this issue was raised. Just pushed a fix. Thanks!
Hello,
I'm using
Django 2.2.7
,Wagtail 2.7
, andDjango Allauth 0.40.0
. I downloaded a copy of thedjango-tos
repo and placed it my root project directory. Because I am usingDjango Allauth
I am following the middleware installation approach. I have created an activeTermsOfService
object and am successfully sent to theTosView
after logging in. However, when a user accepts the terms, they are not always passed to thenext
page. TheUserAgreement
object gets succcessfully created on the first try, but the user remains on theTosView
after thetos_check
form is submitted. It seems as though the user can only move onto the "next" page after I make a change to my project and save the file, causing everything to reload.I have tried setting
request.session['tos_user'] = user.pk
andrequest.session['tos_backend'] = user.backend
afterauth_login(request, user)
is called incheck_tos()
, but that had no effect. I also loggeduser
andUserAgreement.objects.all()
insidecheck_tos()
to make sure that information is there, and it is.My value for
redirect_to
is/
, which I thought might be problematic. So I changed the value of the hiddennext
field to a different relative path in the inspector, but that also did not work. I remain on theTosView
page.Below are my
settings/base.py
and rooturls.py
. Thank you for any help you can provide.