Ajout de la fonctionnalité de recherche

This commit is contained in:
Maurice Debray 2022-02-20 11:17:05 +01:00
parent 1ad025e046
commit d60c50490a
4 changed files with 86 additions and 2 deletions

View file

@ -0,0 +1,43 @@
from django.db.models import Q
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from petitscours.models import PetitCoursDemande
from shared import autocomplete
class DemandeSearch(autocomplete.ModelSearch):
"""
Classe fille de Modelsearch qui recherche les requêtes selon qu'elles soient traitées ou non
"""
model = PetitCoursDemande
search_fields = [ "name", "email", "phone"]
def __init__(self, processed, *args, **kwargs):
super().__init__(*args, **kwargs)
self.processed = processed
if processed:
self.verbose_name = _("Demandes traitées")
else:
self.verbose_name = _("Demandes non traitées")
def get_queryset_filter(self, *args, **kwargs):
qset_filter = super().get_queryset_filter(*args, **kwargs)
qset_filter &= Q(traitee=self.processed)
return qset_filter
def result_verbose_name(self, result):
return f"{result.name}, {result.created.strftime('%d/%m/%Y')}"
def result_link(self, demande):
return reverse("petits-cours-demande-details", args=(demande.pk,))
class DemandeAutocomplete(autocomplete.Compose):
search_units = [
("not processed", DemandeSearch(False)),
("processed", DemandeSearch(True)),
]
demande_autocomplete = DemandeAutocomplete()

View file

@ -1,8 +1,13 @@
{% extends "petitscours/base_title.html" %} {% extends "petitscours/base_title.html" %}
{% load staticfiles %} {% load staticfiles %}
{% block extra_head %}
<script src="{% static 'vendor/jquery/jquery.autocomplete-light.min.js' %}" type="text/javascript"></script>
{% endblock %}
{% block realcontent %} {% block realcontent %}
<h2>Demandes de petits cours</h2> <h2>Demandes de petits cours </h2>
<input type="text" name="q" id="search_autocomplete" spellcheck="false" placeholder="Chercher un demandeur de petits cours" autocomplete="off">
{% if object_list %} {% if object_list %}
<table class="table table-stripped"> <table class="table table-stripped">
<thead> <thead>
@ -44,4 +49,27 @@
{% else %} {% else %}
<p>Aucune demande :(</p> <p>Aucune demande :(</p>
{% endif %} {% endif %}
<!-- Code pour l'autocomplétion de la recherche -->
<div class="yourlabs-autocomplete"></div>
<script type="text/javascript">
$(document).ready(function() {
$('input#search_autocomplete').yourlabsAutocomplete({
url: '{% url 'petits-cours-demandes-search' %}',
minimumCharacters: 3,
id: 'search_autocomplete',
choiceSelector: 'li:has(a)',
box: $(".yourlabs-autocomplete"),
});
});
$('input#search_autocomplete').bind(
'selectChoice',
function(e, choice, autocomplete) {
autocomplete.hide();
link = choice.find('a:first');
if (link.length && link.attr('href') != undefined) {
document.location.href = link.attr('href');
}
}
);
</script>
{% endblock %} {% endblock %}

View file

@ -2,7 +2,7 @@ from django.urls import path
from gestioncof.decorators import buro_required from gestioncof.decorators import buro_required
from petitscours import views from petitscours import views
from petitscours.views import DemandeDetailView, DemandeListView from petitscours.views import DemandeDetailView, DemandeListView, DemandeAutocompleteView
urlpatterns = [ urlpatterns = [
path("inscription", views.inscription, name="petits-cours-inscription"), path("inscription", views.inscription, name="petits-cours-inscription"),
@ -18,6 +18,11 @@ urlpatterns = [
buro_required(DemandeListView.as_view()), buro_required(DemandeListView.as_view()),
name="petits-cours-demandes-list", name="petits-cours-demandes-list",
), ),
path(
"demandes/search",
DemandeAutocompleteView.as_view(),
name="petits-cours-demandes-search",
),
path( path(
"demandes/<int:pk>", "demandes/<int:pk>",
buro_required(DemandeDetailView.as_view()), buro_required(DemandeDetailView.as_view()),

View file

@ -22,6 +22,10 @@ from petitscours.models import (
PetitCoursDemande, PetitCoursDemande,
) )
from petitscours.autocomplete import demande_autocomplete
from gestioncof.decorators import BuroRequiredMixin
from shared.views import AutocompleteView
class DemandeListView(ListView): class DemandeListView(ListView):
queryset = PetitCoursDemande.objects.prefetch_related("matieres").order_by( queryset = PetitCoursDemande.objects.prefetch_related("matieres").order_by(
@ -45,6 +49,10 @@ class DemandeDetailView(DetailView):
context["attributions"] = obj.petitcoursattribution_set.all() context["attributions"] = obj.petitcoursattribution_set.all()
return context return context
class DemandeAutocompleteView(BuroRequiredMixin, AutocompleteView):
template_name = "shared/search_results.html"
search_composer = demande_autocomplete
@buro_required @buro_required
def traitement(request, demande_id, redo=False): def traitement(request, demande_id, redo=False):