forked from DGNum/gestioCOF
Generic autocompletion view
This commit is contained in:
parent
30783d677b
commit
e7517195cd
20 changed files with 272 additions and 309 deletions
|
@ -1,4 +1,9 @@
|
|||
from collections import namedtuple
|
||||
|
||||
from dal import autocomplete
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.http import Http404
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
from shared.autocomplete import ModelSearch
|
||||
|
||||
|
@ -9,3 +14,44 @@ class Select2QuerySetView(ModelSearch, autocomplete.Select2QuerySetView):
|
|||
def get_queryset(self):
|
||||
keywords = self.q.split()
|
||||
return super().search(keywords)
|
||||
|
||||
|
||||
Section = namedtuple("Section", ("name", "verbose_name", "entries"))
|
||||
Entry = namedtuple("Entry", ("verbose_name", "link"))
|
||||
|
||||
|
||||
class AutocompleteView(TemplateView):
|
||||
template_name = "shared/search_results.html"
|
||||
search_composer = None
|
||||
|
||||
def get_search_composer(self):
|
||||
if self.search_composer is None:
|
||||
raise ImproperlyConfigured("Please specify a search composer")
|
||||
return self.search_composer
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
ctx = super().get_context_data(**kwargs)
|
||||
if "q" not in self.request.GET:
|
||||
raise Http404
|
||||
q = self.request.GET["q"]
|
||||
ctx["q"] = q
|
||||
ctx["results"] = self.search(keywords=q.split())
|
||||
return ctx
|
||||
|
||||
def search(self, keywords):
|
||||
search_composer = self.get_search_composer()
|
||||
raw_results = search_composer.search(keywords)
|
||||
sections = []
|
||||
for name, unit in search_composer.search_units:
|
||||
entries = [
|
||||
Entry(
|
||||
verbose_name=unit.result_verbose_name(res),
|
||||
link=unit.result_link(res),
|
||||
)
|
||||
for res in raw_results[name]
|
||||
]
|
||||
if entries:
|
||||
sections.append(
|
||||
Section(name=name, verbose_name=unit.verbose_name, entries=entries)
|
||||
)
|
||||
return sections
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue