From ac5d6e4945903812ce2c9a8e1f76cce7e58ed356 Mon Sep 17 00:00:00 2001 From: Eirik Krogstad Date: Thu, 11 Feb 2016 17:06:47 +0100 Subject: [PATCH] Fix assumptions about user model fields, issue #1433 --- CHANGELOG.txt | 2 ++ CONTRIBUTORS.rst | 1 + docs/releases/1.4.rst | 1 + wagtail/wagtailusers/views/users.py | 37 ++++++++++++++++------------- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 12ebcb6efd..1e4fbb5450 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -47,6 +47,8 @@ Changelog * Fix: SASS compilation errors during Wagtail development no longer cause exit of Gulp process, instead throws error to console and continues (Thomas Winter) * Fix: Explorer page listing now uses specific page models, so that custom URL schemes defined on Page subclasses are respected * Fix: Made settings menu clickable again in Firefox 46.0a2 (Juha Kujala) + * Fix: User management index view no longer assumes the presence of `username`, `first_name`, `last_name` and `email` fields on the user model (Eirik Krogstad) + 1.3.1 (05.01.2016) ~~~~~~~~~~~~~~~~~~ diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 71843961ef..e34e0637a5 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -114,6 +114,7 @@ Contributors * Mike Dingjan * Loic Teixeira * Juha Kujala +* Eirik Krogstad Translators =========== diff --git a/docs/releases/1.4.rst b/docs/releases/1.4.rst index 22dcdbba2f..9105fc7bd3 100644 --- a/docs/releases/1.4.rst +++ b/docs/releases/1.4.rst @@ -91,6 +91,7 @@ Bug fixes * SASS compilation errors during Wagtail development no longer cause exit of Gulp process, instead throws error to console and continues (Thomas Winter) * Explorer page listing now uses specific page models, so that custom URL schemes defined on Page subclasses are respected * Made settings menu clickable again in Firefox 46.0a2 (Juha Kujala) + * User management index view no longer assumes the presence of ``username``, ``first_name``, ``last_name`` and ``email`` fields on the user model (Eirik Krogstad) Upgrade considerations diff --git a/wagtail/wagtailusers/views/users.py b/wagtail/wagtailusers/views/users.py index fc0ad93a43..e1a14a4fbd 100644 --- a/wagtail/wagtailusers/views/users.py +++ b/wagtail/wagtailusers/views/users.py @@ -28,39 +28,42 @@ def index(request): q = None is_searching = False + model_fields = [f.name for f in User._meta.get_fields()] + if 'q' in request.GET: form = SearchForm(request.GET, placeholder=_("Search users")) if form.is_valid(): q = form.cleaned_data['q'] - is_searching = True + conditions = Q() - if User.USERNAME_FIELD == 'username': - users = User.objects.filter( - Q(username__icontains=q) | - Q(first_name__icontains=q) | - Q(last_name__icontains=q) | - Q(email__icontains=q) - ) - else: - users = User.objects.filter( - Q(first_name__icontains=q) | - Q(last_name__icontains=q) | - Q(email__icontains=q) - ) + if 'username' in model_fields: + conditions |= Q(username__icontains=q) + + if 'first_name' in model_fields: + conditions |= Q(first_name__icontains=q) + + if 'last_name' in model_fields: + conditions |= Q(last_name__icontains=q) + + if 'email' in model_fields: + conditions |= Q(email__icontains=q) + + users = User.objects.filter(conditions) else: form = SearchForm(placeholder=_("Search users")) if not is_searching: - users = User.objects + users = User.objects.all() - users = users.order_by('last_name', 'first_name') + if 'last_name' in model_fields and 'first_name' in model_fields: + users = users.order_by('last_name', 'first_name') if 'ordering' in request.GET: ordering = request.GET['ordering'] if ordering == 'username': - users = users.order_by(ordering) + users = users.order_by(User.USERNAME_FIELD) else: ordering = 'name'