def path(self): """ Get the full path of the element. See :meth:`radon.util.merge` :return: The merged path :rtype: str """ return merge(self.container, self.name)
def test_merge(): assert merge("", "a") == "/a" assert merge("/A", "a") == "/A/a" assert merge("/", "a") == "/a" assert merge("/", "") == "/" assert merge("/A/", "a") == "/A/a" assert merge("/A/", "B/") == "/A/B/"
def view_collection(request, path='/'): """Display the page which shows the subcollections/resources of a collection""" if not path: path = "/" collection = Collection.find(path) if not collection: raise Http404() if not collection.user_can(request.user, "read") and not collection.is_root: # If the user can't read, then return 404 rather than 403 so that # we don't leak information. raise Http404() paths = [] full = "/" for p in collection.path.split("/"): if not p: continue full = u"{}{}/".format(full, p) paths.append((p, full)) children_c, children_r = collection.get_child(False) children_c.sort(key=lambda x: x.lower()) children_r.sort(key=lambda x: x.lower()) ctx = { "collection": collection.to_dict(request.user), "children_c": [ Collection.find(merge(path, c)).to_dict(request.user) for c in children_c ], "children_r": [ Resource.find(merge(path, c)).simple_dict(request.user) for c in children_r ], "collection_paths": paths, "empty": len(children_c) + len(children_r) == 0, } return render(request, "archive/index.html", ctx)
def __init__(self, node): """ Create the collection object based on the TreeNode row from Cassandra :param node: The TreeNode row that corresponds to the collection :type node: :class:`radon.model.TreeNode` """ self.node = node self.is_root = self.node.name == "." and self.node.container == "/" # Get name if self.is_root: self.name = "Home" self.path = "/" self.container = "/" else: # Name ends with "/" to follow CDMI standard and differentiate # collections and data objects self.name = self.node.name self.container = self.node.container self.path = merge(self.container, self.name) self.uuid = self.node.uuid
def create(cls, container, name, metadata=None, creator=None): """ Create a new collection :param container: The name of the parent collection :type container: str :param name: The name of the collection, should end with '/' :type name: str :param metadata: A Key/Value pair dictionary for user metadata :type metadata: dict, optional :param creator: The name of the user who created the collection :type creator: str, optional :return: The new Collection object :rtype: :class:`radon.model.Collection` """ from radon.model import Notification from radon.model import Resource if not name.endswith("/"): name = name + '/' if not container.endswith("/"): container = container + '/' path = merge(container, name) # Check if parent collection exists parent = Collection.find(container) if parent is None: raise NoSuchCollectionError(container) resource = Resource.find(merge(container, name)) if resource is not None: raise ResourceConflictError(container) collection = Collection.find(path) if collection is not None: raise CollectionConflictError(container) now_date = now() if not metadata: user_meta = {} else: user_meta = metadata sys_meta = { radon.cfg.meta_create_ts: encode_meta(now_date), radon.cfg.meta_modify_ts: encode_meta(now_date) } coll_node = TreeNode.create(container=container, name=name, user_meta=user_meta, sys_meta=sys_meta) if not creator: creator = radon.cfg.sys_lib_user new = cls(coll_node) state = new.mqtt_get_state() payload = new.mqtt_payload({}, state) Notification.create_collection(creator, path, payload) # # Index the collection # new.index() return new
def create(cls, container, name, url=None, metadata=None, creator=None, mimetype=None, size=None): """ Create a new resource :param container: The name of the parent collection :type container: str :param name: The name of the resource :type name: str :param url: The url of the resource :type url: str, optional :param metadata: A Key/Value pair dictionary for user metadata :type metadata: dict, optional :param creator: The name of the user who created the resource :type creator: str, optional :param mimetype: The mimetype of the resource :type mimetype: str, optional :param size: The name of the user who created the resource :type size: str, optional :return: The new Resource object :rtype: :class:`radon.model.Resource` """ from radon.model import Collection from radon.model import Notification if not container.endswith('/'): container += '/' # Make sure parent/name are not in use. path = merge(container, name) existing = cls.find(path) if existing: raise ResourceConflictError(path) # Check if parent collection exists parent = Collection.find(container) if parent is None: raise NoSuchCollectionError(container) now_date = now() if not metadata: user_meta = {} else: user_meta = {} for k in metadata: user_meta[k] = encode_meta(metadata[k]) sys_meta = { radon.cfg.meta_create_ts: encode_meta(now_date), radon.cfg.meta_modify_ts: encode_meta(now_date) } if mimetype: sys_meta[radon.cfg.meta_mimetype] = mimetype if size: sys_meta[radon.cfg.meta_size] = str(size) if not url: url = "{}{}".format(radon.cfg.protocol_cassandra, default_cdmi_id()) resc_node = TreeNode.create(container=container, name=name, user_meta=user_meta, sys_meta=sys_meta, object_url=url) if not creator: creator = radon.cfg.sys_lib_user if url.startswith(radon.cfg.protocol_cassandra): new = RadonResource(resc_node) else: new = UrlLibResource(resc_node) state = new.mqtt_get_state() payload = new.mqtt_payload({}, state) Notification.create_resource(creator, path, payload) # # Index the resource # new.index() return new