def test_current_page_defaults_to_1(self, pyramid_request): """If there's no 'page' request param it defaults to 1.""" pyramid_request.params = {} page = paginate(pyramid_request, 600, 10) assert page['cur'] == 1
def test_paginate_includes_total(pyramid_request, total): query, view = fake_view(total=total) wrapped = paginate(view) result = wrapped(context=None, request=pyramid_request) assert result['total'] == total
def test_current_page_defaults_to_1(self, pyramid_request): """If there's no 'page' request param it defaults to 1.""" pyramid_request.params = {} page = paginate(pyramid_request, 600, 10) assert page["cur"] == 1
def test_paginate_includes_total(total): request = DummyRequest() query, view = fake_view(total=total) wrapped = paginate(view) result = wrapped(context=None, request=request) assert result['total'] == total
def test_url_for(self, pyramid_request, params, expected): pyramid_request.params = NestedMultiDict(*params) pyramid_request.current_route_path = mock.Mock(spec_set=["__call__"]) url_for = paginate(pyramid_request, 600, 10)["url_for"] url = url_for(page=26) # Request the URL for page 26. pyramid_request.current_route_path.assert_called_once_with(_query=expected) assert url == pyramid_request.current_route_path.return_value
def test_paginate_returns_resultset(pyramid_request): query, view = fake_view(total=10) wrapped = paginate(view) result = wrapped(context=None, request=pyramid_request) assert result['results'] == query # Assert that .all() has been called to turn the query into a list. assert query.all_called
def test_paginate_returns_offsets_and_limits_resultset(pyramid_request, total, param, offset): pyramid_request.params = {'page': param} query, view = fake_view(total) wrapped = paginate(view) result = wrapped(context=None, request=pyramid_request) assert result['results'].offset_param == offset assert result['results'].limit_param == 20
def test_current_page(self, pyramid_request, page_param, expected): pyramid_request.params = {'page': page_param} page = paginate( pyramid_request, # With 600 items in total and 10 items per page there are 60 pages. 600, 10) assert page['cur'] == expected
def test_paginate_params(pyramid_request): pyramid_request.params = {'page': 2} query, view = fake_view(20) decorator = paginate(page_size=5) wrapped = decorator(view) result = wrapped(context=None, request=pyramid_request) assert result['results'].offset_param == 5 assert result['results'].limit_param == 5
def search(self): # Make a copy of the query params to be consumed by search. query_params = self.parsed_query_params.copy() # Check whether a redirect is required. query.check_url(self.request, query_params) page_size = self.request.params.get("page_size", PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results. results = query.execute(self.request, query_params, page_size=page_size) groups_suggestions = [] if self.request.user: for group in self.request.user.groups: groups_suggestions.append({ "name": group.name, "pubid": group.pubid }) def tag_link(tag): tag = parser.unparse({"tag": tag}) return self.request.route_url("activity.search", _query=[("q", tag)]) def username_from_id(userid): parts = split_user(userid) return parts["username"] def user_link(userid): username = username_from_id(userid) return self.request.route_url("activity.user_search", username=username) return { "search_results": results, "groups_suggestions": groups_suggestions, "page": paginate(self.request, results.total, page_size=page_size), "pretty_link": pretty_link, "q": self.request.params.get("q", ""), "tag_link": tag_link, "user_link": user_link, "username_from_id": username_from_id, # The message that is shown (only) if there's no search results. "zero_message": _("No annotations matched your search."), }
def search(request): if not request.feature('search_page'): raise httpexceptions.HTTPNotFound() q = query.extract(request) # Check whether a redirect is required query.check_url(request, q) page_size = request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results result = query.execute(request, q, page_size=page_size) groups_suggestions = [] if request.authenticated_user: for group in request.authenticated_user.groups: groups_suggestions.append({ 'name': group.name, 'pubid': group.pubid }) def tag_link(tag): q = parser.unparse({'tag': tag}) return request.route_url('activity.search', _query=[('q', q)]) def username_from_id(userid): parts = split_user(userid) return parts['username'] def user_link(userid): username=username_from_id(userid) return request.route_url('activity.user_search', username=username) return { 'aggregations': result.aggregations, 'groups_suggestions': groups_suggestions, 'page': paginate(request, result.total, page_size=page_size), 'pretty_link': pretty_link, 'q': request.params.get('q', ''), 'tag_link': tag_link, 'timeframes': result.timeframes, 'total': result.total, 'user_link': user_link, 'username_from_id': username_from_id, }
def test_paginate_returns_page_metadata(pyramid_request, total, param, meta): pyramid_request.params = {'page': param} query, view = fake_view(total) ecur, emax, enext, eprev = meta wrapped = paginate(view) result = wrapped(context=None, request=pyramid_request) assert result['page'] == { 'cur': ecur, 'max': emax, 'next': enext, 'prev': eprev, }
def search(self): # Make a copy of the query params to be consumed by search. q = self.parsed_query_params.copy() # Check whether a redirect is required. query.check_url(self.request, q) page_size = self.request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results. results = query.execute(self.request, q, page_size=page_size) groups_suggestions = [] if self.request.user: for group in self.request.user.groups: groups_suggestions.append({ 'name': group.name, 'pubid': group.pubid }) def tag_link(tag): q = parser.unparse({'tag': tag}) return self.request.route_url('activity.search', _query=[('q', q)]) def username_from_id(userid): parts = split_user(userid) return parts['username'] def user_link(userid): username = username_from_id(userid) return self.request.route_url('activity.user_search', username=username) return { 'search_results': results, 'groups_suggestions': groups_suggestions, 'page': paginate(self.request, results.total, page_size=page_size), 'pretty_link': pretty_link, 'q': self.request.params.get('q', ''), 'tag_link': tag_link, 'user_link': user_link, 'username_from_id': username_from_id, # The message that is shown (only) if there's no search results. 'zero_message': _('No annotations matched your search.'), }
def search(self): q = query.extract(self.request) # Check whether a redirect is required. query.check_url(self.request, q) page_size = self.request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results. results = query.execute(self.request, q, page_size=page_size) groups_suggestions = [] if self.request.user: for group in self.request.user.groups: groups_suggestions.append({ 'name': group.name, 'pubid': group.pubid }) def tag_link(tag): q = parser.unparse({'tag': tag}) return self.request.route_url('activity.search', _query=[('q', q)]) def username_from_id(userid): parts = split_user(userid) return parts['username'] def user_link(userid): username = username_from_id(userid) return self.request.route_url('activity.user_search', username=username) return { 'search_results': results, 'groups_suggestions': groups_suggestions, 'page': paginate(self.request, results.total, page_size=page_size), 'pretty_link': pretty_link, 'q': self.request.params.get('q', ''), 'tag_link': tag_link, 'user_link': user_link, 'username_from_id': username_from_id, # The message that is shown (only) if there's no search results. 'zero_message': _('No annotations matched your search.'), }
def search(self): # Make a copy of the query params to be consumed by search. q = self.parsed_query_params.copy() # Check whether a redirect is required. query.check_url(self.request, q) page_size = self.request.params.get("page_size", PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results. results = query.execute(self.request, q, page_size=page_size) groups_suggestions = [] if self.request.user: for group in self.request.user.groups: groups_suggestions.append({"name": group.name, "pubid": group.pubid}) def tag_link(tag): q = parser.unparse({"tag": tag}) return self.request.route_url("activity.search", _query=[("q", q)]) def username_from_id(userid): parts = split_user(userid) return parts["username"] def user_link(userid): username = username_from_id(userid) return self.request.route_url("activity.user_search", username=username) return { "search_results": results, "groups_suggestions": groups_suggestions, "page": paginate(self.request, results.total, page_size=page_size), "pretty_link": pretty_link, "q": self.request.params.get("q", ""), "tag_link": tag_link, "user_link": user_link, "username_from_id": username_from_id, # The message that is shown (only) if there's no search results. "zero_message": _("No annotations matched your search."), }
def search(request): if not request.feature('search_page'): raise httpexceptions.HTTPNotFound() q = query.extract(request) # Check whether a redirect is required query.check_url(request, q) # Fetch results result = query.execute(request, q, page_size=PAGE_SIZE) return { 'total': result.total, 'aggregations': result.aggregations, 'timeframes': result.timeframes, 'page': paginate(request, result.total, page_size=PAGE_SIZE), }
def search(request): if not request.feature('search_page'): raise httpexceptions.HTTPNotFound() q = query.extract(request) if q is None: return {} # Check whether a redirect is required query.check_url(request, q) # Fetch results result = query.execute(request, q) return { 'q': request.params['q'], 'total': result.total, 'aggregations': result.aggregations, 'timeframes': result.timeframes, 'page': paginate(request, result.total), }
def search(request): if not request.feature('search_page'): raise httpexceptions.HTTPNotFound() q = query.extract(request) # Check whether a redirect is required query.check_url(request, q) page_size = request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results result = query.execute(request, q, page_size=page_size) for user in result.aggregations.get('users', []): user['username'] = util.user.split_user(user['user'])['username'] user['userid'] = user['user'] user['faceted_by'] = _faceted_by_user(request, user['username'], q) del user['user'] groups_suggestions = [] if request.authenticated_user: for group in request.authenticated_user.groups: groups_suggestions.append({ 'name': group.name, 'pubid': group.pubid }) return { 'total': result.total, 'aggregations': result.aggregations, 'groups_suggestions': groups_suggestions, 'timeframes': result.timeframes, 'page': paginate(request, result.total, page_size=page_size), }
def search(request): if not request.feature('search_page'): raise httpexceptions.HTTPNotFound() q = query.extract(request) # Check whether a redirect is required query.check_url(request, q) page_size = request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results result = query.execute(request, q, page_size=page_size) return { 'total': result.total, 'aggregations': result.aggregations, 'timeframes': result.timeframes, 'page': paginate(request, result.total, page_size=page_size), }
def test_prev(self, pyramid_request, page_param, expected): pyramid_request.params = {'page': page_param} assert paginate(pyramid_request, 600, 10)['prev'] == expected
def test_numbers_small_result_set(self, pyramid_request, page_param, expected): pyramid_request.params = {"page": page_param} assert paginate(pyramid_request, 50, 10)["numbers"] == expected
def test_prev(self, pyramid_request, page_param, expected): pyramid_request.params = {"page": page_param} assert paginate(pyramid_request, 600, 10)["prev"] == expected
def test_numbers_large_result_set(self, pyramid_request, page_param, expected): pyramid_request.params = {"page": page_param} assert paginate(pyramid_request, 600, 10)["numbers"] == expected
def test_numbers_small_result_set(self, pyramid_request, page_param, expected): pyramid_request.params = {'page': page_param} assert paginate(pyramid_request, 50, 10)['numbers'] == expected
def test_numbers_large_result_set(self, pyramid_request, page_param, expected): pyramid_request.params = {'page': page_param} assert paginate(pyramid_request, 600, 10)['numbers'] == expected
def test_max(self, pyramid_request, total, page_size, expected): assert paginate(pyramid_request, total, page_size)['max'] == expected
def test_max(self, pyramid_request, total, page_size, expected): assert paginate(pyramid_request, total, page_size)["max"] == expected
def search(self): # If the user is just logged-in in 10 seconds, create JWT grant tokens # for each JWT clients. user = self.request.user grant_tokens = [] if user is not None and \ datetime.utcnow() < user.last_login_date + timedelta(seconds=10): clients = self.request.db.query(AuthClient) \ .order_by(AuthClient.name.asc()) \ .all() for client in clients: if client.secret is not None: grant_tokens.append( self._create_grant_token(user.username, client)) q = query.extract(self.request) # Check whether a redirect is required. query.check_url(self.request, q) page_size = self.request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results. results = query.execute(self.request, q, page_size=page_size) groups_suggestions = [] if self.request.user: for group in self.request.user.groups: groups_suggestions.append({ 'name': group.name, 'pubid': group.pubid }) def tag_link(tag): q = parser.unparse({'tag': tag}) return self.request.route_url('activity.search', _query=[('q', q)]) def username_from_id(userid): parts = split_user(userid) return parts['username'] def user_link(userid): username = username_from_id(userid) return self.request.route_url('activity.user_search', username=username) return { 'search_results': results, 'groups_suggestions': groups_suggestions, 'page': paginate(self.request, results.total, page_size=page_size), 'pretty_link': pretty_link, 'q': self.request.params.get('q', ''), 'tag_link': tag_link, 'user_link': user_link, 'username_from_id': username_from_id, # The message that is shown (only) if there's no search results. 'zero_message': _('No annotations matched your search.'), 'grant_tokens': enumerate(grant_tokens), }