diff --git a/pagure/api/group.py b/pagure/api/group.py index a5e7ba1..d521cee 100644 --- a/pagure/api/group.py +++ b/pagure/api/group.py @@ -16,7 +16,8 @@ import flask import pagure import pagure.exceptions import pagure.lib -from pagure.api import API, APIERROR, api_method, api_login_optional +from pagure.api import ( + API, APIERROR, api_method, api_login_optional, get_page, get_per_page) from pagure.utils import is_true @@ -43,6 +44,14 @@ def api_groups(): | | | | letters of the group | | | | | names | +---------------+----------+---------------+--------------------------+ + | ``page`` | int | Optional | | Specifies which | + | | | | page to return | + | | | | (defaults to: 1) | + +---------------+----------+---------------+--------------------------+ + | ``per_page`` | int | Optional | | The number of projects | + | | | | to return per page. | + | | | | The maximum is 100. | + +---------------+----------+---------------+--------------------------+ Sample response ^^^^^^^^^^^^^^^ @@ -51,17 +60,37 @@ def api_groups(): { "total_groups": 2, + u'pagination': { + 'first': 'http://localhost/api/0/groups?per_page=20&extended=1&page=1', + 'last': 'http://localhost/api/0/groups?per_page=20&extended=1&page=1', + 'next': None, + 'page': 1, + 'pages': 1, + 'per_page': 20, + 'prev': None + }, "groups": ["group1", "group2"] } - ''' + ''' # noqa pattern = flask.request.args.get('pattern', None) extended = is_true(flask.request.args.get('extended', False)) if pattern is not None and not pattern.endswith('*'): pattern += '*' - groups = pagure.lib.search_groups(flask.g.session, pattern=pattern) + page = get_page() + per_page = get_per_page() + group_cnt = pagure.lib.search_groups( + flask.g.session, pattern=pattern, count=True) + pagination_metadata = pagure.lib.get_pagination_metadata( + flask.request, page, per_page, group_cnt) + query_start = (page - 1) * per_page + query_limit = per_page + + groups = pagure.lib.search_groups( + flask.g.session, pattern=pattern, + limit=query_limit, offset=query_start) if extended: groups = [ @@ -76,8 +105,9 @@ def api_groups(): return flask.jsonify( { - 'total_groups': len(groups), - 'groups': groups + 'total_groups': group_cnt, + 'groups': groups, + 'pagination': pagination_metadata, } ) diff --git a/tests/test_pagure_flask_api.py b/tests/test_pagure_flask_api.py index b05729a..b8afdc3 100644 --- a/tests/test_pagure_flask_api.py +++ b/tests/test_pagure_flask_api.py @@ -170,14 +170,18 @@ class PagureFlaskApitests(tests.SimplePagureTest): self.assertEqual(output.status_code, 200) data = json.loads(output.get_data(as_text=True)) self.assertEqual(data['groups'], ['group1', 'rel-eng']) - self.assertEqual(sorted(data.keys()), ['groups', 'total_groups']) + self.assertEqual( + sorted(data.keys()), + ['groups', 'pagination', 'total_groups']) self.assertEqual(data['total_groups'], 2) output = self.app.get('/api/0/groups?pattern=re') self.assertEqual(output.status_code, 200) data = json.loads(output.get_data(as_text=True)) self.assertEqual(data['groups'], ['rel-eng']) - self.assertEqual(sorted(data.keys()), ['groups', 'total_groups']) + self.assertEqual( + sorted(data.keys()), + ['groups', 'pagination', 'total_groups']) self.assertEqual(data['total_groups'], 1) def test_api_whoami_unauth(self): diff --git a/tests/test_pagure_flask_api_group.py b/tests/test_pagure_flask_api_group.py index bd3a117..e89385c 100644 --- a/tests/test_pagure_flask_api_group.py +++ b/tests/test_pagure_flask_api_group.py @@ -85,14 +85,18 @@ class PagureFlaskApiGroupTests(tests.SimplePagureTest): self.assertEqual(output.status_code, 200) data = json.loads(output.get_data(as_text=True)) self.assertEqual(data['groups'], ['some_group', 'group1', 'rel-eng']) - self.assertEqual(sorted(data.keys()), ['groups', 'total_groups']) + self.assertEqual( + sorted(data.keys()), + ['groups', 'pagination', 'total_groups']) self.assertEqual(data['total_groups'], 3) output = self.app.get('/api/0/groups?pattern=re') self.assertEqual(output.status_code, 200) data = json.loads(output.get_data(as_text=True)) self.assertEqual(data['groups'], ['rel-eng']) - self.assertEqual(sorted(data.keys()), ['groups', 'total_groups']) + self.assertEqual( + sorted(data.keys()), + ['groups', 'pagination', 'total_groups']) self.assertEqual(data['total_groups'], 1) def test_api_groups_extended(self): @@ -136,6 +140,17 @@ class PagureFlaskApiGroupTests(tests.SimplePagureTest): "name": "rel-eng" } ], + u'pagination': { + u'first': u'http://localhost/api/0/groups?' + 'per_page=20&extended=1&page=1', + u'last': u'http://localhost/api/0/groups?' + 'per_page=20&extended=1&page=1', + u'next': None, + u'page': 1, + u'pages': 1, + u'per_page': 20, + u'prev': None + }, "total_groups": 3 } )