Hack ugly formset search. Must rewrite this

This commit is contained in:
Mario Hüttel 2022-01-04 00:14:06 +01:00
parent a3fc0e8447
commit 2d83c9ceec
3 changed files with 46 additions and 3 deletions

View File

@ -276,6 +276,29 @@ class ComponentParameterSearchForm(forms.Form):
Column('value')
)
)
def clean(self):
cleaned_data = super().clean()
parameter = cleaned_data.get('parameter')
value = cleaned_data.get('value')
if value != '' or value != None:
value = value.strip()
if value == '' or value == None:
cleaned_data['value'] = None
value = None
if parameter and value is not None and value != '':
if parameter.parameter_type != 'F':
try:
cleaned_data['value'] = EngineeringNumberConverter.engineering_to_number(value)
except:
raise ValidationError('Cannot convert value to number')
return cleaned_data
class ComponentParameterCreateForm(forms.Form):
parameter_type = AutocompleteForeingKeyField(required=True, foreign_model=parts_models.ComponentParameterType, api_search_url='componentparametertype-list', image_field_name=None, name_field_name='descriptive_name')
value = forms.CharField(required=True, max_length=256)

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=1)
ParameterSearchFormSet = formset_factory(ComponentParameterSearchForm, extra=2)
class QrSearchForm(forms.Form):
my_qr_validator = QrCodeValidator()
@ -181,6 +181,17 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
queryset = queryset.filter(manufacturer=cleaned_data['manufacturer'])
return queryset
def filter_queryset_with_parameters(self, queryset, parameter, value):
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))
else:
return queryset.filter(Q(componentparameter__value=value))
return queryset
def get_context_data_int(self, advanced_search, parameter_formset : ParameterSearchFormSet, **kwargs):
context = super().get_context_data(**kwargs)
@ -198,7 +209,12 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
if parameter_formset.is_valid():
# Process parameters
pass
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'])
else:
search = self.request.GET.get('search', default=None)
@ -557,9 +573,10 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
if edit_form.is_valid():
edit_form.save()
else:
pass # Todo: Handle error
pass
context = self.get_context_data(**kwargs)
return self.render_to_response(context)
def handle_amount_change_post(self, request, increase, **kwargs):

View File

@ -31,6 +31,9 @@
<input type="submit" name="submit-advanced-search" value="Search" class="btn btn-success">
</div>
</form>
<template id="advanced-search-parameter-template">
{% crispy advanced_search_param_formset.empty_form %}
</template>
</div>
<div class="list-group mb-3">
{% for comp in components %}