Add formset for parameter search (not functional) and improve usability

This commit is contained in:
Mario Hüttel 2022-01-03 13:18:31 +01:00
parent 5c9f0d0f0a
commit cab865c8fe
5 changed files with 58 additions and 15 deletions

View File

@ -65,7 +65,7 @@ class PartsComponentParameterTypeViewSet(viewsets.ModelViewSet):
serializer_class = ComponentParameterTypeSerializer serializer_class = ComponentParameterTypeSerializer
permission_classes = [permissions.DjangoModelPermissions] permission_classes = [permissions.DjangoModelPermissions]
filter_backends = [filters.SearchFilter] filter_backends = [filters.SearchFilter]
search_fields = ['name'] search_fields = ['parameter_name']
class PartsManufacturerViewSet(viewsets.ModelViewSet): class PartsManufacturerViewSet(viewsets.ModelViewSet):
queryset = parts_models.Manufacturer.objects.all() queryset = parts_models.Manufacturer.objects.all()

View File

@ -250,3 +250,18 @@ class AdvancedComponentSearchForm(forms.Form):
Column('distributor_num'), 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')
)
)

View File

@ -20,8 +20,11 @@ from django.db.models import ProtectedError
from .forms import * from .forms import *
from django.db.models import Q from django.db.models import Q
from django.db.models.functions import Lower from django.db.models.functions import Lower
from django.forms import formset_factory
import uuid import uuid
ParameterSearchFormSet = formset_factory(ComponentParameterSearchForm, extra=1)
class QrSearchForm(forms.Form): class QrSearchForm(forms.Form):
my_qr_validator = QrCodeValidator() my_qr_validator = QrCodeValidator()
@ -178,19 +181,25 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
queryset = queryset.filter(manufacturer=cleaned_data['manufacturer']) queryset = queryset.filter(manufacturer=cleaned_data['manufacturer'])
return queryset 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) context = super().get_context_data(**kwargs)
comp_page_num = self.request.GET.get('comp_page', default=1) comp_page_num = self.request.GET.get('comp_page', default=1)
if advanced_search: if advanced_search and parameter_formset:
search = None search = None
context['advanced_search_shown'] = True context['advanced_search_shown'] = True
context['advanced_search_form'] = advanced_search context['advanced_search_form'] = advanced_search
context['advanced_search_param_formset'] = parameter_formset
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 = Component.objects.all()
if parameter_formset.is_valid():
# Process parameters
pass
else: else:
search = self.request.GET.get('search', default=None) search = self.request.GET.get('search', default=None)
paginator_queryset = self.get_component_query_set(search) 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['components'] = comp_paginator.get_page(comp_page_num)
context['comp_form'] = ComponentForm() context['comp_form'] = ComponentForm()
context['search_string'] = search context['search_string'] = search
if not parameter_formset:
context['advanced_search_param_formset'] = ParameterSearchFormSet()
if not advanced_search: if not advanced_search:
context['advanced_search_form'] = AdvancedComponentSearchForm(auto_id='adv_search_%s') context['advanced_search_form'] = AdvancedComponentSearchForm(auto_id='adv_search_%s')
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, **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) cform = ComponentForm(data=request.POST, files=request.FILES)
new_component = None
if cform.is_valid(): if cform.is_valid():
cform.save() new_component = cform.save()
context = self.get_context_data(**kwargs) context = self.get_context_data(**kwargs)
if not cform.is_valid(): if not cform.is_valid():
context['comp_form'] = cform 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) return self.render_to_response(context)
def handle_advanced_search_post(self, request, **kwargs): def handle_advanced_search_post(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)
if form.is_valid(): if form.is_valid():
print('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) return self.render_to_response(context)
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
if 'submit-edit-component' in request.POST: 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: elif 'submit-advanced-search' in request.POST:
return self.handle_advanced_search_post(request, **kwargs) return self.handle_advanced_search_post(request, **kwargs)
else: else:

View File

@ -16,14 +16,17 @@
<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> <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> </div>
</form> </form>
<div class="collapse mb-3" id="advanced-search-collapse"> <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="POST">
<div class="row"> <div class="row">
<div class="col-sm"> <div class="col-sm">
{% crispy advanced_search_form %} {% crispy advanced_search_form %}
</div> </div>
{{ advanced_search_param_formset.management_form }}
<div class="col-sm"> <div class="col-sm">
Foo {% for f in advanced_search_param_formset %}
{% crispy f %}
{% endfor %}
</div> </div>
<input type="submit" name="submit-advanced-search" value="Search" class="btn btn-success"> <input type="submit" name="submit-advanced-search" value="Search" class="btn btn-success">
</div> </div>
@ -60,7 +63,7 @@
</div> </div>
</div> </div>
{% 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 %} {% endblock content %}
{% block custom_scripts %} {% block custom_scripts %}
@ -69,8 +72,5 @@
{% if comp_form.errors %} {% if comp_form.errors %}
bootstrap.Modal.getOrCreateInstance(document.getElementById('comp-edit-modal')).show() bootstrap.Modal.getOrCreateInstance(document.getElementById('comp-edit-modal')).show()
{% endif %} {% endif %}
{% if advanced_search_shown %}
bootstrap.Collapse.getOrCreateInstance(document.getElementById('advanced-search-collapse')).show()
{% endif %}
</script> </script>
{% endblock custom_scripts %} {% endblock custom_scripts %}

View File

@ -2,6 +2,7 @@
Needs following context: Needs following context:
- heading - heading
- form EditComponentForm - form EditComponentForm
- open_component_button
{% endcomment %} {% endcomment %}
{% load static %} {% load static %}
@ -21,6 +22,9 @@ Needs following context:
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<input type="submit" name="submit-edit-component" class="btn btn-primary" value="Save"> <input type="submit" name="submit-edit-component" class="btn btn-primary" value="Save">
{% if open_component_button %}
<input type="submit" name="submit-edit-component-open" class="btn btn-primary" value="Save and Open">
{% endif %}
</div> </div>
</form> </form>
</div> </div>