Add user-search in the BDS app

This commit is contained in:
Martin Pépin 2020-06-05 17:02:16 +02:00
parent 0789da7bed
commit bca75dbf98
No known key found for this signature in database
GPG key ID: E7520278B1774448
4 changed files with 135 additions and 3 deletions

37
bds/autocomplete.py Normal file
View file

@ -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()

View file

@ -0,0 +1,73 @@
{% load i18n %}
{% load search_utils %}
<ul>
{% if members %}
<li class="autocomplete-header">
<span class="autocomplete-item">{% trans "Membres" %}</span>
</li>
{% for user in members %}
{% if forloop.counter < 5 %}
<li class="autocomplete-value">
<a class="autocomplete-item" href="#TODO">
{{ user|highlight_user:q }}
</a>
</li>
{% elif forloop.counter == 5 %}
<li class="autocomplete-more">
<span class="autocomplete-item">...</span>
</li>
{% endif %}
{% endfor %}
{% endif %}
{% if others %}
<li class="autocomplete-header">
<span class="autocomplete-item">{% trans "Non-membres" %}</span>
</li>
{% for user in others %}
{% if forloop.counter < 5 %}
<li class="autocomplete-value">
<a class="autocomplete-item" href="#TODO">
{{ user|highlight_user:q }}
</a>
</li>
{% elif forloop.counter == 5 %}
<li class="autocomplete-more">
<span class="autocomplete-item">...</span>
</li>
{% endif %}
{% endfor %}
{% endif %}
{% if clippers %}
<li class="autocomplete-header">{% trans "Utilisateurs <tt>clipper</tt>" %}</li>
{% for clipper in clippers %}
{% if forloop.counter < 5 %}
<li class="autocomplete-value">
<a class="autocomplete-item" href="#TODO">
{{ clipper|highlight_clipper:q }}
</a>
</li>
{% elif forloop.counter == 5 %}
<li class="autocomplete-more">
<span class="autocomplete-item">...</span>
</li>
{% endif %}
{% endfor %}
{% endif %}
{% if total %}
<li class="autocomplete-header">
<span class="autocomplete-item">{% trans "Pas dans la liste ?" %}</span>
</li>
{% else %}
<li class="autocomplete-header">
<span class="autocomplete-item">{% trans "Aucune correspondance trouvée" %}</span>
</li>
{% endif %}
<li class="autocomplete-new">
<a class="autocomplete-item" href="#TODO">{% trans "Créer un compte" %}</a>
</li>
</ul>

View file

@ -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"),
]

View file

@ -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