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