diff --git a/membershipworks/models.py b/membershipworks/models.py index 5092d5f..e721a10 100644 --- a/membershipworks/models.py +++ b/membershipworks/models.py @@ -1,5 +1,5 @@ from django.db import models -from django.db.models import Q +from django.db.models import Q, Count, BooleanField class Flag(models.Model): @@ -137,15 +137,19 @@ class Member(models.Model): @classmethod def with_is_active(cls): - return cls.objects.annotate( - is_active=( - Q(flags__name="Members", flags__type="folder") - | Q(flags__name="CMS Staff", flags__type="folder") + def has_flag(flag_type: str, flag_name: str): + return Count( + "uid", + filter=Q(flags__name=flag_name, flags__type=flag_type), + output_field=BooleanField(), ) + + return cls.objects.annotate( + is_active=(has_flag("folder", "Members") | has_flag("folder", "CMS Staff")) & ~( - Q(flags__name="Account On Hold", flags__type="label") - | Q(flags__name="CMS Membership on hold", flags__type="level") - | Q(flags__name="Former Members", flags__type="folder") + has_flag("label", "Account On Hold") + | has_flag("level", "CMS Membership on hold") + | has_flag("folder", "Former Members") ) )