Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

volunteer page improvements #208

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
34 changes: 34 additions & 0 deletions crowdsourcer/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.contrib.auth.models import User

import django_filters

from crowdsourcer.models import ResponseType, Section


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()
)
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")

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
1 change: 1 addition & 0 deletions crowdsourcer/templates/crowdsourcer/icons/edit.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 48 additions & 8 deletions crowdsourcer/templates/crowdsourcer/volunteers/list.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,57 @@
{% extends 'crowdsourcer/base.html' %}

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

{% block content %}
{% if show_login %}
<h1 class="mb-4">Sign in</h1>
<a href="{% url 'login' %}">Sign in</a>
{% else %}
<div class="float-end">
<a href="{% session_url 'add_volunteer' %}">Add volunteer</a>
<a href="{% session_url 'bulk_assign_volunteer' %}">Bulk assignment</a>
<a href="{% session_url 'deactivate_volunteers' %}">Stage deactivate</a>
<div class="d-md-flex align-items-center mb-4">
<h1 class="mb-md-0 me-md-auto">Volunteers</h1>
<ul class="nav ms-n3 me-md-n3">
<li class="nav-item">
<a href="{% session_url 'add_volunteer' %}" class="nav-link">Add volunteer</a>
</li>
<li class="nav-item">
<a href="{% session_url 'bulk_assign_volunteer' %}" class="nav-link">Bulk assignment</a>
</li>
<li class="nav-item">
<a href="{% session_url 'deactivate_volunteers' %}" class="nav-link">Stage deactivate</a>
</li>
</ul>
</div>
<h1 class="mb-4">Volunteers</h1>

<form method="GET" id="volunteer_filter" class="bg-light p-3 rounded-sm mb-3">
<div class="row align-items-end flex-wrap mb-n3 mb-md-n4">
<div class="col" style="min-width: 10rem">
{% bootstrap_field filter.form.marker__response_type %}
</div>
<div class="col" style="min-width: 10rem">
{% bootstrap_field filter.form.is_active %}
</div>
<div class="col" style="min-width: 10rem">
{% bootstrap_field filter.form.has_assignments %}
</div>
<div class="col" style="min-width: 10rem">
{% bootstrap_field filter.form.assigned_section %}
</div>
<div class="col" style="min-width: 10rem">
{% bootstrap_field filter.form.username %}
</div>
<div class="col" style="min-width: 10rem">
<button type="submit" class="btn btn-primary btn-block mb-3 mb-md-4">Filter list</button>
</div>
</div>
</form>

<table class="table">
<thead>
<tr>
<th>Name/Email</th>
<th>Stage</th>
<th>Section</th>
<th>Assignments</th>
<th>Active</th>
</tr>
Expand All @@ -26,14 +60,20 @@ <h1 class="mb-4">Volunteers</h1>
{% for volunteer in volunteers %}
<tr>
<td>
<a title="edit user" href="{% session_url 'edit_volunteer' volunteer.pk %}">{{ volunteer.email|default:volunteer.username }}</a>
<a title="Edit user" href="{% session_url 'edit_volunteer' volunteer.pk %}">{{ volunteer.email|default:volunteer.username }}</a>
</td>
<td>
{{ volunteer.marker.response_type|default:"First Mark" }}
</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>
{{ volunteer.assigned_section }}
</td>
<td>
<a title="Show progress" href="{% session_url 'volunteer_progress' volunteer.pk %}">{{ volunteer.num_assignments }}</a>
<a title="Edit assignments" href="{% session_url 'assign_volunteer' volunteer.pk %}" class="ms-1">
{% include 'crowdsourcer/icons/edit.svg' %}
<span class="visually-hidden">Edit</span>
</a>
</td>
<td>
{{ volunteer.is_active }}
Expand Down
36 changes: 34 additions & 2 deletions crowdsourcer/views/volunteers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

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
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 +39,21 @@ 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_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):
return (
qs = (
User.objects.filter(marker__marking_session=self.request.current_session)
.select_related("marker")
.annotate(
Expand All @@ -54,9 +67,28 @@ 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")
)

return qs


class VolunteerAddView(VolunteerAccessMixin, FormView):
template_name = "crowdsourcer/volunteers/create.html"
Expand Down
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ django-bootstrap5 = "^22.2"
django-simple-history = "^3.2.0"
mysoc-dataset = "^0.3.0"
django-json-widget = "^2.0.1"
django-filter = "^24.3"

[tool.poetry.dev-dependencies]
black = "^24.4.2"
Expand Down
Loading