Пример #1
0
def extract_query_params(request):
    """Validator to extract BSO search parameters from the query string.

    This validator will extract and validate the following search params:

        * older, newer:  bounds on last-modified version (integer)
        * index_above, index_below:  bounds on searchindex (integer)
        * sort:  order in which to return results (string)
        * limit:  maximum number of items to return (integer)
        * offset:  position at which to restart search (string)
        * ids: a comma-separated list of BSO ids (list of strings)
        * full: flag, whether to include full bodies (bool)

    """
    for field in ("older", "newer", "index_above", "index_below", "limit"):
        value = request.GET.get(field)
        if value is not None:
            try:
                value = int(value)
                if value < 0:
                    raise ValueError
            except ValueError:
                msg = "Invalid value for %s: %r" % (field, value)
                request.errors.add("querystring", field, msg)
            else:
                request.validated[field] = value

    # The offset token is an opaque string, with semantics determined by
    # the storage backend, so we can't parse or validate it here.  Rather,
    # we must catch InvalidOffsetError if something goes wrong.
    offset = request.GET.get("offset")
    if offset is not None:
        request.validated["offset"] = offset

    sort = request.GET.get("sort")
    if sort is not None:
        if sort not in ("oldest", "newest", "index"):
            msg = "Invalid value for sort: %r" % (value, )
            request.errors.add("querystring", "sort", msg)
        else:
            request.validated["sort"] = sort

    ids = request.GET.get("ids")
    if ids is not None:
        ids = [id.strip() for id in ids.split(",")]
        if len(ids) > BATCH_MAX_COUNT:
            msg = 'Cannot process more than %s BSOs at a time'
            msg = msg % (BATCH_MAX_COUNT, )
            request.errors.add("querysting", "items", msg)
        else:
            for id in ids:
                if not VALID_ID_REGEX.match(id):
                    msg = "Invalid BSO id: %r" % (id, )
                    request.errors.add("querystring", "ids", msg)
        request.validated["ids"] = ids

    if "full" in request.GET:
        request.validated["full"] = True
Пример #2
0
def extract_query_params(request):
    """Validator to extract BSO search parameters from the query string.

    This validator will extract and validate the following search params:

        * older, newer:  bounds on last-modified version (integer)
        * index_above, index_below:  bounds on searchindex (integer)
        * sort:  order in which to return results (string)
        * limit:  maximum number of items to return (integer)
        * offset:  position at which to restart search (string)
        * ids: a comma-separated list of BSO ids (list of strings)
        * full: flag, whether to include full bodies (bool)

    """
    for field in ("older", "newer", "index_above", "index_below", "limit"):
        value = request.GET.get(field)
        if value is not None:
            try:
                value = int(value)
                if value < 0:
                    raise ValueError
            except ValueError:
                msg = "Invalid value for %s: %r" % (field, value)
                request.errors.add("querystring", field, msg)
            else:
                request.validated[field] = value

    # The offset token is an opaque string, with semantics determined by
    # the storage backend, so we can't parse or validate it here.  Rather,
    # we must catch InvalidOffsetError if something goes wrong.
    offset = request.GET.get("offset")
    if offset is not None:
        request.validated["offset"] = offset

    sort = request.GET.get("sort")
    if sort is not None:
        if sort not in ("oldest", "newest", "index"):
            msg = "Invalid value for sort: %r" % (value,)
            request.errors.add("querystring", "sort", msg)
        else:
            request.validated["sort"] = sort

    ids = request.GET.get("ids")
    if ids is not None:
        ids = [id.strip() for id in ids.split(",")]
        if len(ids) > BATCH_MAX_COUNT:
            msg = 'Cannot process more than %s BSOs at a time'
            msg = msg % (BATCH_MAX_COUNT,)
            request.errors.add("querysting", "items", msg)
        else:
            for id in ids:
                if not VALID_ID_REGEX.match(id):
                    msg = "Invalid BSO id: %r" % (id,)
                    request.errors.add("querystring", "ids", msg)
        request.validated["ids"] = ids

    if "full" in request.GET:
        request.validated["full"] = True
