paperwork: Also generate the ShopLeads mailing list in api

This commit is contained in:
Adam Goldsmith 2023-01-23 15:07:01 -05:00
parent 2f72bb7d51
commit efe476b367
2 changed files with 25 additions and 8 deletions

View File

@ -163,10 +163,9 @@ class Member(models.Model):
db_table = "members"
ordering = ("first_name", "last_name")
@property
def sanitized_mailbox(self):
def sanitized_mailbox(self, name_ext: str = ""):
return django.core.mail.message.sanitize_address(
(self.account_name, self.email), settings.DEFAULT_CHARSET
(self.account_name + name_ext, self.email), settings.DEFAULT_CHARSET
)

View File

@ -30,7 +30,7 @@ class DepartmentViewSet(viewsets.ModelViewSet):
"""
departments = self.queryset.filter(has_mailing_list=True).prefetch_related(
"children",
"list_moderator_flag",
"list_moderator_flag__members",
Prefetch(
"certificationdefinition_set__certificationversion_set__certification_set__member",
queryset=Member.objects.with_is_active(),
@ -39,16 +39,17 @@ class DepartmentViewSet(viewsets.ModelViewSet):
lists = {}
for department in departments:
if department.list_moderator_flag is not None:
moderator_emails = department.list_moderator_flag.members.values_list(
"email", flat=True
)
moderator_emails = {
member.email
for member in department.list_moderator_flag.members.all()
}
else:
moderator_emails = []
# TODO: this could be done in SQL instead if
# membershipworks was in the same database
active_certified_members = {
member_cert.member.sanitized_mailbox
member_cert.member.sanitized_mailbox()
for certification in department.certificationdefinition_set.all()
for version in certification.certificationversion_set.all()
for member_cert in version.certification_set.all()
@ -79,6 +80,23 @@ class DepartmentViewSet(viewsets.ModelViewSet):
if department.parent_id is None:
recurse_children(department)
shopleads = {}
for department in departments:
for member in department.list_moderator_flag.members.all():
if member not in shopleads:
shopleads[member] = []
shopleads[member].append(department)
# Add members to the Shop Leads mailing list, but don't configure it
lists["ShopLeads"] = {
"members": {
shoplead.sanitized_mailbox(
f" - {'/'.join(department.name for department in departments)}"
)
for shoplead, departments in shopleads.items()
},
}
return Response(lists)