Repository: carpentries/amy

Issue:
Include additional contacts from workshop request forms in possible recipients of auto-emails
In several contexts (especially for the 7 day post workshop email), it would be useful to include as recipients people listed as additional contacts in the forms.

It seems that it could be useful to create a new role for the workshops "additional-contact" (or something like this). Email addresses listed in the various additional contacts could then be assigned this role when the workshop request forms get converted into an event.

This way the email addresses would easily be accessible to the email templates and these people could be included as recipients to the automated emails.


Discussion:
We have a field `event.contact` ("Additional people to contact") for storing additional contact emails for each workshop. I'm going to use this field in the `PostWorkshopAction`, if you want to include it in other actions too please let me know.

As for the automatic generation of `event.contact`, it looks like the additional contacts from workshop request don't get copied over to `event.contact` during "Accept & Create a new event". If you want me to change this behavior, please create a separate ticket for it.

If you still want to create a new role for keeping additional contacts this way, please also let me know.

Resolving patch:
diff --git a/amy/autoemails/actions.py b/amy/autoemails/actions.py
--- a/amy/autoemails/actions.py
+++ b/amy/autoemails/actions.py
@@ -441,17 +441,18 @@ def event_slug(self) -> str:
     def all_recipients(self) -> str:
         """If available, return string of all recipients."""
         try:
-            return ", ".join(
-                list(
-                    Person.objects.filter(
-                        task__in=self.context_objects["event"].task_set.filter(
-                            role__name__in=self.ROLES
-                        )
-                    )
-                    .distinct()
-                    .values_list("email", flat=True)
+            event = self.context_objects["event"]
+            person_emails = list(
+                Person.objects.filter(
+                    task__in=event.task_set.filter(role__name__in=self.ROLES)
                 )
+                .distinct()
+                .values_list("email", flat=True)
             )
+            additional_contacts = event.contact.split(TAG_SEPARATOR)
+            all_emails = list(filter(bool, person_emails + additional_contacts))
+            return ", ".join(all_emails)
+
         except (KeyError, AttributeError):
             return ""
 
@@ -516,13 +517,17 @@ def get_additional_context(self, objects, *args, **kwargs):
         )
 
         # querying over Person.objects lets us get rid of duplicates
-        context["all_emails"] = list(
+        person_emails = list(
             Person.objects.filter(
                 task__in=event.task_set.filter(role__name__in=self.ROLES)
             )
             .distinct()
             .values_list("email", flat=True)
         )
+        additional_contacts = event.contact.split(TAG_SEPARATOR)
+        all_emails = list(filter(bool, person_emails + additional_contacts))
+        context["all_emails"] = all_emails
+
         context["assignee"] = (
             event.assigned_to.full_name if event.assigned_to else "Regional Coordinator"
         )


Test patch:
diff --git a/amy/autoemails/tests/test_postworkshopaction.py b/amy/autoemails/tests/test_postworkshopaction.py
--- a/amy/autoemails/tests/test_postworkshopaction.py
+++ b/amy/autoemails/tests/test_postworkshopaction.py
@@ -4,6 +4,7 @@
 
 from autoemails.actions import PostWorkshopAction
 from autoemails.models import Trigger, EmailTemplate
+from workshops.fields import TAG_SEPARATOR
 from workshops.models import Task, Role, Person, Event, Tag, Organization
 
 
@@ -164,6 +165,8 @@ def testContext(self):
             end=date.today() + timedelta(days=8),
             country="GB",
             venue="Ministry of Magic",
+            # additionally testing the empty email
+            contact=TAG_SEPARATOR.join(["peter@webslinger.net", ""])
         )
         e.tags.set(Tag.objects.filter(name__in=["TTT", "SWC"]))
         p1 = Person.objects.create(
@@ -208,7 +211,12 @@ def testContext(self):
                 instructors=[p2],
                 supporting_instructors=[p4],
                 helpers=[p1, p3],
-                all_emails=["hg@magic.uk", "draco@malfoy.com", "hp@magic.uk"],
+                all_emails=[
+                    "hg@magic.uk",
+                    "draco@malfoy.com",
+                    "hp@magic.uk",
+                    "peter@webslinger.net",
+                ],
                 assignee="Regional Coordinator",
                 reports_link="https://workshop-reports.carpentries.org/"
                              "?key=e18dd84d093be5cd6c6ccaf63d38a8477ca126f4"