Add comparison method for parameter search

This commit is contained in:
Mario Hüttel 2022-01-07 16:24:22 +01:00
parent a34557499a
commit 2bc0f3124c
2 changed files with 20 additions and 10 deletions

View File

@ -263,9 +263,17 @@ class AdvancedComponentSearchForm(forms.Form):
),
)
PARAMETER_COMPARISON_TYPES = (
('eq', '=='),
('lte', '<='),
('gte', '>='),
)
class ComponentParameterSearchForm(forms.Form):
parameter = AutocompleteForeingKeyField(required=True, foreign_model=parts_models.ComponentParameterType, api_search_url='componentparametertype-list', image_field_name=None, name_field_name='parameter_name')
value = forms.CharField(max_length=100, required=False)
compare_method = forms.ChoiceField(choices=PARAMETER_COMPARISON_TYPES, required=True, initial=1)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@ -275,6 +283,7 @@ class ComponentParameterSearchForm(forms.Form):
self.helper.layout = Layout(
Row(
Column('parameter'),
Column('compare_method'),
Column('value')
)
)

View File

@ -23,7 +23,7 @@ from django.db.models.functions import Lower
from django.forms import formset_factory
import uuid
ParameterSearchFormSet = formset_factory(ComponentParameterSearchForm, extra=2)
ParameterSearchFormSet = formset_factory(ComponentParameterSearchForm, extra=1)
class QrSearchForm(forms.Form):
my_qr_validator = QrCodeValidator()
@ -181,15 +181,19 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
queryset = queryset.filter(manufacturer=cleaned_data['manufacturer'])
return queryset
def filter_queryset_with_parameters(self, queryset, parameter, value):
def filter_queryset_with_parameters(self, queryset, parameter, value, compare_method):
if parameter and (value is None or value == ''):
return queryset.filter(Q(componentparameter__parameter_type=parameter))
elif parameter and value is not None:
if parameter.parameter_type == 'F':
return queryset.filter(Q(componentparameter__text_value__icontains=value))
return queryset.filter(Q(componentparameter__text_value__icontains=value) & Q(componentparameter__parameter_type=parameter))
else:
return queryset.filter(Q(componentparameter__value=value))
if compare_method == 'lte': # <=
return queryset.filter(Q(componentparameter__value__lte=value) & Q(componentparameter__parameter_type=parameter))
elif compare_method == 'gte': # >=
return queryset.filter(Q(componentparameter__value__gte=value) & Q(componentparameter__parameter_type=parameter))
else:
return queryset.filter(Q(componentparameter__value=value) & Q(componentparameter__parameter_type=parameter))
return queryset
def get_context_data_int(self, advanced_search, parameter_formset : ParameterSearchFormSet, **kwargs):
@ -212,8 +216,7 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
for f in parameter_formset:
# If the form is valid and has changed compared to its initial empty state
if f.is_valid() and f.has_changed():
print(f.cleaned_data)
paginator_queryset = self.filter_queryset_with_parameters(paginator_queryset, f.cleaned_data['parameter'], f.cleaned_data['value'])
paginator_queryset = self.filter_queryset_with_parameters(paginator_queryset, f.cleaned_data['parameter'], f.cleaned_data['value'], f.cleaned_data['compare_method'])
else:
@ -228,8 +231,6 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
if not parameter_formset:
context['advanced_search_param_formset'] = ParameterSearchFormSet()
if not advanced_search:
context['advanced_search_form'] = AdvancedComponentSearchForm(auto_id='adv_search_%s')
@ -239,7 +240,7 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
adv_search_form = None
adv_param_search_formset = None
if 'submit-advanced-search' in self.request.GET:
adv_search_form = AdvancedComponentSearchForm(data=self.request.GET)
adv_search_form = AdvancedComponentSearchForm(auto_id='adv_search_%s', data=self.request.GET)
adv_param_search_formset = ParameterSearchFormSet(data=self.request.GET)
if adv_search_form.is_valid():
print('Advanced search valid')