Skip to content

Instantly share code, notes, and snippets.

@miraculixx
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miraculixx/b34633e224a68924df5b to your computer and use it in GitHub Desktop.
Save miraculixx/b34633e224a68924df5b to your computer and use it in GitHub Desktop.
"""
This is a temporary fix for django-cms 3.0 failing to logout
a user correctly (I don't currently have the time to setup a
PR). The fix goes into cms.middleware.user
The problem is manifest in unit tests where there are multiple
tests that repeatedly login/logout using Django's test Client()
and which use fixtures referencing auth_user.
The first test that runs will install the fixtures fine. Any
subsequent tests will fail creating new auth_user objects:
IntegrityError: (1452, 'Cannot add or update a child row: a
foreign key constraint fails (`test_shrebo`.`cms_pageuser`,
CONSTRAINT `created_by_id_refs_id_9da6953b` FOREIGN KEY
(`created_by_id`) REFERENCES `auth_user` (`id`))')
The problem appears in cms.signals.permissions.post_save_user
while processing post_save_user user, but the origin of the error
is due to CurrentUserMiddleware not resetting the user object
in thread local. This causes subsequent test runs to set a
created_by user object that no longer exists.
Usage without updating Django CMS:
add the below logout_user somewhere in your (at least test) code.
TODO incorporate as a PR into
"""
from django.dispatch import receiver
from django.contrib.auth.signals import user_logged_out
class CurrentUserMiddleware(object):
def process_request(self, request):
from cms.utils.permissions import set_current_user
set_current_user(getattr(request, 'user', None))
@receiver(user_logged_out)
def logout_user(sender, **kwargs):
from cms.utils.permissions import set_current_user
set_current_user(None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment