示例#1
0
def parse_resource(resource):
    """Extract the bucket_id and collection_id of the given resource (URI)

    :param str resource: a uri formatted /buckets/<bid>/collections/<cid> or <bid>/<cid>.
    :returns: a dictionary with the bucket_id and collection_id of the resource
    """

    error_msg = 'Resources should be defined as '
    "'/buckets/<bid>/collections/<cid>' or '<bid>/<cid>'. "
    'with valid collection and bucket ids.'

    from kinto.views import NameGenerator
    id_generator = NameGenerator()
    parts = resource.split('/')
    if len(parts) == 2:
        bucket, collection = parts
    elif len(parts) == 5:
        _, _, bucket, _, collection = parts
    else:
        raise ValueError(error_msg)
    if bucket == '' or collection == '':
        raise ValueError(error_msg)
    if not id_generator.match(bucket) or not id_generator.match(collection):
        raise ValueError(error_msg)
    return {'bucket': bucket, 'collection': collection}
示例#2
0
def parse_resource(resource):
    """Extract the bucket_id and collection_id of the given resource (URI)

    :param str resource: a uri formatted /buckets/<bid>/collections/<cid> or <bid>/<cid>.
    :returns: a dictionary with the bucket_id and collection_id of the resource
    """

    error_msg = "Resources should be defined as "
    "'/buckets/<bid>/collections/<cid>' or '<bid>/<cid>'. "
    "with valid collection and bucket ids."

    from kinto.views import NameGenerator

    id_generator = NameGenerator()
    parts = resource.split("/")
    if len(parts) == 2:
        bucket, collection = parts
    elif len(parts) == 5:
        _, _, bucket, _, collection = parts
    else:
        raise ValueError(error_msg)
    if bucket == "" or collection == "":
        raise ValueError(error_msg)
    if not id_generator.match(bucket) or not id_generator.match(collection):
        raise ValueError(error_msg)
    return {"bucket": bucket, "collection": collection}
示例#3
0
def _get_resource(resource):
    # Use the default NameGenerator in Kinto resources to check if the resource
    # URIs seem valid.
    # XXX: if a custom ID generator is specified in settings, this verification would
    # not result as expected.
    name_generator = NameGenerator()

    parts = resource.split('/')
    if len(parts) == 2:
        bucket, collection = parts
    elif len(parts) == 3 and parts[1] == 'buckets':
        # /buckets/bid
        _, _, bucket = parts
        collection = None
    elif len(parts
             ) == 5 and parts[1] == 'buckets' and parts[3] == 'collections':
        # /buckets/bid/collections/cid
        _, _, bucket, _, collection = parts
    else:
        raise ValueError("should be a bucket or collection URI")
    valid_ids = (name_generator.match(bucket)
                 and (collection is None or name_generator.match(collection)))
    if not valid_ids:
        raise ValueError("bucket or collection id is invalid")
    return {'bucket': bucket, 'collection': collection}
示例#4
0
def _get_resource(resource):
    # Use the default NameGenerator in Kinto resources to check if the resource
    # URIs seem valid.
    # XXX: if a custom ID generator is specified in settings, this verification would
    # not result as expected.
    name_generator = NameGenerator()

    parts = resource.split('/')
    if len(parts) == 2:
        bucket, collection = parts
    elif len(parts) == 3 and parts[1] == 'buckets':
        # /buckets/bid
        _, _, bucket = parts
        collection = None
    elif len(parts) == 5 and parts[1] == 'buckets' and parts[3] == 'collections':
        # /buckets/bid/collections/cid
        _, _, bucket, _, collection = parts
    else:
        raise ValueError("should be a bucket or collection URI")
    valid_ids = (name_generator.match(bucket) and
                 (collection is None or name_generator.match(collection)))
    if not valid_ids:
        raise ValueError("bucket or collection id is invalid")
    return {
        'bucket': bucket,
        'collection': collection
    }