Skip to content

Commit

Permalink
Fix flush with reverse m2m changed signal
Browse files Browse the repository at this point in the history
  • Loading branch information
k4rl85 committed Jan 18, 2021
1 parent 0ef9d30 commit 917f573
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
6 changes: 5 additions & 1 deletion waffle/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
@receiver(m2m_changed, sender=get_waffle_flag_model().groups.through)
def flag_membership_changed(sender, instance, action, **kwargs):
if action in ('post_add', 'post_remove'):
instance.flush()
if kwargs['reverse']:
for flag in get_waffle_flag_model().objects.filter(pk__in=kwargs['pk_set']):
flag.flush()
else:
instance.flush()
50 changes: 50 additions & 0 deletions waffle/tests/test_waffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,29 @@ def test_user(self):
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)

def test_remove_from_user(self):
"""Same operation of `test_user` but performed with reverse relation"""
user = get_user_model().objects.create(username='foo')
flag = waffle.get_waffle_flag_model().objects.create(name='myflag')
flag.users.add(user)

request = get()
request.user = user
response = process_request(request, views.flag_in_view)
self.assertEqual(b'on', response.content)
assert 'dwf_myflag' not in response.cookies

request.user = get_user_model().objects.create(username='someone_else')
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)
assert 'dwf_myflag' not in response.cookies

# Unsetting the flag on a user should have an effect.
user.flag_set.remove(flag)
request.user = user
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)

def test_group(self):
"""Test the per-group switch."""
group = Group.objects.create(name='foo')
Expand Down Expand Up @@ -191,6 +214,33 @@ def test_group(self):
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)

def test_remove_from_group(self):
"""Same operation of `test_group` but performed with reverse relation"""
group = Group.objects.create(name='foo')
user = get_user_model().objects.create(username='bar')
user.groups.add(group)

flag = waffle.get_waffle_flag_model().objects.create(name='myflag')
flag.groups.add(group)

request = get()
request.user = user
response = process_request(request, views.flag_in_view)
self.assertEqual(b'on', response.content)
assert 'dwf_myflag' not in response.cookies

request.user = get_user_model()(username='someone_else')
request.user.save()
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)
assert 'dwf_myflag' not in response.cookies

# Unsetting the flag on a group should have an effect.
group.flag_set.remove(flag)
request.user = user
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)

def test_authenticated(self):
"""Test the authenticated/anonymous switch."""
waffle.get_waffle_flag_model().objects.create(name='myflag', authenticated=True)
Expand Down

0 comments on commit 917f573

Please sign in to comment.