Start advanced search

This commit is contained in:
Mario Hüttel 2022-01-02 20:00:29 +01:00
parent 3c51c2485c
commit 5c9f0d0f0a
4 changed files with 109 additions and 6 deletions

View File

@ -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
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'),
),
)

View File

@ -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)

View File

@ -1,4 +1,5 @@
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
@ -11,8 +12,23 @@
<button type="submit" class="btn btn-primary">
<i class="bi bi-search"></i>
</button>
<button class="btn btn-success" type="button" data-bs-toggle="modal" data-bs-target="#comp-edit-modal"><i class="bi bi-plus-circle"></i> Add Component</button> </div>
<button class="btn btn-secondary" type="button" data-bs-toggle="collapse" href="#advanced-search-collapse">Advanced <i class="bi bi-search"></i></button>
<button class="btn btn-success" type="button" data-bs-toggle="modal" data-bs-target="#comp-edit-modal"><i class="bi bi-plus-circle"></i> Add Component</button>
</div>
</form>
<div class="collapse mb-3" id="advanced-search-collapse">
<form method="POST">
<div class="row">
<div class="col-sm">
{% crispy advanced_search_form %}
</div>
<div class="col-sm">
Foo
</div>
<input type="submit" name="submit-advanced-search" value="Search" class="btn btn-success">
</div>
</form>
</div>
<div class="list-group mb-3">
{% for comp in components %}
<a href="{% url 'parts-components-detail' uuid=comp.id %}" class="text-decoration-none">
@ -53,5 +69,8 @@
{% 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 %}
</script>
{% endblock custom_scripts %}

View File

@ -57,7 +57,7 @@
</button>
</div>
</form>
<div class="list-group">
<div class="list-group mb-3">
{% for stock in stocks %}
<li class="list-group-item list-group-item-action d-flex align-items-center">
<div class="flex-shrink-0">