From 8636c513b72dd7a1dd15efc3127d52cafb3c97b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Fri, 9 Jun 2023 23:40:30 +0200 Subject: [PATCH] Implement Paginated search using a mixin --- shimatta_kenkyusho/parts/views.py | 27 ++++++++++--------- .../templates/parts/component-types.html | 2 +- .../templates/parts/components.html | 2 +- .../templates/parts/packages.html | 4 +-- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index 74e8adb..5535423 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -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' diff --git a/shimatta_kenkyusho/templates/parts/component-types.html b/shimatta_kenkyusho/templates/parts/component-types.html index 348cb12..40dabee 100644 --- a/shimatta_kenkyusho/templates/parts/component-types.html +++ b/shimatta_kenkyusho/templates/parts/component-types.html @@ -33,7 +33,7 @@ {% endfor %} - {% 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' %} diff --git a/shimatta_kenkyusho/templates/parts/components.html b/shimatta_kenkyusho/templates/parts/components.html index 6336a9e..12bfa07 100644 --- a/shimatta_kenkyusho/templates/parts/components.html +++ b/shimatta_kenkyusho/templates/parts/components.html @@ -66,7 +66,7 @@ {% endfor %} - {% 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' %} diff --git a/shimatta_kenkyusho/templates/parts/packages.html b/shimatta_kenkyusho/templates/parts/packages.html index c655bba..4f85fac 100644 --- a/shimatta_kenkyusho/templates/parts/packages.html +++ b/shimatta_kenkyusho/templates/parts/packages.html @@ -18,7 +18,7 @@
- {% 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' %}
{% for pkg in packages %} @@ -42,7 +42,7 @@ {% endfor %}
- {% 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' %}