Пример #3
0
def extract_query_params(request):
    """Validator to extract BSO search parameters from the query string.

    This validator will extract and validate the following search params:

        * newer: lower-bound on last-modified time (float timestamp)
        * older: upper-bound on last-modified time (float timestamp)
        * sort:  order in which to return results (string)
        * limit:  maximum number of items to return (integer)
        * offset:  position at which to restart search (string)
        * ids: a comma-separated list of BSO ids (list of strings)
        * full: flag, whether to include full bodies (bool)

    """
    newer = request.GET.get("newer")
    if newer is not None:
        try:
            newer = get_timestamp(newer)
            if newer < 0:
                raise ValueError
        except ValueError:
            msg = "Invalid value for newer: %r" % (newer, )
            request.errors.add("querystring", "newer", msg)
        else:
            request.validated["newer"] = newer

    older = request.GET.get("older")
    if older is not None:
        try:
            older = get_timestamp(older)
            if older < 0:
                raise ValueError
        except ValueError:
            msg = "Invalid value for older: %r" % (older, )
            request.errors.add("querystring", "older", msg)
        else:
            request.validated["older"] = older

    limit = request.GET.get("limit")
    if limit is not None:
        try:
            limit = int(limit)
            if limit < 0:
                raise ValueError
        except ValueError:
            msg = "Invalid value for limit: %r" % (limit, )
            request.errors.add("querystring", "limit", msg)
        else:
            request.validated["limit"] = limit

    # The offset token is an opaque string, with semantics determined by
    # the storage backend, so we can't parse or validate it here.  Rather,
    # we must catch InvalidOffsetError if something goes wrong.
    offset = request.GET.get("offset")
    if offset is not None:
        request.validated["offset"] = offset

    sort = request.GET.get("sort")
    if sort is not None:
        if sort not in ("newest", "oldest", "index"):
            msg = "Invalid value for sort: %r" % (sort, )
            request.errors.add("querystring", "sort", msg)
        else:
            request.validated["sort"] = sort

    ids = request.GET.get("ids")
    if ids is not None:
        ids = [id.strip() for id in ids.split(",")]
        if len(ids) > BATCH_MAX_IDS:
            msg = 'Cannot process more than %s BSOs at a time'
            msg = msg % (BATCH_MAX_IDS, )
            request.errors.add("querysting", "items", msg)
        else:
            for id in ids:
                if not VALID_ID_REGEX.match(id):
                    msg = "Invalid BSO id: %r" % (id, )
                    request.errors.add("querystring", "ids", msg)
        request.validated["ids"] = ids

    if "full" in request.GET:
        request.validated["full"] = True
Пример #4
0
def extract_query_params(request):
    """Validator to extract BSO search parameters from the query string.

    This validator will extract and validate the following search params:

        * newer: lower-bound on last-modified time (float timestamp)
        * sort:  order in which to return results (string)
        * limit:  maximum number of items to return (integer)
        * offset:  position at which to restart search (string)
        * ids: a comma-separated list of BSO ids (list of strings)
        * full: flag, whether to include full bodies (bool)

    """
    newer = request.GET.get("newer")
    if newer is not None:
        try:
            newer = get_timestamp(newer)
            if newer < 0:
                raise ValueError
        except ValueError:
            msg = "Invalid value for newer: %r" % (newer,)
            request.errors.add("querystring", "newer", msg)
        else:
            request.validated["newer"] = newer

    limit = request.GET.get("limit")
    if limit is not None:
        try:
            limit = int(limit)
            if limit < 0:
                raise ValueError
        except ValueError:
            msg = "Invalid value for limit: %r" % (limit,)
            request.errors.add("querystring", "limit", msg)
        else:
            request.validated["limit"] = limit

    # The offset token is an opaque string, with semantics determined by
    # the storage backend, so we can't parse or validate it here.  Rather,
    # we must catch InvalidOffsetError if something goes wrong.
    offset = request.GET.get("offset")
    if offset is not None:
        request.validated["offset"] = offset

    sort = request.GET.get("sort")
    if sort is not None:
        if sort not in ("newest", "oldest", "index"):
            msg = "Invalid value for sort: %r" % (sort,)
            request.errors.add("querystring", "sort", msg)
        else:
            request.validated["sort"] = sort

    ids = request.GET.get("ids")
    if ids is not None:
        ids = [id.strip() for id in ids.split(",")]
        if len(ids) > BATCH_MAX_IDS:
            msg = 'Cannot process more than %s BSOs at a time'
            msg = msg % (BATCH_MAX_IDS,)
            request.errors.add("querysting", "items", msg)
        else:
            for id in ids:
                if not VALID_ID_REGEX.match(id):
                    msg = "Invalid BSO id: %r" % (id,)
                    request.errors.add("querystring", "ids", msg)
        request.validated["ids"] = ids

    if "full" in request.GET:
        request.validated["full"] = True