From 3c51c2485cba3f214775131ec21fe66349bdf776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sun, 2 Jan 2022 17:57:00 +0100 Subject: [PATCH] Implement simple search for components --- shimatta_kenkyusho/parts/views.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index 7774c74..0228fca 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -145,15 +145,30 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): navbar_selected = 'Components' default_page_size = 25 + def get_component_query_set(self, search_string): + queryset = Component.objects.all() + + if not search_string: + return queryset + + search_fragments = search_string.strip().split() + for search in search_fragments: + queryset = queryset.filter(Q(name__icontains = search) | Q(manufacturer__name__icontains = search) | Q(package__name__icontains = search)) + + return queryset + def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) comp_page_num = self.request.GET.get('comp_page', default=1) - comp_paginator = Paginator(Component.objects.all(), self.default_page_size) + search = self.request.GET.get('search', default=None) + + comp_paginator = Paginator(self.get_component_query_set(search), self.default_page_size) context['components'] = comp_paginator.get_page(comp_page_num) context['comp_form'] = ComponentForm() + context['search_string'] = search return context