Skip to content

Commit 97eff50

Browse files
pushpit kambojpushpit kamboj
authored andcommitted
[feature] Added OpenWispPagination class with suitable default values
1 parent 9f2ab62 commit 97eff50

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

docs/developer/other-utilities.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,77 @@ Every openwisp module which has an API should use this class to configure
8383
its own default settings, which will be merged with the settings of the
8484
other modules.
8585

86+
``openwisp_utils.api.pagination.OpenWispPagination``
87+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88+
89+
A reusable pagination class for DRF views that provides consistent
90+
pagination behavior across OpenWISP modules with sensible defaults and
91+
flexible configuration.
92+
93+
**Default Behavior:**
94+
95+
- 10 items per page
96+
- Clients can request custom page sizes via ``?page_size=N`` query
97+
parameter
98+
- Maximum 100 items per page to prevent performance issues
99+
- Standard page navigation via ``?page=N``
100+
101+
**Configuration via Django Settings:**
102+
103+
The following settings can be used to customize the default pagination
104+
behavior:
105+
106+
- ``OPENWISP_PAGINATION_PAGE_SIZE`` (default: ``10``): Default number of
107+
items per page
108+
- ``OPENWISP_PAGINATION_MAX_PAGE_SIZE`` (default: ``100``): Maximum
109+
allowed page size
110+
- ``OPENWISP_PAGINATION_PAGE_SIZE_QUERY_PARAM`` (default:
111+
``"page_size"``): Query parameter name for page size
112+
113+
**Usage in Views:**
114+
115+
.. code-block:: python
116+
117+
from openwisp_utils.api.pagination import OpenWispPagination
118+
from rest_framework.viewsets import ModelViewSet
119+
120+
121+
class DeviceViewSet(ModelViewSet):
122+
queryset = Device.objects.all()
123+
serializer_class = DeviceSerializer
124+
pagination_class = OpenWispPagination
125+
126+
**Custom Configuration via Subclassing:**
127+
128+
For view-specific pagination requirements, you can subclass
129+
``OpenWispPagination``:
130+
131+
.. code-block:: python
132+
133+
from openwisp_utils.api.pagination import OpenWispPagination
134+
135+
136+
class LargePagination(OpenWispPagination):
137+
page_size = 50
138+
max_page_size = 500
139+
140+
141+
class ReportViewSet(ModelViewSet):
142+
queryset = Report.objects.all()
143+
serializer_class = ReportSerializer
144+
pagination_class = LargePagination
145+
146+
**Global Configuration:**
147+
148+
To use ``OpenWispPagination`` as the default pagination class for all DRF
149+
views in your project, add it to ``REST_FRAMEWORK`` settings:
150+
151+
.. code-block:: python
152+
153+
REST_FRAMEWORK = {
154+
"DEFAULT_PAGINATION_CLASS": "openwisp_utils.api.pagination.OpenWispPagination",
155+
}
156+
86157
Storage Utilities
87158
-----------------
88159

openwisp_utils/api/pagination.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from django.conf import settings
2+
from django.core.exceptions import ImproperlyConfigured
3+
4+
try:
5+
from rest_framework.pagination import PageNumberPagination
6+
except ImportError: # pragma: nocover
7+
raise ImproperlyConfigured(
8+
"Django REST Framework is required to use "
9+
"this feature but it is not installed"
10+
)
11+
12+
13+
class OpenWispPagination(PageNumberPagination):
14+
"""Reusable pagination class with sensible defaults.
15+
16+
Configurable via Django settings: - OPENWISP_PAGINATION_PAGE_SIZE
17+
(default: 10) - OPENWISP_PAGINATION_MAX_PAGE_SIZE (default: 100) -
18+
OPENWISP_PAGINATION_PAGE_SIZE_QUERY_PARAM (default: 'page_size')
19+
"""
20+
21+
page_size = getattr(settings, "OPENWISP_PAGINATION_PAGE_SIZE", 10)
22+
max_page_size = getattr(settings, "OPENWISP_PAGINATION_MAX_PAGE_SIZE", 100)
23+
page_size_query_param = getattr(
24+
settings, "OPENWISP_PAGINATION_PAGE_SIZE_QUERY_PARAM", "page_size"
25+
)

0 commit comments

Comments
 (0)