Implement Paginated search using a mixin

This commit is contained in:
Mario Hüttel 2023-06-09 23:40:30 +02:00
parent 3d263ca27c
commit 8636c513b7
4 changed files with 18 additions and 17 deletions

View File

@ -38,6 +38,15 @@ class QrSearchForm(forms.Form):
qr_search = forms.CharField(label='qr_search', validators=[my_qr_validator])
class KeepSearchParamMixin(object):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
search = self.request.GET.get('search', default=None)
if search:
context['additional_params'] = urlencode({'search': search})
return context
class BaseTemplateMixin(object):
navbar_selected = ''
base_title = ''
@ -199,7 +208,7 @@ class ComponentTypeDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView)
return context
class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
class ComponentView(LoginRequiredMixin, KeepSearchParamMixin, BaseTemplateMixin, TemplateView):
template_name = 'parts/components.html'
base_title = 'Components'
navbar_selected = 'Components'
@ -284,7 +293,6 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
context['components'] = comp_paginator.get_page(comp_page_num)
context['comp_form'] = ComponentForm()
context['search_string'] = search
context['additional_get_params'] = urlencode({'search': self.request.GET.get('search', default='')}) if search else None
if not parameter_formset:
context['advanced_search_param_formset'] = ParameterSearchFormSet()
@ -330,7 +338,7 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
else:
return super().post(request, *args, **kwargs)
class PackageView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
class PackageView(LoginRequiredMixin, KeepSearchParamMixin, BaseTemplateMixin, TemplateView):
template_name = 'parts/packages.html'
base_title = 'Packages'
navbar_selected = 'Packages'
@ -369,11 +377,6 @@ class PackageView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
context['search_string'] = search_string
context['packages'] = paginator.get_page(page_num)
context['new_pkg_form'] = PackageForm()
get_params_for_paginator = None
if search_string:
get_params_for_paginator = urlencode({'search': search_string})
context['additional_get_params'] = get_params_for_paginator
return context
@ -397,7 +400,7 @@ class PackageView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
return super().post(request, *args, **kwargs)
class DistributorView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
class DistributorView(LoginRequiredMixin, KeepSearchParamMixin, BaseTemplateMixin, TemplateView):
template_name = 'parts/distributors.html'
base_title = 'Distributors'
navbar_selected = 'Distributors'
@ -511,7 +514,7 @@ class StockView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
return super().post(request, **kwargs)
class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
class StockViewDetail(LoginRequiredMixin, KeepSearchParamMixin, BaseTemplateMixin, DetailView):
template_name = 'parts/stocks-detail.html'
model = Storage
pk_url_kwarg = 'uuid'
@ -573,8 +576,6 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
context['add_storage_form'] = add_storage_form
context['delete_storage_error'] = None
context['add_stock_form'] = AddStockForm()
if stock_search_input:
context['additional_params'] = urlencode({'search': stock_search_input})
return context
def handle_add_storage_post(self, request, **kwargs):
@ -914,7 +915,7 @@ class DistributorDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
return super().post(request, *args, **kwargs)
class ManufacturersViewSet(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
class ManufacturersViewSet(LoginRequiredMixin, KeepSearchParamMixin, BaseTemplateMixin, TemplateView):
template_name = 'parts/manufacturers.html'
base_title = 'Manufacturers'
navbar_selected = 'Manufacturers'

View File

@ -33,7 +33,7 @@
</a>
{% endfor %}
</div>
{% include 'paginator.html' with paginator=comptypes get_param='page' aria_label='Component Type Page Navigation' additional_params=None %}
{% include 'paginator.html' with paginator=comptypes get_param='page' aria_label='Component Type Page Navigation' %}
</div>
</div>
</div>

View File

@ -66,7 +66,7 @@
</a>
{% endfor %}
</div>
{% include 'paginator.html' with paginator=components get_param='comp_page' aria_label='Component Page Navigation' additional_params=additional_get_params %}
{% include 'paginator.html' with paginator=components get_param='comp_page' aria_label='Component Page Navigation' %}
</div>
</div>
</div>

View File

@ -18,7 +18,7 @@
</div>
</form>
<div class="mb-3">
{% include 'paginator.html' with paginator=packages get_param='page' aria_label='Package Page Navigation' additional_params=additional_get_params %}
{% include 'paginator.html' with paginator=packages get_param='page' aria_label='Package Page Navigation' %}
</div>
<div class="list-group mb-3">
{% for pkg in packages %}
@ -42,7 +42,7 @@
</a>
{% endfor %}
</div>
{% include 'paginator.html' with paginator=packages get_param='page' aria_label='Package Page Navigation' additional_params=additional_get_params %}
{% include 'paginator.html' with paginator=packages get_param='page' aria_label='Package Page Navigation' %}
</div>
</div>
</div>