Repository: carpentries/amy

Issue:
Search must look in applications for training
Search doesn't currently pull up matches in training applications - it should.



Discussion:

Resolving patch:
diff --git a/workshops/forms.py b/workshops/forms.py
--- a/workshops/forms.py
+++ b/workshops/forms.py
@@ -292,6 +292,9 @@ class SearchForm(forms.Form):
     in_airports = forms.BooleanField(label='in airports',
                                      required=False,
                                      initial=True)
+    in_training_requests = forms.BooleanField(label='in training requests',
+                                              required=False,
+                                              initial=True)
 
     helper = BootstrapHelper(add_cancel_button=False)
 
diff --git a/workshops/models.py b/workshops/models.py
--- a/workshops/models.py
+++ b/workshops/models.py
@@ -2052,7 +2052,16 @@ def get_absolute_url(self):
         return reverse('trainingrequest_details', args=[self.pk])
 
     def __str__(self):
-        return "Training request #{}".format(self.pk)
+        return (
+            '{personal} {family} <{email}> - {state}'
+            .format(
+                state=self.get_state_display(),
+                personal=self.personal,
+                family=self.family,
+                email=self.email,
+                timestamp=self.created_at,
+            )
+        )
 
 
 @reversion.register
diff --git a/workshops/views.py b/workshops/views.py
--- a/workshops/views.py
+++ b/workshops/views.py
@@ -1994,7 +1994,8 @@ def workshop_staff(request):
 def search(request):
     '''Search the database by term.'''
 
-    term, organizations, events, persons, airports = '', None, None, None, None
+    term = ''
+    organizations = events = persons = airports = training_requests = None
 
     if request.method == 'POST':
         form = SearchForm(request.POST)
@@ -2051,6 +2052,18 @@ def search(request):
                     .order_by('iata')
                 results += list(airports)
 
+            if form.cleaned_data['in_training_requests']:
+                training_requests = TrainingRequest.objects.filter(
+                    Q(group_name__contains=term) |
+                    Q(family__contains=term) |
+                    Q(email__contains=term) |
+                    Q(github__contains=term) |
+                    Q(affiliation__contains=term) |
+                    Q(location__contains=term) |
+                    Q(comment__contains=term)
+                )
+                results += list(training_requests)
+
             # only 1 record found? Let's move to it immediately
             if len(results) == 1:
                 return redirect(results[0].get_absolute_url())
@@ -2059,13 +2072,16 @@ def search(request):
     else:
         form = SearchForm()
 
-    context = {'title' : 'Search',
-               'form': form,
-               'term' : term,
-               'organizations' : organizations,
-               'events' : events,
-               'persons' : persons,
-               'airports' : airports}
+    context = {
+        'title': 'Search',
+        'form': form,
+        'term': term,
+        'organizations' : organizations,
+        'events': events,
+        'persons': persons,
+        'airports': airports,
+        'training_requests': training_requests,
+    }
     return render(request, 'workshops/search.html', context)
 
 #------------------------------------------------------------


Test patch:
diff --git a/workshops/test/test_search.py b/workshops/test/test_search.py
--- a/workshops/test/test_search.py
+++ b/workshops/test/test_search.py
@@ -1,5 +1,5 @@
 from django.core.urlresolvers import reverse
-from ..models import Organization, Person
+from ..models import Organization, Person, TrainingRequest
 from .base import TestBase
 
 
@@ -76,3 +76,43 @@ def test_search_for_people_by_personal_family_names(self):
         texts = set([n.text for n in nodes])
         assert texts == {str(self.org_alpha), str(self.hermione)}, \
             'Wrong names {0} in search result'.format(texts)
+
+    def test_search_for_training_requests(self):
+        """Make sure that finding training requests works."""
+
+        # added so that the search doesn't redirect with only 1 result
+        Person.objects.create(
+            personal='Victor', family='Krum', email='vkrum@durmstrang.edu',
+            github='vkrum Lorem Ipsum Leprechauns',
+        )
+
+        TrainingRequest.objects.create(
+            group_name='Leprechauns', personal='Victor', family='Krum',
+            email='vkrum@durmstrang.edu', github='vkrum',
+            comment='Lorem Ipsum',
+        )
+
+        search_options = {
+            'term': 'Leprechaun',
+            'in_training_requests': 'on',
+            'in_persons': 'on',
+        }
+        url = reverse('search')
+
+        response = self.client.post(url, search_options)
+        self.assertEqual(len(response.context['training_requests']), 1)
+
+        search_options['term'] = 'Krum'
+        response = self.client.post(url, search_options)
+        self.assertEqual(len(response.context['training_requests']), 1)
+
+        search_options['term'] = 'Lorem'
+        response = self.client.post(url, search_options)
+        self.assertEqual(len(response.context['training_requests']), 1)
+
+        search_options['term'] = 'Potter'
+        # otherwise it'd redirect to Harry Potter's profile
+        del search_options['in_persons']
+        response = self.client.post(url, search_options)
+        self.assertEqual(len(response.context['training_requests']), 0)
+