Compare commits

...

3 Commits

Author SHA1 Message Date
1aa0bd6014 paperwork: Add deskers mailing list 2023-08-27 13:48:48 -04:00
a3a8c305ac membershipworks: Use EXISTS for testing flags in is_active
This behaves much more correctly, although may not have ideal performance
2023-08-27 13:48:48 -04:00
a51f246667 paperwork: Slightly change structure of mailing lists api for Mailman3 2023-08-26 20:16:04 -04:00
2 changed files with 26 additions and 8 deletions

View File

@ -3,7 +3,7 @@ from typing import Optional
import django.core.mail.message
from django.conf import settings
from django.db import models
from django.db.models import Q, Count, BooleanField
from django.db.models import Exists, OuterRef
class Flag(models.Model):
@ -21,14 +21,17 @@ class Flag(models.Model):
class MemberQuerySet(models.QuerySet):
# TODO: maybe rename to reflect EXISTS?
@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(),
)
return Exists(Flag.objects.filter(
type=flag_type,
name=flag_name,
members=OuterRef("pk")
))
# TODO: it should be fairly easy to reduce the number of EXISTS by
# merging the ORed flags
def with_is_active(self):
return self.annotate(
is_active=(

View File

@ -1,5 +1,5 @@
from django.db.models import Prefetch
from rest_framework import routers, serializers, viewsets
from django.db.models import Prefetch, Q
from rest_framework.decorators import action
from rest_framework.response import Response
@ -59,10 +59,10 @@ class DepartmentViewSet(viewsets.ModelViewSet):
lists[department.list_name] = {
"config": {
"real_name": department.list_name,
"moderator": moderator_emails,
"subject_prefix": f"[CMS {department.name}] ",
"reply_to_address": department.list_reply_to_address,
},
"moderators": moderator_emails,
"members": active_certified_members,
}
@ -98,6 +98,21 @@ class DepartmentViewSet(viewsets.ModelViewSet):
},
}
# TODO: this isn't really in the domain of the `paperwork` app...
deskers = (
Member.objects.with_is_active()
.filter(is_active=True)
.filter(
Member.objects.has_flag("label", "Volunteer: Desker")
| Q(billing_method__startswith="Desker")
)
)
lists["Deskers"] = {
"members": {
desker.sanitized_mailbox(use_volunteer=True) for desker in deskers
}
}
return Response(lists)