def paginate(query, page, per_page_num):
    """Paginate a query, returning also the total before pagination."""
    total = query.count()
    return query.offset(page * per_page_num).limit(per_page_num).all(), total
