Skip to content

Instantly share code, notes, and snippets.

@febuiles
Created May 20, 2009 03:00
Show Gist options
  • Save febuiles/114584 to your computer and use it in GitHub Desktop.
Save febuiles/114584 to your computer and use it in GitHub Desktop.
def get_page(cls, app_id, bookmark=None):
pagesize = 15
next, more, less = None, None, None
if not bookmark: # first fetch
transactions = Transaction.app_id(app_id).order("-date").fetch(pagesize + 1)
more = True if len(transactions) == pagesize+1 else None
else:
if bookmark.startswith("-"): # we're moving backwards
date = str_to_date(bookmark[1:])
transactions = Transaction.app_id(app_id).order("date").filter("date >", date).fetch(pagesize+1)
more = True
less = True if len(transactions) == pagesize+1 else None
else:
date = str_to_date(bookmark)
transactions = Transaction.app_id(app_id).order("-date").filter('date <=', date).fetch(pagesize+1)
less = True # since we're moving forward it's safe to point backwards.
more = True if len(transactions) == pagesize+1 else None
if len(transactions) == pagesize + 1: # we have one more page to show
next = transactions[-1].date
transactions = transactions[:pagesize]
return transactions, next, more, less
get_page = classmethod(get_page)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment