Hi,

This time I have summoned up some ideas from web and from this group.
I use memcache to store bookmarks across page requests, this is idea
of someone else whom I cannot remember. I also stole:) the idea of
random paginating from appengine's official pagination docs.

I am very new to appengine and also web development so please comment
me on any problem with the code:

Usage:
paginate with property:

        query = scComment.all().filter("game = ", game)
        pgt = scPaginator(query, PAGESIZE,
order_property="create_date")
        try:
            comments = pgt.get_page(next_key)
        except Exception, e:
            logging.error("Exception occurred while retrieving
comments.["+str(e)+"]")

paginate without property: (defaults to __key__ ordering)

        query = scComment.all().filter("game = ", game)
        pgt = scPaginator(query, PAGESIZE)
        try:
            comments = pgt.get_page(next_key)
        except Exception, e:
            logging.error("Exception occurred while retrieving
comments.["+str(e)+"]")



here is my class:

class scPaginator:
    def __init__(self, query, size, property_name=None):
        self.__nextkey = None
        self.__query = query # todo cannot be None
        self.__pagesize = size
        self.__propname = property_name
        if not self.__propname:
            self.__propname = "__key__" # default to __key__ ordering
then.


    def get_next_key(self):
        return self.__nextkey

    """
    Gets the next self.__size entries from db starting from start_key
    """
    def get_page(self, start_key = None, ascending=True):

        entities = None

        # initialize order and filter strings
        if ascending:
            filter_str = self.__propname + " <= "
            order_str = "-" + self.__propname
        else:
            filter_str = self.__propname + " >= "
            order_str = self.__propname


        # first time?
        if not start_key:
            entities =
self.__query.order(order_str).fetch(self.__pagesize+1)
        else:
            # requesting random order?
            if self.__propname == "__key__":
                start_val = Key(start_key)
            else:
                # retrieve the entity from the passed start_key for
the filter function
                # of the query object. first try memcache, then get
from db.
                start_entity = memcache.get(start_key)
                if not start_entity:
                   start_entity = db.get(start_key) # can throw
BadIndex
                start_val = getattr(start_entity, self.__propname) #
can throw AttibuteError

            entities =
self.__query.order(order_str).filter(filter_str,
start_val).fetch(self.__pagesize+1)

        # still has next page?
        if len(entities) == self.__pagesize+1:
            last_entity = entities[-1]
            self.__nextkey = last_entity.key()

            # save the entity to memcache, because we probably will
retrieve it
            # in the subsequent next page request. As many clients
will paginate the same
            # offset again and again, choose a big expire time.
            memcache.add(str(self.__nextkey), last_entity, 86400) #
time = 3600 * 24

            del entities[-1]

        return entities

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en.

Reply via email to