diff --git a/representatives_votes/autocompletes.py b/representatives_votes/autocompletes.py new file mode 100644 index 0000000000000000000000000000000000000000..ee1e817fb61fcaa5a6d00c4bb663af96aabb20fe --- /dev/null +++ b/representatives_votes/autocompletes.py @@ -0,0 +1,19 @@ +from dal import autocomplete + +from django.db.models import Q + +from models import Proposal + + +class ProposalAutocomplete(autocomplete.Select2QuerySetView): + def get_queryset(self): + qs = Proposal.objects.all() + + if self.q: + qs = qs.filter( + Q(dossier__title__icontains=self.q) | + Q(title__icontains=self.q) | + Q(reference__icontains=self.q) + ) + + return qs diff --git a/representatives_votes/urls.py b/representatives_votes/urls.py index fc32da7a1e95433240ae8f4bfedd0578a81d17b6..e6e2b7475f6bf1780790d49e80065d88a4b1661c 100644 --- a/representatives_votes/urls.py +++ b/representatives_votes/urls.py @@ -1,3 +1,4 @@ +from django.conf import settings from django.conf.urls import url import views @@ -14,3 +15,14 @@ urlpatterns = [ name='dossier-list' ), ] + +if 'dal_select2' in settings.INSTALLED_APPS: + from autocompletes import ProposalAutocomplete # noqa + + urlpatterns.append( + url( + 'autocomplete/proposal/$', + ProposalAutocomplete.as_view(), + name='proposal-autocomplete', + ), + )