From efe476b367c735fc17aa26892256a40c032d3fd2 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Mon, 23 Jan 2023 15:07:01 -0500 Subject: [PATCH] paperwork: Also generate the ShopLeads mailing list in api --- membershipworks/models.py | 5 ++--- paperwork/api.py | 28 +++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/membershipworks/models.py b/membershipworks/models.py index beb89f8..e5a5bdc 100644 --- a/membershipworks/models.py +++ b/membershipworks/models.py @@ -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 ) diff --git a/paperwork/api.py b/paperwork/api.py index 1ffcdb6..8b5a991 100644 --- a/paperwork/api.py +++ b/paperwork/api.py @@ -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)