def validate_page_token(page_token: str) -> None: # INFO - G.M - 2020-07-23 - Because they lack an explicit message, we catch exceptions from # the unserialize_bookmark method of sqlakeyset and re-raise them with an explicit message. # See https://github.com/djrobstep/sqlakeyset/issues/34 try: unserialize_bookmark(page_token) except Exception as e: raise ValidationError( 'Page token "{}" is not a valid page token'.format( page_token)) from e
def parse_pagination_param(param_key): pagination_param = request.args.get(param_key, None) if not pagination_param: return False sanitized_param = filter_pagination_param(pagination_param) unserialized_pagination = unserialize_bookmark(sanitized_param) return unserialized_pagination
def check_paging(q=None, selectable=None, s=None): ITEM_COUNTS = range(1, 12) if q is not None: unpaged = q.all() elif selectable is not None: result = s.execute(selectable) unpaged = result.fetchall() for per_page in ITEM_COUNTS: for backwards in [False, True]: gathered = [] page = None, backwards while True: serialized_page = serialize_bookmark(page) page = unserialize_bookmark(serialized_page) if q is not None: method = get_page args = (q, ) elif selectable is not None: method = select_page args = (s, selectable) rows = method(*args, per_page=per_page, page=serialized_page) p = rows.paging assert p.current == page if selectable is not None: assert rows.keys() == result.keys() if backwards: gathered = rows + gathered else: gathered = gathered + rows page = p.further if not rows: assert not p.has_further assert p.further == p.current assert p.current_opposite == (None, not p.backwards) break assert gathered == unpaged
def parse_pagination_param(param_key) -> Tuple[Optional[List[Any]], bool]: pagination_param = request.args.get(param_key, None) if not pagination_param: return None, False sanitized_param = filter_pagination_param(pagination_param) try: unserialized_pagination = unserialize_bookmark(sanitized_param) return unserialized_pagination except dateutil.parser.ParserError as err: # type: ignore[attr-defined] log.warning("parsing date field of %r failed: %s", sanitized_param, err) except ValueError as err: log.warning( "unserializing pagination bookmark %r failed: %s", sanitized_param, err ) return None, False
def check_paging_orm(q): item_counts = range(1, 12) unpaged = q.all() for backwards in [False, True]: for per_page in item_counts: gathered = [] page = None, backwards while True: serialized_page = serialize_bookmark(page) page = unserialize_bookmark(serialized_page) page_with_paging = get_page(q, per_page=per_page, page=serialized_page) paging = page_with_paging.paging assert paging.current == page if backwards: gathered = page_with_paging + gathered else: gathered = gathered + page_with_paging page = paging.further if len(gathered) < len(unpaged): # Ensure each page is the correct size assert paging.has_further assert len(page_with_paging) == per_page else: assert not paging.has_further if not page_with_paging: assert not paging.has_further assert paging.further == paging.current assert paging.current_opposite == (None, not paging.backwards) break # Ensure union of pages is original q.all() assert gathered == unpaged
def check_paging_core(selectable, s): item_counts = range(1, 12) result = s.execute(selectable) unpaged = result.fetchall() for backwards in [False, True]: for per_page in item_counts: gathered = [] page = None, backwards while True: serialized_page = serialize_bookmark(page) page = unserialize_bookmark(serialized_page) page_with_paging = select_page(s, selectable, per_page=per_page, page=serialized_page) paging = page_with_paging.paging assert paging.current == page assert page_with_paging.keys() == result.keys() if backwards: gathered = page_with_paging + gathered else: gathered = gathered + page_with_paging page = paging.further if not page_with_paging: assert not paging.has_further assert paging.further == paging.current assert paging.current_opposite == (None, not paging.backwards) break assert gathered == unpaged
def check_paging_orm(q): item_counts = range(1, 12) unpaged = q.all() for backwards in [False, True]: for per_page in item_counts: gathered = [] page = None, backwards while True: serialized_page = serialize_bookmark(page) page = unserialize_bookmark(serialized_page) page_with_paging = get_page(q, per_page=per_page, page=serialized_page) paging = page_with_paging.paging assert paging.current == page if backwards: gathered = page_with_paging + gathered else: gathered = gathered + page_with_paging page = paging.further if not page_with_paging: assert not paging.has_further assert paging.further == paging.current assert paging.current_opposite == (None, not paging.backwards) break assert gathered == unpaged
def twoway(x): before = x ss = serialize_bookmark(x) after = unserialize_bookmark(ss) return before == after