Skip to content

Commit 58c9fab

Browse files
committed
feat: rework the pagination with per-model setting
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent 7192434 commit 58c9fab

6 files changed

Lines changed: 49 additions & 16 deletions

File tree

component_catalog/views.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,6 @@ class ComponentListView(
353353
template_list_table = "component_catalog/tables/component_list_table.html"
354354
include_reference_dataspace = True
355355
put_results_in_session = True
356-
paginate_by = settings.PAGINATE_BY or 200
357356
group_name_version = True
358357

359358
table_headers = (
@@ -1734,9 +1733,9 @@ class ScanListView(
17341733
AddPackagePermissionMixin,
17351734
APIWrapperListView,
17361735
):
1737-
paginate_by = 50
17381736
template_name = "component_catalog/scan_list.html"
17391737
template_list_table = "component_catalog/tables/scan_list_table.html"
1738+
paginate_by = settings.DEJACODE_PAGINATE_BY.get("scan", 50)
17401739

17411740
def dispatch(self, request, *args, **kwargs):
17421741
user = self.request.user

dejacode/settings.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,23 @@ def gettext_noop(s):
387387
PAGINATE_BY = env.int("PAGINATE_BY", default=None)
388388
TAB_PAGINATE_BY = env.int("TAB_PAGINATE_BY", default=100)
389389

390+
# List views pagination, controls the number of items displayed per page.
391+
# Syntax in .env: DEJACODE_PAGINATE_BY=product=20,package=100
392+
DEJACODE_PAGINATE_BY = env.dict(
393+
"DEJACODE_PAGINATE_BY",
394+
default={
395+
"product": 50,
396+
"component": PAGINATE_BY or 100,
397+
"package": 100,
398+
"license": 100,
399+
"owner": 100,
400+
"report": 50,
401+
"request": 50,
402+
"scan": 50,
403+
"vulnerability": 100,
404+
},
405+
)
406+
390407
ADMIN_FORMS_CONFIGURATION = env.dict("ADMIN_FORMS_CONFIGURATION", default={})
391408

392409
# Location of the changelog file

dje/views.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,29 @@ def get_queryset(self):
260260
return qs.scope(dataspace, include_reference=self.include_reference_dataspace)
261261

262262

263-
class PreviousNextPaginationMixin:
263+
class PaginationMixin:
264264
query_dict_page_param = "page"
265+
paginate_by = None
266+
default_paginate_by = 100
267+
268+
def get_paginate_by(self, queryset):
269+
"""
270+
Determine the number of items per page.
271+
272+
Resolution order:
273+
1. ``paginate_by`` set directly on the view instance.
274+
2. Per-model value from the ``DEJACODE_PAGINATE_BY`` setting.
275+
3. ``default_paginate_by`` as a fallback.
276+
"""
277+
if self.paginate_by:
278+
return self.paginate_by
279+
280+
if self.model:
281+
model_name = self.model._meta.model_name
282+
if paginate_by := settings.DEJACODE_PAGINATE_BY.get(model_name):
283+
return paginate_by
284+
285+
return self.default_paginate_by
265286

266287
def get_previous_next(self, page_obj):
267288
"""Return url links for the previous and next navigation."""
@@ -330,12 +351,11 @@ class DataspacedFilterView(
330351
GetDataspaceMixin,
331352
HasPermissionMixin,
332353
TableHeaderMixin,
333-
PreviousNextPaginationMixin,
354+
PaginationMixin,
334355
FilterView,
335356
):
336357
template_name = "object_list_base.html"
337358
template_list_table = None
338-
paginate_by = settings.PAGINATE_BY or 100
339359
# Required if `show_previous_and_next_object_links` enabled on the
340360
# details view.
341361
put_results_in_session = False
@@ -2246,7 +2266,7 @@ def page(self, number):
22462266

22472267

22482268
class APIWrapperListView(
2249-
PreviousNextPaginationMixin,
2269+
PaginationMixin,
22502270
ListView,
22512271
):
22522272
paginate_by = 100

product_portfolio/views.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
from dje.views import Header
100100
from dje.views import LicenseDataForBuilderMixin
101101
from dje.views import ObjectDetailsView
102-
from dje.views import PreviousNextPaginationMixin
102+
from dje.views import PaginationMixin
103103
from dje.views import SendAboutFilesView
104104
from dje.views import TabContentView
105105
from dje.views import TabField
@@ -164,7 +164,6 @@ class ProductListView(
164164
filterset_class = ProductFilterSet
165165
template_name = "product_portfolio/product_list.html"
166166
template_list_table = "product_portfolio/tables/product_list_table.html"
167-
paginate_by = 50
168167
put_results_in_session = False
169168
group_name_version = True
170169
table_headers = (
@@ -733,7 +732,7 @@ def get_context_data(self, **kwargs):
733732
class ProductTabInventoryView(
734733
LoginRequiredMixin,
735734
BaseProductViewMixin,
736-
PreviousNextPaginationMixin,
735+
PaginationMixin,
737736
TabContentView,
738737
):
739738
template_name = "product_portfolio/tabs/tab_inventory.html"
@@ -978,7 +977,7 @@ def inject_scan_data(scancodeio, feature_grouped, dataspace_uuid):
978977
class ProductTabCodebaseView(
979978
LoginRequiredMixin,
980979
BaseProductViewMixin,
981-
PreviousNextPaginationMixin,
980+
PaginationMixin,
982981
TabContentView,
983982
):
984983
template_name = "product_portfolio/tabs/tab_codebase.html"
@@ -1059,7 +1058,7 @@ def has_any_values(field_name):
10591058
class ProductTabDependenciesView(
10601059
LoginRequiredMixin,
10611060
BaseProductViewMixin,
1062-
PreviousNextPaginationMixin,
1061+
PaginationMixin,
10631062
TableHeaderMixin,
10641063
TabContentView,
10651064
):
@@ -1138,7 +1137,7 @@ def get_context_data(self, **kwargs):
11381137
class ProductTabVulnerabilitiesView(
11391138
LoginRequiredMixin,
11401139
BaseProductViewMixin,
1141-
PreviousNextPaginationMixin,
1140+
PaginationMixin,
11421141
TableHeaderMixin,
11431142
TabContentView,
11441143
):

reporting/views.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from dje.views import DataspacedFilterView
3535
from dje.views import DownloadableMixin
3636
from dje.views import HasPermissionMixin
37-
from dje.views import PreviousNextPaginationMixin
37+
from dje.views import PaginationMixin
3838
from reporting.filters import ReportFilterSet
3939
from reporting.forms import RuntimeFilterBaseFormSet
4040
from reporting.forms import RuntimeFilterForm
@@ -81,7 +81,7 @@ def get(self, request, *args, **kwargs):
8181

8282
class ReportDetailsView(
8383
LoginRequiredMixin,
84-
PreviousNextPaginationMixin,
84+
PaginationMixin,
8585
BootstrapCSSMixin,
8686
DownloadableMixin,
8787
HasPermissionMixin,
@@ -294,7 +294,6 @@ class ReportListView(
294294
filterset_class = ReportFilterSet
295295
template_name = "reporting/report_list.html"
296296
template_list_table = "reporting/includes/report_list_table.html"
297-
paginate_by = 50
298297

299298
def get_queryset(self):
300299
qs = super().get_queryset()

workflow/views.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class RequestListView(
4545
filterset_class = RequestFilterSet
4646
template_name = "workflow/request_list.html"
4747
template_list_table = "workflow/includes/request_list_table.html"
48-
paginate_by = 50
4948

5049
def get_queryset(self):
5150
"""

0 commit comments

Comments
 (0)