Repository: carpentries/amy

Issue:
Support first workshop session
I'm thinking in run once a month a session for instructors that will teach their first workshop. What about generate this list?



Discussion:
Rayna Harris and Sheldon McKay are running a session every two weeks for 
instructors who have recently taught; I'd like those to start including 
people who are about to teach (particularly new instructors).  
Generating this report would therefore be v. useful.
Cheers,
G

When I use `workshops/debrief` I would like to know who taught their first workshop. Feedbacks from everyone are important but the ones from someone that taught for the first time can help us avoid drop outs.

Excellent idea - display count of number of workshops taught beside each 
person's name?

> display count of number of workshops taught beside each person's name?

It can be.


Resolving patch:
diff --git a/workshops/models.py b/workshops/models.py
--- a/workshops/models.py
+++ b/workshops/models.py
@@ -330,6 +330,19 @@ def get_queryset(self):
         """Fetch only existing tasks (ie. not deleted)."""
         return super().get_queryset().filter(deleted=False)
 
+    def instructors(self):
+        """Fetch tasks with role 'instructor'."""
+        return self.get_queryset().filter(role__name="instructor")
+
+    def learners(self):
+        """Fetch tasks with role 'learner'."""
+        return self.get_queryset().filter(role__name="learner")
+
+    def helpers(self):
+        """Fetch tasks with role 'helper'."""
+        return self.get_queryset().filter(role__name="helper")
+
+
 class Task(models.Model):
     '''Represent who did what at events.'''
 
diff --git a/workshops/views.py b/workshops/views.py
--- a/workshops/views.py
+++ b/workshops/views.py
@@ -113,8 +113,6 @@ def index(request):
     upcoming_events = Event.objects.upcoming_events()
     unpublished_events = Event.objects.unpublished_events()
     unpaid_events = Event.objects.unpaid_events()
-    for e in unpublished_events:
-        e.num_instructors = e.task_set.filter(role__name='instructor').count()
     context = {'title': None,
                'upcoming_events': upcoming_events,
                'unpaid_events': unpaid_events,
@@ -409,8 +407,6 @@ def all_events(request):
 
     all_events = Event.objects.all()
     events = _get_pagination_items(request, all_events)
-    for e in events:
-        e.num_instructors = e.task_set.filter(role__name='instructor').count()
     context = {'title' : 'All Events',
                'all_events' : events}
     return render(request, 'workshops/all_events.html', context)
@@ -602,8 +598,7 @@ def instructors(request):
 
             # Add metadata which we will eventually filter by
             for p in persons:
-                p.num_taught = \
-                    p.task_set.filter(role__name='instructor').count()
+                p.num_taught = p.task_set.instructors().count()
 
             # Sort by location.
             loc = (form.cleaned_data['latitude'],


Test patch:
diff --git a/workshops/test/test_tasks.py b/workshops/test/test_tasks.py
--- a/workshops/test/test_tasks.py
+++ b/workshops/test/test_tasks.py
@@ -1,7 +1,10 @@
-from django.core.urlresolvers import reverse
+from itertools import product
 from datetime import datetime
-from ..models import Task, Event, Role, Person, Site
+
+from django.core.urlresolvers import reverse
+
 from .base import TestBase
+from ..models import Task, Event, Role, Person, Site
 
 
 class TestTask(TestBase):
@@ -31,6 +34,20 @@ def setUp(self):
                                             site=test_site,
                                             admin_fee=0)
 
+        test_event_3 = Event.objects.create(start=datetime.now(),
+                                            slug='test_event_3',
+                                            site=test_site,
+                                            admin_fee=0)
+
+        instructor_role = Role.objects.create(name="instructor")
+        learner_role = Role.objects.create(name="learner")
+        helper_role = Role.objects.create(name="helper")
+        roles = [instructor_role, learner_role, helper_role]
+        people = [test_person_1, test_person_2]
+
+        for role, person in product(roles, people):
+            Task.objects.create(person=person, role=role, event=test_event_3)
+
         test_role_1 = Role.objects.create(name='test_role_1')
         test_role_2 = Role.objects.create(name='test_role_2')
 
@@ -59,3 +76,13 @@ def test_task_edit_view_reachable_from_event_person_and_role_of_task(self):
         response = self.client.get(reverse('task_edit',
                                    kwargs=url_kwargs))
         assert response.context['task'].pk == correct_task.pk
+
+    def test_task_manager_roles_lookup(self):
+        """Test TaskManager methods for looking up roles by names."""
+        event = Event.objects.get(slug='test_event_3')
+        instructors = event.task_set.instructors()
+        learners = event.task_set.learners()
+        helpers = event.task_set.helpers()
+        tasks = event.task_set.all()
+
+        assert set(tasks) == set(instructors) | set(learners) | set(helpers)