diff --git a/shimatta_kenkyusho/parts/forms.py b/shimatta_kenkyusho/parts/forms.py index 6e72706..d73b585 100644 --- a/shimatta_kenkyusho/parts/forms.py +++ b/shimatta_kenkyusho/parts/forms.py @@ -6,6 +6,9 @@ from shimatta_modules.EngineeringNumberConverter import EngineeringNumberConvert import uuid from django.urls import reverse +from crispy_forms.helper import FormHelper +from crispy_forms.layout import Layout, Fieldset, Row, Column + class AutoCompleteWidget(widgets.Input): template_name = 'widgets/autocomplete-foreign-key.html' @@ -215,4 +218,35 @@ class DistributorNumberDeleteForm(forms.Form): distributor_number = parts_models.DistributorNum.objects.get(id=my_uuid) except: raise ValidationError('distributor number invalid') - return distributor_number \ No newline at end of file + return distributor_number + +class AdvancedComponentSearchForm(forms.Form): + name = forms.CharField(max_length=255, label='Component Name', required=False) + package = AutocompleteForeingKeyField(required=False, api_search_url='package-list', foreign_model=parts_models.Package) + package_pin_count = forms.IntegerField(min_value=0, label='Pin Count', required=False) + distributor_num = forms.CharField(max_length=100, label='Distributor Part Number', required=False) + distributor = AutocompleteForeingKeyField(required=False, api_search_url='distributor-list', foreign_model=parts_models.Distributor) + component_type = AutocompleteForeingKeyField(required=False, api_search_url='componenttype-list', foreign_model=parts_models.ComponentType, image_field_name=None, name_field_name='class_name') + manufacturer = AutocompleteForeingKeyField(required=False, api_search_url='manufacturer-list', foreign_model=parts_models.Manufacturer) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.helper = FormHelper() + self.helper.form_tag = False + self.helper.layout = Layout( + Row( + Column('name'), + ), + Row( + Column('package'), + Column('package_pin_count'), + ), + Row( + Column('component_type'), + Column('manufacturer'), + ), + Row( + Column('distributor'), + Column('distributor_num'), + ), + ) \ No newline at end of file diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index 0228fca..7ce8ec2 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -157,20 +157,57 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): return queryset - def get_context_data(self, **kwargs): + def get_component_queryset_from_advanced_search(self, cleaned_data): + queryset = Component.objects.all() + + if cleaned_data['name']: + queryset = queryset.filter(Q(name__icontains=cleaned_data['name'])) + if cleaned_data['package']: + queryset = queryset.filter(package=cleaned_data['package']) + if cleaned_data['package_pin_count']: + queryset = queryset.filter(package__pin_count=cleaned_data['package_pin_count']) + if cleaned_data['component_type']: + queryset = queryset.filter(component_type=cleaned_data['component_type']) + if cleaned_data['distributor_num']: + if cleaned_data['distributor']: + distri = cleaned_data['distributor'] + queryset = queryset.filter(Q(distributornum__distributor_part_number__icontains=cleaned_data['distributor_num']) & Q(distributornum__distributor=distri)) + else: + queryset = queryset.filter(Q(distributornum__distributor_part_number__icontains=cleaned_data['distributor_num'])) + if cleaned_data['manufacturer']: + queryset = queryset.filter(manufacturer=cleaned_data['manufacturer']) + return queryset + + def get_context_data_int(self, advanced_search, **kwargs): context = super().get_context_data(**kwargs) comp_page_num = self.request.GET.get('comp_page', default=1) - search = self.request.GET.get('search', default=None) + if advanced_search: + search = None + context['advanced_search_shown'] = True + context['advanced_search_form'] = advanced_search + if advanced_search.is_valid(): + paginator_queryset = self.get_component_queryset_from_advanced_search(advanced_search.cleaned_data) + else: + paginator_queryset = Component.objects.all() + else: + search = self.request.GET.get('search', default=None) + paginator_queryset = self.get_component_query_set(search) - comp_paginator = Paginator(self.get_component_query_set(search), self.default_page_size) + comp_paginator = Paginator(paginator_queryset, self.default_page_size) context['components'] = comp_paginator.get_page(comp_page_num) context['comp_form'] = ComponentForm() context['search_string'] = search + 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) + def handle_new_component_post(self, request, **kwargs): cform = ComponentForm(data=request.POST, files=request.FILES) @@ -181,10 +218,23 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): if not cform.is_valid(): context['comp_form'] = cform return self.render_to_response(context) + + def handle_advanced_search_post(self, request, **kwargs): + + form = AdvancedComponentSearchForm(auto_id='adv_search_%s', data=request.POST) + + if form.is_valid(): + print('Valid') + + + context = self.get_context_data_int(form, **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) + elif 'submit-advanced-search' in request.POST: + return self.handle_advanced_search_post(request, **kwargs) else: return super().post(request, *args, **kwargs) diff --git a/shimatta_kenkyusho/templates/parts/components.html b/shimatta_kenkyusho/templates/parts/components.html index 2c07673..8cadc65 100644 --- a/shimatta_kenkyusho/templates/parts/components.html +++ b/shimatta_kenkyusho/templates/parts/components.html @@ -1,4 +1,5 @@ {% extends 'base.html' %} +{% load crispy_forms_tags %} {% block content %}