from django.http import Http404 from django.views.generic import TemplateView from bds.autocomplete import bds_search from bds.mixins import StaffRequiredMixin class AutocompleteView(StaffRequiredMixin, TemplateView): template_name = "bds/search_results.html" def get_context_data(self, *args, **kwargs): ctx = super().get_context_data(*args, **kwargs) if "q" not in self.request.GET: raise Http404 q = self.request.GET["q"] ctx["q"] = q results = bds_search.search(q.split()) ctx.update(results) ctx["total"] = sum((len(r) for r in results.values())) return ctx class Home(StaffRequiredMixin, TemplateView): template_name = "bds/home.html"