"""Utils for make ready flask response values.""" from ows_accounting.constants import pagination def add_pagination(items, limit=0, offset=0, pagination_type=pagination.TYPE_NONE): """Add pagination to the list of items objects. Paginate items if necessary depend on pagination_type. Args: items (list): list of items objects. limit (int): limit number of result records. offset (int): offset start of result records. pagination_type (string): all variants are in pagination.TYPE_TO_RETURN_MAPPING Returns: response: Paginated list of objects. """ total_records = len(items) return_pagination_type = pagination.TYPE_TO_RETURN_MAPPING[ pagination_type] pagination_dict = { 'items': items, 'pagination': { 'type': return_pagination_type, 'offset': offset, 'limit': limit, 'total_records': total_records } } if pagination_type == pagination.TYPE_STANDARD_WITH_CALCULATION: pagination_dict['items'] = items[ offset:(limit + offset if limit is not None else None)] if pagination_type == pagination.TYPE_NONE: pagination_dict['pagination'] = { 'type': return_pagination_type, 'total_records': total_records } return pagination_dict