U S��W � @ s� d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z d dlm Z d d lmZ d d lm Z G dd� de�Zd d� Zddd�Zdd� Zddd�ZG dd� de�ZdS )� N)�abort)�render_template)�request)�Database)�DoesNotExist)�Model)�Proxy)�SelectQuery)�connectc @ s. e Zd Zddd�Zdd� Zdd� Zd d � ZdS ) �PaginatedQuery�pageFc C sD || _ || _|| _t|t�r.|| _| jj| _n|| _| j�� | _d S �N) �paginate_by�page_var�check_bounds� isinstancer �queryZmodel_classZmodel�select)�self�query_or_modelr r r � r �/flask_utils.py�__init__ s zPaginatedQuery.__init__c C s, t j�| j�}|r(|�� r(tdt|��S dS )N� )r �args�getr �isdigit�max�int)r Z curr_pager r r �get_page s zPaginatedQuery.get_pagec C s t t�t| j�� �| j ��S r )r �mathZceil�floatr �countr �r r r r �get_page_count# s zPaginatedQuery.get_page_countc C s2 | j r| �� | �� krtd� | j�| �� | j�S �Ni� )r r r$ r r Zpaginater r# r r r �get_object_list&