def test_last_page(self): page = 10 per_page = 10 total_count = 98 pagination = Pagination(page, per_page, total_count) self.assertTrue(pagination.has_prev) self.assertFalse(pagination.has_next) self.assertEqual(list(pagination.iter_pages()), [1, None, 9, 10])
def test_middle_page(self): page = 5 per_page = 10 total_count = 98 pagination = Pagination(page, per_page, total_count) self.assertTrue(pagination.has_prev) self.assertTrue(pagination.has_next) self.assertEqual(list(pagination.iter_pages()), [1, None, 4, 5, 6, 7, None, 10])
def load(page, per_page, sort_key): """Load objects for the table.""" # FIXME: Load tags in this way until wash_arguments handles lists. tags = request.args.getlist("tags[]") or [] # empty to show all sort_key = request.args.get( 'sort_key', session.get('holdingpen_sort_key', "modified") ) response = get_holdingpen_objects(tags, sort_key) current_app.logger.debug(tags) current_app.logger.debug(response) page = max(page, 1) per_page = per_page or session.get('holdingpen_per_page') or 10 pagination = Pagination(page, per_page, len(response)) # Make sure requested page is within limits. if pagination.page > pagination.pages: pagination.page = pagination.pages pages_iteration = [] for iter_page in pagination.iter_pages(): res = {"page": iter_page} if iter_page == pagination.page: res["active"] = True else: res["active"] = False pages_iteration.append(res) table_data = { 'rows': [], 'pagination': { "page": pagination.page, "pages": pagination.pages, "iter_pages": pages_iteration, "per_page": pagination.per_page, "total_count": pagination.total_count } } # Add current ids in table for use by previous/next session['holdingpen_current_ids'] = response session['holdingpen_sort_key'] = sort_key session['holdingpen_per_page'] = per_page session['holdingpen_tags'] = tags display_start = max(pagination.per_page*(pagination.page-1), 0) display_end = min( pagination.per_page*pagination.page, pagination.total_count ) table_data["rows"] = get_rows( response[display_start:display_end] ) table_data["rendered_rows"] = "".join(table_data["rows"]) return jsonify(table_data)
def load(page, per_page, sort_key): """Load objects for the table.""" # FIXME: Load tags in this way until wash_arguments handles lists. tags = request.args.getlist("tags[]") or [] # empty to show all sort_key = request.args.get( 'sort_key', session.get('holdingpen_sort_key', "updated") ) per_page = per_page or session.get('holdingpen_per_page') or 10 object_list = get_holdingpen_objects(tags) object_list = sort_bwolist(object_list, sort_key) page = max(page, 1) pagination = Pagination(page, per_page, len(object_list)) # Make sure requested page is within limits. if pagination.page > pagination.pages: pagination.page = pagination.pages pages_iteration = [] for iter_page in pagination.iter_pages(): res = {"page": iter_page} if iter_page == pagination.page: res["active"] = True else: res["active"] = False pages_iteration.append(res) table_data = { 'rows': [], 'pagination': { "page": pagination.page, "pages": pagination.pages, "iter_pages": pages_iteration, "per_page": pagination.per_page, "total_count": pagination.total_count } } # Add current ids in table for use by previous/next session['holdingpen_current_ids'] = [o.id for o in object_list] session['holdingpen_sort_key'] = sort_key session['holdingpen_per_page'] = per_page session['holdingpen_tags'] = tags display_start = max(pagination.per_page*(pagination.page-1), 0) display_end = min( pagination.per_page*pagination.page, pagination.total_count ) table_data["rows"] = get_rows(object_list[display_start:display_end]) table_data["rendered_rows"] = "".join(table_data["rows"]) return jsonify(table_data)