Add user-search in the BDS app
This commit is contained in:
parent
001a832116
commit
2cff19cee1
37
bds/autocomplete.py
Normal file
37
bds/autocomplete.py
Normal 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()
|
73
bds/templates/bds/search_results.html
Normal file
73
bds/templates/bds/search_results.html
Normal 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>
|
10
bds/urls.py
10
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"),
|
||||
]
|
||||
|
|
18
bds/views.py
18
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
|
||||
|
|
Loading…
Reference in a new issue