diff --git a/shimatta_kenkyusho/parts/forms.py b/shimatta_kenkyusho/parts/forms.py index e33173b..b750fb2 100644 --- a/shimatta_kenkyusho/parts/forms.py +++ b/shimatta_kenkyusho/parts/forms.py @@ -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) diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index 2eff882..6d58f93 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -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): diff --git a/shimatta_kenkyusho/templates/parts/components.html b/shimatta_kenkyusho/templates/parts/components.html index c031198..e77bfff 100644 --- a/shimatta_kenkyusho/templates/parts/components.html +++ b/shimatta_kenkyusho/templates/parts/components.html @@ -31,6 +31,9 @@ +
{% for comp in components %}