added basic search lexer and parser enabling search for parameters and attributes - could be extended
the error handling is very basic, but effective some missing features like ordering ...and maybe a potential security as the search will allow search for all attributes somehow related to the component model
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import uuid
|
||||
from django.shortcuts import redirect
|
||||
from django.urls import reverse
|
||||
from django.views.generic import TemplateView, DetailView
|
||||
@@ -8,6 +7,7 @@ from django.db.models import Q
|
||||
from django.forms import formset_factory
|
||||
from django.db import IntegrityError
|
||||
from django.db.models import ProtectedError
|
||||
from shimatta_modules.ShimattaSearchLanguage import ShimattaSearchLanguage
|
||||
from ..models import Stock, Component, ComponentParameter, DistributorNum, PackageParameter
|
||||
from ..forms import *
|
||||
from .component_import import import_components_from_csv
|
||||
@@ -26,15 +26,23 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
|
||||
|
||||
def get_component_query_set(self, search_string):
|
||||
queryset = Component.objects.all()
|
||||
error_string = ''
|
||||
|
||||
if not search_string:
|
||||
return queryset
|
||||
|
||||
search_parser = ShimattaSearchLanguage()
|
||||
query, errors = search_parser.search_for_components(search_string)
|
||||
|
||||
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))
|
||||
if query:
|
||||
try:
|
||||
queryset = queryset.filter(query)
|
||||
except Exception as ex:
|
||||
error_string = str(ex)
|
||||
else:
|
||||
error_string = '<br><br>'.join(errors)
|
||||
|
||||
return queryset
|
||||
return queryset, error_string
|
||||
|
||||
def get_component_queryset_from_advanced_search(self, cleaned_data):
|
||||
queryset = Component.objects.all()
|
||||
@@ -60,6 +68,8 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
|
||||
def get_context_data_int(self, advanced_search, parameter_formset : ParameterSearchFormSet, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
|
||||
errors = ''
|
||||
|
||||
comp_page_num = self.request.GET.get('comp_page', default=1)
|
||||
|
||||
if advanced_search and parameter_formset:
|
||||
@@ -79,7 +89,7 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
|
||||
|
||||
else:
|
||||
search = self.request.GET.get('search', default=None)
|
||||
paginator_queryset = self.get_component_query_set(search)
|
||||
paginator_queryset, errors = self.get_component_query_set(search)
|
||||
|
||||
comp_paginator = Paginator(paginator_queryset, self.default_page_size)
|
||||
|
||||
@@ -87,11 +97,11 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
|
||||
context['comp_form'] = ComponentForm()
|
||||
context['import_comp_form'] = ImportComponentForm()
|
||||
context['search_string'] = search
|
||||
context['errors'] = errors
|
||||
|
||||
if not parameter_formset:
|
||||
context['advanced_search_param_formset'] = ParameterSearchFormSet()
|
||||
|
||||
|
||||
if not advanced_search:
|
||||
context['advanced_search_form'] = AdvancedComponentSearchForm(auto_id='adv_search_%s')
|
||||
|
||||
|
Reference in New Issue
Block a user