Skip to content

Instantly share code, notes, and snippets.

@bootandy
Created November 15, 2011 11:48
Show Gist options
  • Save bootandy/1366886 to your computer and use it in GitHub Desktop.
Save bootandy/1366886 to your computer and use it in GitHub Desktop.
GAE Python Data Loader that handles Next & Previous correctly
class LoadHelper:
PAGE_SIZE = 10
""" load objects, ordered by a parameter
supports next & previous"""
def loadByString(self, request, order, filter):
next = request.get('next')
prev = request.get('prev')
query = MyObject.all()
if prev:
query.order("-"+order)
else:
query.order(order)
if filter:
query.filter(filter,True)
if next:
query.filter(order + " >= " , next)
elif prev:
query.filter(order + " <= " , prev)
list = query.fetch(LoadHelper.PAGE_SIZE )
if prev:
list.reverse()
return list
class ObjectHandler(webapp.RequestHandler):
url = '/web/something'
def get(self):
list = LoadHelper().loadByString(self.request, 'name', 'online')
next=''
prev=''
if list[0]:
prev = list[0].name
next = list[-1].name
path = os.path.join(os.path.dirname(__file__), 'template.html')
self.response.out.write(template.render(path, dict() ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment