def create_id(user, data, acl=None): ''' register objects or collections''' try: data.keys() if "ids" in data: data = data["ids"] raise AttributeError() return register_object(user, data), data except AttributeError: collection = [] # each entitty can either be an ID or an JSON object description ids = [] objects = [] for entity in data: uuid = uuid_format(entity) if uuid: ids.append(uuid) else: #collection.append(register_object(user, entity)) objects.append(format_object(user, entity)) store = Couch(settings["db_name"]) id_tuples = store.raw_db().update(objects) collection = ids + [tup[1] for tup in id_tuples] if acl: return register_object(user, {"ids": collection, "acl": acl}), collection return register_object(user, {"ids": collection}), collection
def create_id(user, data, acl=None): ''' register objects or collections''' try: data.keys() if "ids" in data: data = data["ids"] raise AttributeError() return register_object(user, data), data except AttributeError: collection = [] # each entitty can either be an ID or an JSON object description ids = [] objects = [] for entity in data: uuid = uuid_format(entity) if uuid: ids.append(uuid) else: #collection.append(register_object(user, entity)) objects.append(format_object(user, entity)) store = Couch(settings["db_name"]) id_tuples = store.raw_db().update(objects) collection = ids + [tup[1] for tup in id_tuples] if acl: return register_object(user, { "ids": collection, "acl": acl }), collection return register_object(user, {"ids": collection}), collection
def register_object(user, record): ''' Create a new id for this metadata ''' if not can_create_ids(user): raise Unauthorized("You do not have permission to create new IDs") record = format_object(user, record) store = Couch(settings["db_name"]) uuid = store.save(record) return uuid
def get_metadata(user, uuid, perm, ignore=None): '''Retrun the simple metadata associated with this id if the user making this request is permitted to see it''' if ignore is None: ignore = [] store = Couch(settings["db_name"]) try: record = store[uuid] acl = IdAcl(record["acl"]) if acl.allow(perm, IdAcl.TENANT_NAME, user.tenant_name()) or \ acl.allow(perm, IdAcl.USERNAME, user.username()): if "ids" in record: collection = store[record["ids"]] # check for nested collection for now assume one level of # nesting. if "ids" in collection[0]: return {"ids": [item["_id"] for item in collection]} return [{k: v for k, v in item.items() if k not in ignore} for item in collection] return {k: v for k, v in record.items() if k not in ignore} else: raise Unauthorized() except couchdb.ResourceNotFound: raise NotFound()
def list_projects(user, write=None, ignore=None): ''' Return the list of projects this user can see''' if ignore is None: ignore = ["_rev", "acl", "file_protocol", "metadata_format", "cloud", "file_acl"] store = Couch(db_name="project") projects = store.list_all() perm = IdAcl.WRITE if write else IdAcl.READ visible = [] for record in projects: acl = IdAcl(record["acl"]) if acl.allow(perm, IdAcl.TENANT_NAME, user.tenant_name()) or \ acl.allow(perm, IdAcl.USERNAME, user.username()): visible.append(record) return [{k.strip("_"): v for k, v in i.items() if k not in ignore} for i in visible]
def modify_acl(user, uuid, data, func): ''' Helper function to change acl of uuid. func takes arguments metadata of uuid attribute ["acl"] and data["acl"] then set new acl to func's return value. ''' record_str = get_metadata(user, uuid, IdAcl.WRITE_ACP) try: IdAcl(data["acl"]) record = json.loads(record_str) record["acl"] = func(record["acl"], data["acl"]) store = Couch(settings["db_name"]) store[uuid] = record except (KeyError, ValueError): raise NotFound()
def get_metadata(project_name): ''' Return the full metadata of the project ''' couch = Couch(project_name) # Wrong! # TOOD: fix this return json.dumps(couch)
def get_cloud_info(cloud): ''' Return info about the cloud ''' couch = Couch("clouds") return json.dumps(couch[cloud])