From cba6a35b6c21892ae996375a80040ab29d4b8dd0 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Thu, 6 Aug 2020 13:07:22 +0200 Subject: [PATCH] Display matched alias in user table --- apps/member/tables.py | 4 +++- apps/member/views.py | 10 ++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/apps/member/tables.py b/apps/member/tables.py index 1247da00..7c2673d9 100644 --- a/apps/member/tables.py +++ b/apps/member/tables.py @@ -38,6 +38,8 @@ class UserTable(tables.Table): """ List all users. """ + alias = tables.Column() + section = tables.Column(accessor='profile.section') balance = tables.Column(accessor='note.balance', verbose_name=_("Balance")) @@ -50,7 +52,7 @@ class UserTable(tables.Table): 'class': 'table table-condensed table-striped table-hover' } template_name = 'django_tables2/bootstrap4.html' - fields = ('last_name', 'first_name', 'username', 'email') + fields = ('last_name', 'first_name', 'username', 'alias', 'email') model = User row_attrs = { 'class': 'table-row', diff --git a/apps/member/views.py b/apps/member/views.py index 3caf5a84..46f56816 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -10,7 +10,7 @@ from django.contrib.auth import logout from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.models import User from django.contrib.auth.views import LoginView -from django.db.models import Q +from django.db.models import Q, F from django.shortcuts import redirect from django.urls import reverse_lazy from django.utils import timezone @@ -172,7 +172,9 @@ class UserListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): """ Filter the user list with the given pattern. """ - qs = super().get_queryset().distinct().filter(profile__registration_valid=True) + qs = super().get_queryset().distinct("pk").annotate(alias=F("note__alias__name"))\ + .annotate(normalized_alias=F("note__alias__normalized_name"))\ + .filter(profile__registration_valid=True) if "search" in self.request.GET: pattern = self.request.GET["search"] @@ -184,8 +186,8 @@ class UserListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): | Q(last_name__iregex=pattern) | Q(profile__section__iregex=pattern) | Q(username__iregex="^" + pattern) - | Q(note__alias__name__iregex="^" + pattern) - | Q(note__alias__normalized_name__iregex=Alias.normalize("^" + pattern)) + | Q(alias__iregex="^" + pattern) + | Q(normalized_alias__iregex=Alias.normalize("^" + pattern)) ) else: qs = qs.none()