Repository: carpentries/amy

Issue:
Match Secondary Email in WRF
Hi Piotr, 
We recently added the option to include a secondary email in AMY's person record. I'm creating this issue to verify or update our person matching for instructor training applications and/or all workshop request forms to the secondary email address if used.



Discussion:
Once matched, person's secondary email gets updated with what's in Training Request secondary email field.

However, secondary email from Training Request was not used to search for matching person. This will be changed.

Workshop requests (and workshop inquiries, and self-organised submissions) aren't meant for creating persons, so they don't offer "match with this person" capability.

Resolving patch:
diff --git a/amy/extrequests/views.py b/amy/extrequests/views.py
--- a/amy/extrequests/views.py
+++ b/amy/extrequests/views.py
@@ -697,11 +697,24 @@ def trainingrequest_details(request, pk):
         else:
             # No person is matched to the TrainingRequest yet. Suggest a
             # person from existing records.
-            person = Person.objects.filter(Q(email__iexact=req.email) |
-                                           Q(personal__iexact=req.personal,
-                                             middle__iexact=req.middle,
-                                             family__iexact=req.family)) \
-                                   .first()  # may return None
+            primary_email = (
+                Q(email__iexact=req.email) |
+                Q(secondary_email__iexact=req.email)
+            )
+            # only match secondary email if there's one provided, otherwise
+            # we could get false-positive matches for empty email.
+            secondary_email = Q() if not req.secondary_email else (
+                Q(email__iexact=req.secondary_email) |
+                Q(secondary_email__iexact=req.secondary_email)
+            )
+            name = Q(
+                personal__iexact=req.personal,
+                middle__iexact=req.middle,
+                family__iexact=req.family,
+            )
+            person = Person.objects \
+                           .filter(primary_email | secondary_email | name) \
+                           .first()  # may return None
         form = MatchTrainingRequestForm(initial={'person': person})
 
     context = {


Test patch:
diff --git a/amy/extrequests/tests/test_training_request.py b/amy/extrequests/tests/test_training_request.py
--- a/amy/extrequests/tests/test_training_request.py
+++ b/amy/extrequests/tests/test_training_request.py
@@ -485,6 +485,19 @@ def test_person_is_suggested(self):
 
         self.assertEqual(rv.context['form'].initial['person'], p)
 
+    def test_person_is_suggested_based_on_secondary_email(self):
+        """Having just secondary email matching should show the person
+        in results."""
+        req = create_training_request(state='p', person=None)
+        p = Person.objects.create(username='john_kowalsky',
+                                  personal='Johnny',
+                                  family='Kowalsky',
+                                  email='asdf@gmail.com',
+                                  secondary_email='john@smith.com')
+        rv = self.client.get(reverse('trainingrequest_details', args=[req.pk]))
+
+        self.assertEqual(rv.context['form'].initial['person'], p)
+
     def test_new_person(self):
         req = create_training_request(state='p', person=None)
         rv = self.client.get(reverse('trainingrequest_details', args=[req.pk]))