Implement simple search for components

This commit is contained in:
Mario Hüttel 2022-01-02 17:57:00 +01:00
parent aa7b35522b
commit 3c51c2485c

View File

@ -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