Restructure Advanced search to a get request

This commit is contained in:
Mario Hüttel 2022-01-07 13:31:50 +01:00
parent 35255cf4e9
commit a34557499a
3 changed files with 20 additions and 16 deletions

View File

@ -244,6 +244,7 @@ class AdvancedComponentSearchForm(forms.Form):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.helper = FormHelper() self.helper = FormHelper()
self.helper.form_tag = False self.helper.form_tag = False
self.helper.disable_csrf = True
self.helper.layout = Layout( self.helper.layout = Layout(
Row( Row(
Column('name'), Column('name'),
@ -270,6 +271,7 @@ class ComponentParameterSearchForm(forms.Form):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.helper = FormHelper() self.helper = FormHelper()
self.helper.form_tag = False self.helper.form_tag = False
self.helper.disable_csrf = True
self.helper.layout = Layout( self.helper.layout = Layout(
Row( Row(
Column('parameter'), Column('parameter'),

View File

@ -149,9 +149,9 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
default_page_size = 25 default_page_size = 25
def get_component_query_set(self, search_string): def get_component_query_set(self, search_string):
queryset = Component.objects.all() queryset = Component.objects.select_related('package', 'manufacturer').all()
if not search_string: if search_string is None or search_string == '':
return queryset return queryset
search_fragments = search_string.strip().split() search_fragments = search_string.strip().split()
@ -161,7 +161,7 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
return queryset return queryset
def get_component_queryset_from_advanced_search(self, cleaned_data): def get_component_queryset_from_advanced_search(self, cleaned_data):
queryset = Component.objects.all() queryset = Component.objects.select_related('manufacturer', 'package').all()
if cleaned_data['name']: if cleaned_data['name']:
queryset = queryset.filter(Q(name__icontains=cleaned_data['name'])) queryset = queryset.filter(Q(name__icontains=cleaned_data['name']))
@ -205,7 +205,7 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
if advanced_search.is_valid(): if advanced_search.is_valid():
paginator_queryset = self.get_component_queryset_from_advanced_search(advanced_search.cleaned_data) paginator_queryset = self.get_component_queryset_from_advanced_search(advanced_search.cleaned_data)
else: else:
paginator_queryset = Component.objects.all() paginator_queryset = self.get_component_query_set(None)
if parameter_formset.is_valid(): if parameter_formset.is_valid():
# Process parameters # Process parameters
@ -236,7 +236,18 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
return context return context
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
return self.get_context_data_int(advanced_search = None, parameter_formset=None, **kwargs) adv_search_form = None
adv_param_search_formset = None
if 'submit-advanced-search' in self.request.GET:
adv_search_form = AdvancedComponentSearchForm(data=self.request.GET)
adv_param_search_formset = ParameterSearchFormSet(data=self.request.GET)
if adv_search_form.is_valid():
print('Advanced search valid')
if adv_param_search_formset.is_valid():
print('Parameter search valid')
return self.get_context_data_int(advanced_search = adv_search_form, parameter_formset=adv_param_search_formset, **kwargs)
def handle_new_component_post(self, request, open=False, **kwargs): def handle_new_component_post(self, request, open=False, **kwargs):
@ -253,18 +264,11 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
return redirect(reverse('parts-components-detail', kwargs={'uuid':new_component.id})) return redirect(reverse('parts-components-detail', kwargs={'uuid':new_component.id}))
return self.render_to_response(context) return self.render_to_response(context)
def handle_advanced_search_post(self, request, **kwargs): def handle_advanced_search_get(self, request, **kwargs):
form = AdvancedComponentSearchForm(auto_id='adv_search_%s', data=request.POST) form = AdvancedComponentSearchForm(auto_id='adv_search_%s', data=request.POST)
param_formset = ParameterSearchFormSet(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, param_formset, **kwargs) context = self.get_context_data_int(form, param_formset, **kwargs)
return self.render_to_response(context) return self.render_to_response(context)
@ -273,8 +277,6 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
return self.handle_new_component_post(request, open=False, **kwargs) return self.handle_new_component_post(request, open=False, **kwargs)
elif 'submit-edit-component-open' in request.POST: elif 'submit-edit-component-open' in request.POST:
return self.handle_new_component_post(request, open=True, **kwargs) 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: else:
return super().post(request, *args, **kwargs) return super().post(request, *args, **kwargs)

View File

@ -17,7 +17,7 @@
</div> </div>
</form> </form>
<div class="collapse mb-3{% if advanced_search_shown %} show{% endif %}" id="advanced-search-collapse" aria-expanded="{% if advanced_search_shown %}true{% else %}false{% endif %}"> <div class="collapse mb-3{% if advanced_search_shown %} show{% endif %}" id="advanced-search-collapse" aria-expanded="{% if advanced_search_shown %}true{% else %}false{% endif %}">
<form method="POST"> <form method="GET">
<div class="row"> <div class="row">
<div class="col-sm"> <div class="col-sm">
{% crispy advanced_search_form %} {% crispy advanced_search_form %}