Advertisement
Guest User

simo

a guest
Mar 17th, 2009
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.36 KB | None | 0 0
  1. def encodebookmark(created, key, direction):
  2.   from urllib import quote_plus
  3.   timestamp = time.mktime(created.timetuple())+1e-6*created.microsecond
  4.   return quote_plus(base64.b64encode("%f|%s|%s" % (timestamp, key, direction)))
  5.  
  6. def decodebookmark(b64bookmark):
  7.   from urllib import unquote_plus
  8.   timestamp, key, direction = base64.b64decode(
  9.     unquote_plus(b64bookmark)).split('|')
  10.   created = datetime.datetime.fromtimestamp(float(timestamp))
  11.   return created, key, direction
  12.  
  13. class PagingModel:
  14.   PAGESIZE = 10
  15.  
  16.   @staticmethod
  17.   def page(bm=None):
  18.     PAGESIZE = PagingModel.PAGESIZE
  19.     prev, next = None, None
  20.  
  21.     if bm:
  22.       modified, key, direction = decodebookmark(bm)
  23.       key = db.Key(key)
  24.       if direction == 'next':
  25.         query = Issue().all()
  26.         query.filter('modified =', modified)
  27.         query.filter('__key__ >=', key)
  28.         query.order('__key__')
  29.         issues = query.fetch(PAGESIZE+1)
  30.         if len(issues) < PAGESIZE+1:
  31.           query = Issue().all()
  32.           query.filter('modified >', modified)
  33.           query.order('modified')
  34.           query.order('__key__')
  35.           needed = (PAGESIZE+1) - len(issues)
  36.           issues.extend(query.fetch(needed))
  37.         if len(issues) > PAGESIZE:
  38.           next_modified = issues[-1].modified
  39.           next_key = issues[-1].key()
  40.           next = encodebookmark(next_modified, next_key, 'next')
  41.           issues = issues[:-1]
  42.  
  43.         query = Issue().all()
  44.         query.filter('__key__ <', key)
  45.         query.order('-__key__')
  46.         query.order('-modified')
  47.         previssue = query.fetch(1)
  48.         if previssue:
  49.           prev_modified = previssue[0].modified
  50.           prev_key = previssue[0].key()
  51.           prev = encodebookmark(prev_modified, prev_key, 'prev')
  52.         return (issues, next, prev)
  53.       else: # if direction == 'next'
  54.         query = Issue().all()
  55.         query.filter('modified =', modified)
  56.         query.filter('__key__ <=', key)
  57.         query.order('-__key__')
  58.         issues = query.fetch(PAGESIZE+1)
  59.         issues.reverse()
  60.         if len(issues) < PAGESIZE+1:
  61.           query = Issue().all()
  62.           query.filter('modified <', modified)
  63.           query.order('-modified')
  64.           query.order('-__key__')
  65.           needed = (PAGESIZE+1) - len(issues)
  66.           extras = query.fetch(needed)
  67.           extras.reverse()
  68.           extras.extend(issues)
  69.           issues = extras
  70.         if len(issues) > PAGESIZE:
  71.           prev_modified = issues[0].modified
  72.           prev_key = issues[0].key()
  73.           prev = encodebookmark(prev_modified, prev_key, 'prev')
  74.           issues = issues[1:]
  75.  
  76.         query = Issue().all()
  77.         query.filter('__key__ >', key)
  78.         query.order('__key__')
  79.         query.order('modified')
  80.         nextissue = query.fetch(1)
  81.         if nextissue:
  82.           next_modified = nextissue[0].modified
  83.           next_key = nextissue[0].key()
  84.           next = encodebookmark(next_modified, next_key, 'next')
  85.         return (issues, next, prev)
  86.     else:
  87.       query = Issue().all()
  88.       query.order('modified')
  89.       query.order('__key__')
  90.       issues = query.fetch(PAGESIZE+1)
  91.       if len(issues) > PAGESIZE:
  92.         modified = issues[-1].modified
  93.         key = issues[-1].key()
  94.         next = encodebookmark(modified, key, 'next')
  95.         issues = issues[:-1]
  96.       return (issues, next, None)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement