From 2cff19cee11ba23eb469f8435eb913b0014edd9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 5 Jun 2020 17:02:16 +0200 Subject: [PATCH] Add user-search in the BDS app --- bds/autocomplete.py | 37 ++++++++++++++ bds/templates/bds/search_results.html | 73 +++++++++++++++++++++++++++ bds/urls.py | 10 +++- bds/views.py | 18 ++++++- 4 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 bds/autocomplete.py create mode 100644 bds/templates/bds/search_results.html diff --git a/bds/autocomplete.py b/bds/autocomplete.py new file mode 100644 index 00000000..0a240cea --- /dev/null +++ b/bds/autocomplete.py @@ -0,0 +1,37 @@ +from django.contrib.auth import get_user_model +from django.db.models import Q + +from shared.views import autocomplete + +User = get_user_model() + + +class BDSMemberSearch(autocomplete.ModelSearch): + model = User + search_fields = ["username", "first_name", "last_name"] + + def get_queryset_filter(self, *args, **kwargs): + qset_filter = super().get_queryset_filter(*args, **kwargs) + qset_filter &= Q(bds__is_member=True) + return qset_filter + + +class BDSOthersSearch(autocomplete.ModelSearch): + model = User + search_fields = ["username", "first_name", "last_name"] + + def get_queryset_filter(self, *args, **kwargs): + qset_filter = super().get_queryset_filter(*args, **kwargs) + qset_filter &= Q(bds__isnull=True) | Q(bds__is_member=False) + return qset_filter + + +class BDSSearch(autocomplete.Compose): + search_units = [ + ("members", "username", BDSMemberSearch), + ("others", "username", BDSOthersSearch), + ("clippers", "clipper", autocomplete.LDAPSearch), + ] + + +bds_search = BDSSearch() diff --git a/bds/templates/bds/search_results.html b/bds/templates/bds/search_results.html new file mode 100644 index 00000000..b1c46622 --- /dev/null +++ b/bds/templates/bds/search_results.html @@ -0,0 +1,73 @@ +{% load i18n %} +{% load search_utils %} + + diff --git a/bds/urls.py b/bds/urls.py index e4487422..8e72a1c1 100644 --- a/bds/urls.py +++ b/bds/urls.py @@ -1,2 +1,8 @@ -app_label = "bds" -urlpatterns = [] +from django.urls import path + +from bds import views + +app_name = "bds" +urlpatterns = [ + path("autocomplete", views.AutocompleteView.as_view(), name="autocomplete"), +] diff --git a/bds/views.py b/bds/views.py index 60f00ef0..40670a56 100644 --- a/bds/views.py +++ b/bds/views.py @@ -1 +1,17 @@ -# Create your views here. +from django.http import Http404 +from django.views.generic import TemplateView + +from bds.autocomplete import bds_search + + +class AutocompleteView(TemplateView): + template_name = "bds/search_results.html" + + def get_context_data(self, *args, **kwargs): + ctx = super().get_context_data(*args, **kwargs) + if "q" not in self.request.GET: + raise Http404 + q = self.request.GET["q"] + ctx["q"] = q + ctx.update(bds_search.search(q.split())) + return ctx