diff --git a/petitscours/autocomplete.py b/petitscours/autocomplete.py new file mode 100644 index 00000000..9377dc51 --- /dev/null +++ b/petitscours/autocomplete.py @@ -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() diff --git a/petitscours/templates/petitscours/demande_list.html b/petitscours/templates/petitscours/demande_list.html index e4c3c782..69440fec 100644 --- a/petitscours/templates/petitscours/demande_list.html +++ b/petitscours/templates/petitscours/demande_list.html @@ -1,8 +1,13 @@ {% extends "petitscours/base_title.html" %} {% load staticfiles %} +{% block extra_head %} + +{% endblock %} + {% block realcontent %} -

Demandes de petits cours

+

Demandes de petits cours

+ {% if object_list %} @@ -44,4 +49,27 @@ {% else %}

Aucune demande :(

{% endif %} + +
+ {% endblock %} diff --git a/petitscours/urls.py b/petitscours/urls.py index b4bf1538..ae26990a 100644 --- a/petitscours/urls.py +++ b/petitscours/urls.py @@ -2,7 +2,7 @@ from django.urls import path from gestioncof.decorators import buro_required from petitscours import views -from petitscours.views import DemandeDetailView, DemandeListView +from petitscours.views import DemandeDetailView, DemandeListView, DemandeAutocompleteView urlpatterns = [ path("inscription", views.inscription, name="petits-cours-inscription"), @@ -18,6 +18,11 @@ urlpatterns = [ buro_required(DemandeListView.as_view()), name="petits-cours-demandes-list", ), + path( + "demandes/search", + DemandeAutocompleteView.as_view(), + name="petits-cours-demandes-search", + ), path( "demandes/", buro_required(DemandeDetailView.as_view()), diff --git a/petitscours/views.py b/petitscours/views.py index 81504e15..ec5272cb 100644 --- a/petitscours/views.py +++ b/petitscours/views.py @@ -22,6 +22,10 @@ from petitscours.models import ( PetitCoursDemande, ) +from petitscours.autocomplete import demande_autocomplete +from gestioncof.decorators import BuroRequiredMixin + +from shared.views import AutocompleteView class DemandeListView(ListView): queryset = PetitCoursDemande.objects.prefetch_related("matieres").order_by( @@ -45,6 +49,10 @@ class DemandeDetailView(DetailView): context["attributions"] = obj.petitcoursattribution_set.all() return context +class DemandeAutocompleteView(BuroRequiredMixin, AutocompleteView): + template_name = "shared/search_results.html" + search_composer = demande_autocomplete + @buro_required def traitement(request, demande_id, redo=False):