Add formset for parameter search (not functional) and improve usability
This commit is contained in:
parent
5c9f0d0f0a
commit
cab865c8fe
@ -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()
|
||||
|
@ -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')
|
||||
)
|
||||
)
|
@ -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:
|
||||
|
@ -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>
|
||||
</div>
|
||||
</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">
|
||||
<div class="row">
|
||||
<div class="col-sm">
|
||||
{% crispy advanced_search_form %}
|
||||
</div>
|
||||
{{ advanced_search_param_formset.management_form }}
|
||||
<div class="col-sm">
|
||||
Foo
|
||||
{% for f in advanced_search_param_formset %}
|
||||
{% crispy f %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
<input type="submit" name="submit-advanced-search" value="Search" class="btn btn-success">
|
||||
</div>
|
||||
@ -60,7 +63,7 @@
|
||||
</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 %}
|
||||
{% 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 %}
|
||||
</script>
|
||||
{% endblock custom_scripts %}
|
@ -2,6 +2,7 @@
|
||||
Needs following context:
|
||||
- heading
|
||||
- form EditComponentForm
|
||||
- open_component_button
|
||||
{% endcomment %}
|
||||
|
||||
{% load static %}
|
||||
@ -21,6 +22,9 @@ Needs following context:
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<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>
|
||||
</form>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user