diff --git a/src/wagtail_personalisation/forms.py b/src/wagtail_personalisation/forms.py index 9d342d9..a946158 100644 --- a/src/wagtail_personalisation/forms.py +++ b/src/wagtail_personalisation/forms.py @@ -39,7 +39,7 @@ class SegmentAdminForm(WagtailAdminModelForm): return count User = get_user_model() - users = User.objects.all() + users = User.objects.filter(is_active=True, is_staff=False) for user in users.iterator(): if match_any: diff --git a/tests/unit/test_static_dynamic_segments.py b/tests/unit/test_static_dynamic_segments.py index c43e776..686e89f 100644 --- a/tests/unit/test_static_dynamic_segments.py +++ b/tests/unit/test_static_dynamic_segments.py @@ -286,6 +286,48 @@ def test_count_users_matching_static_rules(site, client, django_user_model): assert form.count_matching_users([rule], True) is 2 +@pytest.mark.django_db +def test_count_matching_users_excludes_staff(site, client, django_user_model): + class TestStaticRule(AbstractBaseRule): + static = True + + class Meta: + app_label = 'wagtail_personalisation' + + def test_user(self, request, user): + return True + + django_user_model.objects.create(username='first') + django_user_model.objects.create(username='second', is_staff=True) + + segment = SegmentFactory.build(type=Segment.TYPE_STATIC) + rule = TestStaticRule() + form = form_with_data(segment, rule) + + assert form.count_matching_users([rule], True) is 1 + + +@pytest.mark.django_db +def test_count_matching_users_excludes_inactive(site, client, django_user_model): + class TestStaticRule(AbstractBaseRule): + static = True + + class Meta: + app_label = 'wagtail_personalisation' + + def test_user(self, request, user): + return True + + django_user_model.objects.create(username='first') + django_user_model.objects.create(username='second', is_active=False) + + segment = SegmentFactory.build(type=Segment.TYPE_STATIC) + rule = TestStaticRule() + form = form_with_data(segment, rule) + + assert form.count_matching_users([rule], True) is 1 + + @pytest.mark.django_db def test_count_matching_users_only_counts_static_rules(site, client, django_user_model): class TestStaticRule(AbstractBaseRule):