diff --git a/bookwyrm/templates/settings/manage_invite_requests.html b/bookwyrm/templates/settings/manage_invite_requests.html index 7a7088d9b..03ddd2104 100644 --- a/bookwyrm/templates/settings/manage_invite_requests.html +++ b/bookwyrm/templates/settings/manage_invite_requests.html @@ -26,11 +26,20 @@ {% endif %} ({{ count }}) + {% include 'settings/invite_request_filters.html' %} + + {% url 'settings-invite-requests' as url %} - + - + {% if not requests %} @@ -50,7 +59,7 @@ {% endif %}
{% trans "Date" %} + {% trans "Date" as text %} + {% include 'snippets/table-sort-header.html' with field="created_date" sort=sort text=text %} + {% trans "Email" %}{% trans "Status" %} + {% trans "Status" as text %} + {% include 'snippets/table-sort-header.html' with field="invite__times_used" sort=sort text=text %} + {% trans "Action" %}
-
+ {% csrf_token %} {% if not req.invite %} diff --git a/bookwyrm/views/invite.py b/bookwyrm/views/invite.py index 49b5c91b1..665f98ce7 100644 --- a/bookwyrm/views/invite.py +++ b/bookwyrm/views/invite.py @@ -1,9 +1,15 @@ """ invites when registration is closed """ +from functools import reduce +import operator +from urllib.parse import urlencode + from django.contrib.auth.decorators import login_required, permission_required from django.core.paginator import Paginator +from django.db.models import Q from django.http import HttpResponseBadRequest from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse +from django.urls import reverse from django.utils.decorators import method_decorator from django.views import View from django.views.decorators.http import require_POST @@ -92,10 +98,34 @@ class ManageInviteRequests(View): except ValueError: page = 1 + sort = request.GET.get("sort") + sort_fields = ["created_date", "invite__times_used"] + if not sort in sort_fields + ["-{:s}".format(f) for f in sort_fields]: + sort = "-created_date" + + requests = models.InviteRequest.objects.filter(ignored=ignored).order_by(sort) + + status_filters = [ + s + for s in request.GET.getlist("status") + if s in ["requested", "sent", "accepted"] + ] + + filters = [] + if "requested" in status_filters: + filters.append({"invite__isnull": True}) + if "sent" in status_filters: + filters.append({"invite__isnull": False}) + if "accepted" in status_filters: + filters.append({"invite__isnull": False, "invite__times_used__gte": 1}) + + if filters: + requests = requests.filter( + reduce(operator.or_, (Q(**f) for f in filters)) + ).distinct() + paginated = Paginator( - models.InviteRequest.objects.filter(ignored=ignored).order_by( - "-created_date" - ), + requests, PAGE_LENGTH, ) @@ -103,6 +133,7 @@ class ManageInviteRequests(View): "ignored": ignored, "count": paginated.count, "requests": paginated.page(page), + "sort": sort, } return TemplateResponse(request, "settings/manage_invite_requests.html", data) @@ -119,7 +150,11 @@ class ManageInviteRequests(View): ) invite_request.save() emailing.invite_email(invite_request) - return redirect("settings-invite-requests") + return redirect( + "{:s}?{:s}".format( + reverse("settings-invite-requests"), urlencode(request.GET.dict()) + ) + ) class InviteRequest(View):