Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def encodebookmark(created, key, direction):
- from urllib import quote_plus
- timestamp = time.mktime(created.timetuple())+1e-6*created.microsecond
- return quote_plus(base64.b64encode("%f|%s|%s" % (timestamp, key, direction)))
- def decodebookmark(b64bookmark):
- from urllib import unquote_plus
- timestamp, key, direction = base64.b64decode(
- unquote_plus(b64bookmark)).split('|')
- created = datetime.datetime.fromtimestamp(float(timestamp))
- return created, key, direction
- class PagingModel:
- PAGESIZE = 10
- @staticmethod
- def page(bm=None):
- PAGESIZE = PagingModel.PAGESIZE
- prev, next = None, None
- if bm:
- modified, key, direction = decodebookmark(bm)
- key = db.Key(key)
- if direction == 'next':
- query = Issue().all()
- query.filter('modified =', modified)
- query.filter('__key__ >=', key)
- query.order('__key__')
- issues = query.fetch(PAGESIZE+1)
- if len(issues) < PAGESIZE+1:
- query = Issue().all()
- query.filter('modified >', modified)
- query.order('modified')
- query.order('__key__')
- needed = (PAGESIZE+1) - len(issues)
- issues.extend(query.fetch(needed))
- if len(issues) > PAGESIZE:
- next_modified = issues[-1].modified
- next_key = issues[-1].key()
- next = encodebookmark(next_modified, next_key, 'next')
- issues = issues[:-1]
- query = Issue().all()
- query.filter('__key__ <', key)
- query.order('-__key__')
- query.order('-modified')
- previssue = query.fetch(1)
- if previssue:
- prev_modified = previssue[0].modified
- prev_key = previssue[0].key()
- prev = encodebookmark(prev_modified, prev_key, 'prev')
- return (issues, next, prev)
- else: # if direction == 'next'
- query = Issue().all()
- query.filter('modified =', modified)
- query.filter('__key__ <=', key)
- query.order('-__key__')
- issues = query.fetch(PAGESIZE+1)
- issues.reverse()
- if len(issues) < PAGESIZE+1:
- query = Issue().all()
- query.filter('modified <', modified)
- query.order('-modified')
- query.order('-__key__')
- needed = (PAGESIZE+1) - len(issues)
- extras = query.fetch(needed)
- extras.reverse()
- extras.extend(issues)
- issues = extras
- if len(issues) > PAGESIZE:
- prev_modified = issues[0].modified
- prev_key = issues[0].key()
- prev = encodebookmark(prev_modified, prev_key, 'prev')
- issues = issues[1:]
- query = Issue().all()
- query.filter('__key__ >', key)
- query.order('__key__')
- query.order('modified')
- nextissue = query.fetch(1)
- if nextissue:
- next_modified = nextissue[0].modified
- next_key = nextissue[0].key()
- next = encodebookmark(next_modified, next_key, 'next')
- return (issues, next, prev)
- else:
- query = Issue().all()
- query.order('modified')
- query.order('__key__')
- issues = query.fetch(PAGESIZE+1)
- if len(issues) > PAGESIZE:
- modified = issues[-1].modified
- key = issues[-1].key()
- next = encodebookmark(modified, key, 'next')
- issues = issues[:-1]
- return (issues, next, None)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement