diff --git a/crowdsourcer/filters.py b/crowdsourcer/filters.py index eb56c07..f43cc45 100644 --- a/crowdsourcer/filters.py +++ b/crowdsourcer/filters.py @@ -2,7 +2,7 @@ import django_filters -from crowdsourcer.models import ResponseType +from crowdsourcer.models import ResponseType, Section def filter_not_empty(queryset, name, value): @@ -17,6 +17,12 @@ class VolunteerFilter(django_filters.FilterSet): marker__response_type = django_filters.ChoiceFilter( label="Stage", choices=ResponseType.choices() ) + assigned_section = django_filters.ChoiceFilter( + field_name="assigned_section", + label="Assigned Section", + lookup_expr="icontains", + choices=Section.objects.values_list("title", "title"), + ) # have to specify it like this otherwise bootstrap doesn't recognise it as a bound field username = django_filters.CharFilter(field_name="username", lookup_expr="icontains") diff --git a/crowdsourcer/templates/crowdsourcer/volunteers/list.html b/crowdsourcer/templates/crowdsourcer/volunteers/list.html index e6586f3..464396c 100644 --- a/crowdsourcer/templates/crowdsourcer/volunteers/list.html +++ b/crowdsourcer/templates/crowdsourcer/volunteers/list.html @@ -32,6 +32,11 @@

Volunteers

{% bootstrap_field filter.form.has_assignments %} +
+
+ {% bootstrap_field filter.form.assigned_section %} +
+
{% bootstrap_field filter.form.username %} @@ -50,6 +55,7 @@

Volunteers

Name/Email Stage + Section Assignments Active @@ -63,6 +69,9 @@

Volunteers

{{ volunteer.marker.response_type|default:"First Mark" }} + + {{ volunteer.assigned_section }} + {{ volunteer.num_assignments }} edit diff --git a/crowdsourcer/views/volunteers.py b/crowdsourcer/views/volunteers.py index 6faa3ef..eead60b 100644 --- a/crowdsourcer/views/volunteers.py +++ b/crowdsourcer/views/volunteers.py @@ -2,6 +2,7 @@ from django.contrib.auth.mixins import UserPassesTestMixin from django.contrib.auth.models import User +from django.contrib.postgres.aggregates import StringAgg from django.db.models import Count, OuterRef, Subquery from django.http import JsonResponse from django.shortcuts import get_object_or_404 @@ -43,6 +44,14 @@ class VolunteersView(VolunteerAccessMixin, FilterView): context_object_name = "volunteers" filterset_class = VolunteerFilter + def get_filterset(self, filterset_class): + fs = super().get_filterset(filterset_class) + + fs.filters["assigned_section"].field.choices = Section.objects.filter( + marking_session=self.request.current_session + ).values_list("title", "title") + return fs + def get_queryset(self): qs = ( User.objects.filter(marker__marking_session=self.request.current_session) @@ -58,6 +67,23 @@ def get_queryset(self): .values("num_assignments") ) ) + .annotate( + assigned_section=( + Subquery( + Assigned.objects.filter( + marking_session=self.request.current_session, + user=OuterRef("pk"), + ) + .values_list("user") + .annotate( + joined_title=StringAgg( + "section__title", distinct=True, delimiter=", " + ) + ) + .values("joined_title") + ) + ) + ) .order_by("username") )