Skip to content

Commit

Permalink
fixup! add some filters to the volunteers list
Browse files Browse the repository at this point in the history
  • Loading branch information
struan committed Nov 21, 2024
1 parent d664a72 commit 463ccf9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
8 changes: 7 additions & 1 deletion crowdsourcer/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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")

Expand Down
9 changes: 9 additions & 0 deletions crowdsourcer/templates/crowdsourcer/volunteers/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ <h1 class="mb-4">Volunteers</h1>
{% bootstrap_field filter.form.has_assignments %}
</div>
</div>
<div class="col-5 col-md-4 col-lg">
<div class="form-group">
{% bootstrap_field filter.form.assigned_section %}
</div>
</div>
<div class="col-5 col-md-5 col-lg">
<div class="form-group">
{% bootstrap_field filter.form.username %}
Expand All @@ -50,6 +55,7 @@ <h1 class="mb-4">Volunteers</h1>
<tr>
<th>Name/Email</th>
<th>Stage</th>
<th>Section</th>
<th>Assignments</th>
<th>Active</th>
</tr>
Expand All @@ -63,6 +69,9 @@ <h1 class="mb-4">Volunteers</h1>
<td>
{{ volunteer.marker.response_type|default:"First Mark" }}
</td>
<td>
{{ volunteer.assigned_section }}
</td>
<td>
<a title="show progress" href="{% session_url 'volunteer_progress' volunteer.pk %}">{{ volunteer.num_assignments }}</a>
<a title="assign to volunteer" href="{% session_url 'assign_volunteer' volunteer.pk %}">edit</a>
Expand Down
26 changes: 26 additions & 0 deletions crowdsourcer/views/volunteers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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")
)

Expand Down

0 comments on commit 463ccf9

Please sign in to comment.