Commit b293f7a2 authored by Chris Lamb's avatar Chris Lamb
Browse files

Add simple ordering and filtering for the (admin-only) list of users. (Closes: #48)

parent c6c0a6ab
from django import forms
from django.contrib.auth import get_user_model
User = get_user_model()
class FilterForm(forms.Form):
order_by = forms.ChoiceField(choices=(
('-date_joined', "Date joined (most recent first)"),
('date_joined', "Date joined (oldest first)"),
('email', "Email (ascending)"),
('-email', "Email (descending)"),
), required=False)
only_admins = forms.BooleanField(required=False)
def apply_filter(self, qs):
if self.cleaned_data['only_admins']:
qs = qs.filter(is_staff=True)
qs = qs.order_by(self.cleaned_data['order_by'], 'pk')
return qs
def clean_order_by(self):
val = self.cleaned_data['order_by']
return val or self.fields['order_by'].choices[0][0]
from django.http import HttpResponseBadRequest
from django.contrib import messages
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth import get_user_model
......@@ -6,16 +7,23 @@ from django.views.decorators.http import require_POST
from freenodejobs.utils.paginator import AutoPaginator
from freenodejobs.utils.decorators import staff_required
from .forms import FilterForm
User = get_user_model()
def view(request):
users = User.objects.order_by('-is_staff', 'date_joined')
form = FilterForm(request.GET)
if not form.is_valid():
return HttpResponseBadRequest(form.errors.as_json())
page = AutoPaginator(request, users, 20).current_page()
qs = User.objects.all()
page = AutoPaginator(request, form.apply_filter(qs), 20).current_page()
return render(request, 'admin/users/view.html', {
'form': form,
'page': page,
$.feature('f_admin_users', function() {
$('.js-filter-form-control').on('change', function() {
{% extends "admin/base.html" %}
{% block body_classes %}{{ block.super }} f_admin_users{% endblock %}
{% block title %}Users — {{ block.super }}{% endblock %}
{% block subtitle %}Users{% endblock %}
{% block admin %}
<form method="GET" action="" class="form-inline pull-right mb-3">
<label class="col-form-label mr-2" for="{{ form.order_by.id_for_label }}">
Order by:
id="{{ form.order_by.id_for_label }}"
class="js-filter-form-control form-control"
name="{{ form.order_by.html_name }}"
{% for val, display in form.order_by.field.choices %}
value="{{ val }}"
{% if form.order_by.value|stringformat:"s" == val|stringformat:"s" %}selected{% endif %}
>{{ display }}</option>
{% endfor %}
<div class="form-check form-check-inline ml-4">
<label class="form-check-label">
id="{{ form.only_admins.id_for_label }}"
name="{{ form.only_admins.html_name }}"
class="js-filter-form-control form-check-input"
{% if form.only_admins.value %}checked{% endif %}
Only show admins
<div class="clearfix"></div>
<table class="table table-striped mb-4">
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment