Source

wdmmg / wdmmg / lib / mongo_page.py

Full commit
from pymongo.cursor import Cursor
from webhelpers import paginate
from webhelpers.paginate import get_wrapper as _get_wrapper


class _MongoCursorWrapper(object):
    def __init__(self, cur):
        self.cur = cur
        
    def __getitem__(self, r):
        cur = self.cur.clone()
        return list(cur.__getitem__(r))
        
    def __len__(self):
        return self.cur.clone().count()
    

def get_wrapper(obj, sqlalchemy_session=None):
    if isinstance(obj, Cursor):
        return _MongoCursorWrapper(obj)
    return _get_wrapper(obj, sqlalchemy_session=sqlalchemy_session)
    
paginate.get_wrapper = get_wrapper

class Page(paginate.Page):
    # Curry the pager method of the webhelpers.paginate.Page class, so we have
    # our custom layout set as default.
    def pager(self, *args, **kwargs):
        kwargs.update(
            format="<div class='pager'>$link_previous ~2~ $link_next</div>",
            symbol_previous=u'\xab Prev', symbol_next=u'Next \xbb'
        )
        return super(Page, self).pager(*args, **kwargs)