diff --git a/shimatta_kenkyusho/api/views.py b/shimatta_kenkyusho/api/views.py index 78722a0..ede5fef 100644 --- a/shimatta_kenkyusho/api/views.py +++ b/shimatta_kenkyusho/api/views.py @@ -65,7 +65,7 @@ class PartsComponentParameterTypeViewSet(viewsets.ModelViewSet): serializer_class = ComponentParameterTypeSerializer permission_classes = [permissions.DjangoModelPermissions] filter_backends = [filters.SearchFilter] - search_fields = ['name'] + search_fields = ['parameter_name'] class PartsManufacturerViewSet(viewsets.ModelViewSet): queryset = parts_models.Manufacturer.objects.all() diff --git a/shimatta_kenkyusho/parts/forms.py b/shimatta_kenkyusho/parts/forms.py index d73b585..93f6a52 100644 --- a/shimatta_kenkyusho/parts/forms.py +++ b/shimatta_kenkyusho/parts/forms.py @@ -249,4 +249,19 @@ class AdvancedComponentSearchForm(forms.Form): Column('distributor'), Column('distributor_num'), ), + ) + +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) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.helper = FormHelper() + self.helper.form_tag = False + self.helper.layout = Layout( + Row( + Column('parameter'), + Column('value') + ) ) \ No newline at end of file diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index 7ce8ec2..fa410d0 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -20,8 +20,11 @@ from django.db.models import ProtectedError from .forms import * from django.db.models import Q from django.db.models.functions import Lower +from django.forms import formset_factory import uuid +ParameterSearchFormSet = formset_factory(ComponentParameterSearchForm, extra=1) + class QrSearchForm(forms.Form): my_qr_validator = QrCodeValidator() @@ -178,19 +181,25 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): queryset = queryset.filter(manufacturer=cleaned_data['manufacturer']) return queryset - def get_context_data_int(self, advanced_search, **kwargs): + def get_context_data_int(self, advanced_search, parameter_formset : ParameterSearchFormSet, **kwargs): context = super().get_context_data(**kwargs) comp_page_num = self.request.GET.get('comp_page', default=1) - if advanced_search: + if advanced_search and parameter_formset: search = None context['advanced_search_shown'] = True context['advanced_search_form'] = advanced_search + context['advanced_search_param_formset'] = parameter_formset if advanced_search.is_valid(): paginator_queryset = self.get_component_queryset_from_advanced_search(advanced_search.cleaned_data) else: paginator_queryset = Component.objects.all() + + if parameter_formset.is_valid(): + # Process parameters + pass + else: search = self.request.GET.get('search', default=None) paginator_queryset = self.get_component_query_set(search) @@ -200,39 +209,54 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): context['components'] = comp_paginator.get_page(comp_page_num) context['comp_form'] = ComponentForm() context['search_string'] = search + + if not parameter_formset: + context['advanced_search_param_formset'] = ParameterSearchFormSet() + + if not advanced_search: context['advanced_search_form'] = AdvancedComponentSearchForm(auto_id='adv_search_%s') return context def get_context_data(self, **kwargs): - return self.get_context_data_int(advanced_search = None, **kwargs) + return self.get_context_data_int(advanced_search = None, parameter_formset=None, **kwargs) - def handle_new_component_post(self, request, **kwargs): + def handle_new_component_post(self, request, open=False, **kwargs): cform = ComponentForm(data=request.POST, files=request.FILES) + new_component = None if cform.is_valid(): - cform.save() + new_component = cform.save() context = self.get_context_data(**kwargs) if not cform.is_valid(): context['comp_form'] = cform + + if open and new_component: + return redirect(reverse('parts-components-detail', kwargs={'uuid':new_component.id})) return self.render_to_response(context) def handle_advanced_search_post(self, request, **kwargs): form = AdvancedComponentSearchForm(auto_id='adv_search_%s', data=request.POST) - + param_formset = ParameterSearchFormSet(data=request.POST) + if form.is_valid(): print('Valid') + if param_formset.is_valid(): + print('Formset is valid!') - context = self.get_context_data_int(form, **kwargs) + + context = self.get_context_data_int(form, param_formset, **kwargs) return self.render_to_response(context) def post(self, request, *args, **kwargs): if 'submit-edit-component' in request.POST: - return self.handle_new_component_post(request, **kwargs) + return self.handle_new_component_post(request, open=False, **kwargs) + elif 'submit-edit-component-open' in request.POST: + return self.handle_new_component_post(request, open=True, **kwargs) elif 'submit-advanced-search' in request.POST: return self.handle_advanced_search_post(request, **kwargs) else: diff --git a/shimatta_kenkyusho/templates/parts/components.html b/shimatta_kenkyusho/templates/parts/components.html index 8cadc65..c031198 100644 --- a/shimatta_kenkyusho/templates/parts/components.html +++ b/shimatta_kenkyusho/templates/parts/components.html @@ -16,14 +16,17 @@ -
+
{% crispy advanced_search_form %}
+ {{ advanced_search_param_formset.management_form }}
- Foo + {% for f in advanced_search_param_formset %} + {% crispy f %} + {% endfor %}
@@ -60,7 +63,7 @@
-{% include 'parts/modals/edit-component-modal.html' with form=comp_form heading='New Component' %} +{% include 'parts/modals/edit-component-modal.html' with form=comp_form heading='New Component' open_component_button=True %} {% endblock content %} {% block custom_scripts %} @@ -69,8 +72,5 @@ {% if comp_form.errors %} bootstrap.Modal.getOrCreateInstance(document.getElementById('comp-edit-modal')).show() {% endif %} -{% if advanced_search_shown %} -bootstrap.Collapse.getOrCreateInstance(document.getElementById('advanced-search-collapse')).show() -{% endif %} {% endblock custom_scripts %} \ No newline at end of file diff --git a/shimatta_kenkyusho/templates/parts/modals/edit-component-modal.html b/shimatta_kenkyusho/templates/parts/modals/edit-component-modal.html index 1966e97..5d95aa1 100644 --- a/shimatta_kenkyusho/templates/parts/modals/edit-component-modal.html +++ b/shimatta_kenkyusho/templates/parts/modals/edit-component-modal.html @@ -2,6 +2,7 @@ Needs following context: - heading - form EditComponentForm +- open_component_button {% endcomment %} {% load static %} @@ -21,6 +22,9 @@ Needs following context: