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}
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}
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 }
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}
def __init__(self, *args, **kwargs): super(Collection, self).__init__(*args, **kwargs) bucket_id = self.request.matchdict['bucket_id'] object_exists_or_404(self.request, collection_id='bucket', object_id=bucket_id) self.collection.id_generator = NameGenerator()
def __init__(self, *args, **kwargs): super(Group, self).__init__(*args, **kwargs) bucket_id = self.request.matchdict['bucket_id'] object_exists_or_404(self.request, collection_id='bucket', object_id=bucket_id) parent_id = '/buckets/%s' % bucket_id self.collection.parent_id = parent_id self.collection.id_generator = NameGenerator()
def parse_resources(raw_resources): resources = OrderedDict() name_generator = NameGenerator() for res in aslist(raw_resources): error_msg = ("Resources should be defined as " "'/buckets/<bid>/collections/<cid>;" "/buckets/<bid>/collections/<cid>' and " "separated with space or linebreaks. Got %r" % res) if ";" not in res: raise ConfigurationError(error_msg) try: triplet = res.strip(';').split(';') if len(triplet) == 2: source, destination = triplet preview = None else: source, preview, destination = triplet except ValueError: raise ConfigurationError(error_msg) def _get_resource(resource): parts = resource.split('/') if len(parts) == 2: bucket, collection = parts elif len(parts) == 5: _, _, bucket, _, collection = parts else: raise ConfigurationError(error_msg) valid_ids = (name_generator.match(bucket) and name_generator.match(collection)) if not valid_ids: raise ConfigurationError(error_msg) return { 'bucket': bucket, 'collection': collection } pattern = '/buckets/{bucket}/collections/{collection}' source = _get_resource(source) destination = _get_resource(destination) key = pattern.format(**source) resources[key] = { 'source': source, 'destination': destination, } if preview is not None: resources[key]['preview'] = _get_resource(preview) return resources
def __init__(self, *args, **kwargs): super(Bucket, self).__init__(*args, **kwargs) self.collection.id_generator = NameGenerator()
def __init__(self, *args, **kwargs): super(Bucket, self).__init__(*args, **kwargs) self.model.id_generator = NameGenerator()
def id_generator(self): # This generator is used for ID validation. return NameGenerator()
def __init__(self, *args, **kwargs): super(Collection, self).__init__(*args, **kwargs) self.model.id_generator = NameGenerator()
def __init__(self, *args, **kwargs): super(Bucket, self).__init__(*args, **kwargs) # Buckets are not isolated by user, unlike Cliquet resources. self.collection.parent_id = '' self.collection.id_generator = NameGenerator()