Skip to content

Commit

Permalink
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 6f736d6 commit d664a72
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions ceuk-marking/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"django.contrib.staticfiles",
"compressor",
"django_bootstrap5",
"django_filters",
"django_json_widget",
"crowdsourcer",
]
Expand Down
28 changes: 28 additions & 0 deletions crowdsourcer/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.contrib.auth.models import User

import django_filters

from crowdsourcer.models import ResponseType


def filter_not_empty(queryset, name, value):
lookup = "__".join([name, "isnull"])
return queryset.filter(**{lookup: not value})


class VolunteerFilter(django_filters.FilterSet):
has_assignments = django_filters.BooleanFilter(
field_name="num_assignments", method=filter_not_empty, label="Has assignments"
)
marker__response_type = django_filters.ChoiceFilter(
label="Stage", choices=ResponseType.choices()
)
# 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")

class Meta:
model = User
fields = {
"marker__response_type": ["exact"],
"is_active": ["exact"],
}
5 changes: 5 additions & 0 deletions crowdsourcer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ class ResponseType(models.Model):
priority = models.IntegerField()
active = models.BooleanField(default=False)

@classmethod
def choices(cls):
choices = cls.objects.values_list("pk", "type")
return choices

def __str__(self):
return self.type

Expand Down
6 changes: 6 additions & 0 deletions crowdsourcer/static/css/_forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@
margin-bottom: 0;
}
}

#volunteer_filter {
.btn-primary {
margin-top: 2rem;
}
}
32 changes: 32 additions & 0 deletions crowdsourcer/templates/crowdsourcer/volunteers/list.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% extends 'crowdsourcer/base.html' %}

{% load crowdsourcer_tags %}
{% load django_bootstrap5 %}

{% block content %}
{% if show_login %}
Expand All @@ -12,7 +13,38 @@ <h1 class="mb-4">Sign in</h1>
<a href="{% session_url 'bulk_assign_volunteer' %}">Bulk assignment</a>
<a href="{% session_url 'deactivate_volunteers' %}">Stage deactivate</a>
</div>

<h1 class="mb-4">Volunteers</h1>
<form method="GET" id="volunteer_filter">
<div class="row">
<div class="col-5 col-md-4 col-lg">
<div class="form-group">
{% bootstrap_field filter.form.marker__response_type %}
</div>
</div>
<div class="col-5 col-md-4 col-lg">
<div class="form-group">
{% bootstrap_field filter.form.is_active %}
</div>
</div>
<div class="col-5 col-md-4 col-lg">
<div class="form-group">
{% bootstrap_field filter.form.has_assignments %}
</div>
</div>
<div class="col-5 col-md-5 col-lg">
<div class="form-group">
{% bootstrap_field filter.form.username %}
</div>
</div>
<div class="col col-md-4 col-lg">
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block mb-0">Filter list</button>
</div>
</div>
</div>
</form>

<table class="table">
<thead>
<tr>
Expand Down
10 changes: 8 additions & 2 deletions crowdsourcer/views/volunteers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from django.urls import reverse
from django.views.generic import FormView, ListView

from django_filters.views import FilterView

from crowdsourcer.filters import VolunteerFilter
from crowdsourcer.forms import (
CreateMarkerForm,
MarkerFormset,
Expand Down Expand Up @@ -35,12 +38,13 @@ def test_func(self):
return self.request.user.has_perm("crowdsourcer.can_manage_users")


class VolunteersView(VolunteerAccessMixin, ListView):
class VolunteersView(VolunteerAccessMixin, FilterView):
template_name = "crowdsourcer/volunteers/list.html"
context_object_name = "volunteers"
filterset_class = VolunteerFilter

def get_queryset(self):
return (
qs = (
User.objects.filter(marker__marking_session=self.request.current_session)
.select_related("marker")
.annotate(
Expand All @@ -57,6 +61,8 @@ def get_queryset(self):
.order_by("username")
)

return qs


class VolunteerAddView(VolunteerAccessMixin, FormView):
template_name = "crowdsourcer/volunteers/create.html"
Expand Down

0 comments on commit d664a72

Please sign in to comment.