Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit38d4467

Browse files
author
Gauvain Pocentek
committed
Pagination generators: expose more information
Expose the X-* pagination attributes returned by the Gitlab server whenrequesting lists.Closes#304
1 parentfba7730 commit38d4467

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

‎docs/api-usage.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,15 @@ handle the next calls to the API when required:
225225
for itemin items:
226226
print(item.attributes)
227227
228+
The generator exposes extra listing information as received by the server:
229+
230+
* ``current_page``: current page number (first page is 1)
231+
* ``prev_page``: if ``None`` the current page is the first one
232+
* ``next_page``: if ``None`` the current page is the last one
233+
* ``per_page``: number of items per page
234+
* ``total_pages``: total number of pages available
235+
* ``total``: total number of items in the list
236+
228237
Sudo
229238
====
230239

‎gitlab/__init__.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ def _query(self, url, query_data={}, **kwargs):
864864
exceptKeyError:
865865
self._next_url=None
866866
self._current_page=result.headers.get('X-Page')
867+
self._prev_page=result.headers.get('X-Prev-Page')
867868
self._next_page=result.headers.get('X-Next-Page')
868869
self._per_page=result.headers.get('X-Per-Page')
869870
self._total_pages=result.headers.get('X-Total-Pages')
@@ -877,6 +878,42 @@ def _query(self, url, query_data={}, **kwargs):
877878

878879
self._current=0
879880

881+
@property
882+
defcurrent_page(self):
883+
"""The current page number."""
884+
returnint(self._current_page)
885+
886+
@property
887+
defprev_page(self):
888+
"""The next page number.
889+
890+
If None, the current page is the last.
891+
"""
892+
returnint(self._prev_page)ifself._prev_pageelseNone
893+
894+
@property
895+
defnext_page(self):
896+
"""The next page number.
897+
898+
If None, the current page is the last.
899+
"""
900+
returnint(self._next_page)ifself._next_pageelseNone
901+
902+
@property
903+
defper_page(self):
904+
"""The number of items per page."""
905+
returnint(self._per_page)
906+
907+
@property
908+
deftotal_pages(self):
909+
"""The total number of pages."""
910+
returnint(self._total_pages)
911+
912+
@property
913+
deftotal(self):
914+
"""The total number of items."""
915+
returnint(self._total)
916+
880917
def__iter__(self):
881918
returnself
882919

‎gitlab/base.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,42 @@ def next(self):
670670
data=self._list.next()
671671
returnself._obj_cls(self.manager,data)
672672

673+
@property
674+
defcurrent_page(self):
675+
"""The current page number."""
676+
returnself._list.current_page
677+
678+
@property
679+
defprev_page(self):
680+
"""The next page number.
681+
682+
If None, the current page is the last.
683+
"""
684+
returnself._list.prev_page
685+
686+
@property
687+
defnext_page(self):
688+
"""The next page number.
689+
690+
If None, the current page is the last.
691+
"""
692+
returnself._list.next_page
693+
694+
@property
695+
defper_page(self):
696+
"""The number of items per page."""
697+
returnself._list.per_page
698+
699+
@property
700+
deftotal_pages(self):
701+
"""The total number of pages."""
702+
returnself._list.total_pages
703+
704+
@property
705+
deftotal(self):
706+
"""The total number of items."""
707+
returnself._list.total
708+
673709

674710
classRESTManager(object):
675711
"""Base class for CRUD operations on objects.

‎gitlab/tests/test_gitlab.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ def resp_2(url, request):
209209
self.assertEqual(len(obj),2)
210210
self.assertEqual(obj._next_url,
211211
'http://localhost/api/v4/tests?per_page=1&page=2')
212+
self.assertEqual(obj.current_page,1)
213+
self.assertEqual(obj.prev_page,None)
214+
self.assertEqual(obj.next_page,2)
215+
self.assertEqual(obj.per_page,1)
216+
self.assertEqual(obj.total_pages,2)
217+
self.assertEqual(obj.total,2)
212218

213219
withHTTMock(resp_2):
214220
l=list(obj)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp