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