membershipworks: Move Member.with_is_active() to a QuerySet

This commit is contained in:
Adam Goldsmith 2023-01-03 23:16:06 -05:00
parent 9a2915b247
commit aa87026e99
2 changed files with 26 additions and 19 deletions

View File

@ -15,8 +15,33 @@ class Flag(models.Model):
db_table = "flag" db_table = "flag"
class MemberQuerySet(models.QuerySet):
@staticmethod
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(),
)
def with_is_active(self):
return self.annotate(
is_active=(
self.has_flag("folder", "Members")
| self.has_flag("folder", "CMS Staff")
)
& ~(
self.has_flag("label", "Account On Hold")
| self.has_flag("level", "CMS Membership on hold")
| self.has_flag("folder", "Former Members")
)
)
# TODO: is this still a temporal table? # TODO: is this still a temporal table?
class Member(models.Model): class Member(models.Model):
objects = MemberQuerySet.as_manager()
uid = models.CharField(max_length=24, primary_key=True) uid = models.CharField(max_length=24, primary_key=True)
year_of_birth = models.TextField(db_column="Year of Birth", null=True) year_of_birth = models.TextField(db_column="Year of Birth", null=True)
account_name = models.TextField(db_column="Account Name", null=True) account_name = models.TextField(db_column="Account Name", null=True)
@ -135,24 +160,6 @@ class Member(models.Model):
db_table = "members" db_table = "members"
ordering = ("first_name", "last_name") ordering = ("first_name", "last_name")
@classmethod
def with_is_active(cls):
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"))
& ~(
has_flag("label", "Account On Hold")
| has_flag("level", "CMS Membership on hold")
| has_flag("folder", "Former Members")
)
)
class MemberFlag(models.Model): class MemberFlag(models.Model):
member = models.ForeignKey(Member, on_delete=models.PROTECT, db_column="uid") member = models.ForeignKey(Member, on_delete=models.PROTECT, db_column="uid")

View File

@ -27,7 +27,7 @@ class CertificationDefinitionViewSet(viewsets.ModelViewSet):
for definition in self.queryset.exclude(mailing_list="").prefetch_related( for definition in self.queryset.exclude(mailing_list="").prefetch_related(
Prefetch( Prefetch(
"certificationversion_set__certification_set__member", "certificationversion_set__certification_set__member",
queryset=Member.with_is_active(), queryset=Member.objects.with_is_active(),
), ),
): ):
if definition.mailing_list not in lists: if definition.mailing_list not in lists: