def update(self, **kwargs): """ Update a collection. We intercept the call to encode the metadata if we modify it. Metadata passed in this method is user meta. :param username: the name of the user who made the action :type username: str, optional :param metadata: The plain password to encrypt :type metadata: dict """ from radon.model import Notification pre_state = self.mqtt_get_state() now_date = now() if "metadata" in kwargs: # Transform the metadata in cdmi format to the format stored in # Cassandra #kwargs["metadata"][radon.cfg.meta_modify_ts] = now_date kwargs["user_meta"] = meta_cdmi_to_cassandra(kwargs["metadata"]) del kwargs["metadata"] sys_meta = self.node.sys_meta sys_meta[radon.cfg.meta_modify_ts] = encode_meta(now_date) kwargs["sys_meta"] = sys_meta if "username" in kwargs: username = kwargs["username"] del kwargs["username"] else: username = None self.node.update(**kwargs) coll = Collection.find(self.path) post_state = coll.mqtt_get_state() payload = coll.mqtt_payload(pre_state, post_state) Notification.update_collection(username, coll.path, payload)
def create_root(cls): """ Create the root Collection to initialise the hierarchy :return: The Collection object for the root :rtype: :class:`radon.model.Collection` """ from radon.model import Notification now_date = now() sys_meta = { radon.cfg.meta_create_ts: encode_meta(now_date), radon.cfg.meta_modify_ts: encode_meta(now_date) } # If the root already exist it won't be replaced as PK will be the same root_node = TreeNode.create( container="/", name=".", sys_meta=sys_meta, ) root_node.add_default_acl() new = cls(root_node) state = new.mqtt_get_state() payload = new.mqtt_payload({}, state) Notification.create_collection(radon.cfg.sys_lib_user, "/", payload) return new
def test_metadata_to_list(): meta = {"test": encode_meta("val")} assert metadata_to_list(meta) == [("test", "val")] meta = {"test": encode_meta(["val1", "val2"])} assert metadata_to_list(meta) == [("test", ["val1", "val2"])] meta = {cfg.meta_create_ts: encode_meta("Today")} assert metadata_to_list(meta, cfg.vocab_dict) == [ (cfg.vocab_dict[cfg.meta_create_ts], "Today") ] now_date = now() now_str = now_date.strftime("%A %d %B %Y - %H:%M:%S (%z)") meta = {cfg.meta_modify_ts: encode_meta(now_date)} assert metadata_to_list(meta, cfg.vocab_dict) == [ (cfg.vocab_dict[cfg.meta_modify_ts], now_str) ]
def update(self, **kwargs): """ Update a resource. We intercept the call to encode the metadata if we modify it. Metadata passed in this method is user meta. :param username: the name of the user who made the action :type username: str, optional :param metadata: The plain password to encrypt :type metadata: dict :param mimetype: The mimetype of the resource :type mimetype: str, optional """ from radon.model import Notification pre_state = self.mqtt_get_state() now_date = now() # Metadata given in cdmi format are transformed to be stored in Cassandra if 'metadata' in kwargs: kwargs['user_meta'] = kwargs['metadata'] del kwargs["metadata"] if "user_meta" in kwargs: kwargs["user_meta"] = meta_cdmi_to_cassandra(kwargs["user_meta"]) sys_meta = self.node.sys_meta sys_meta[radon.cfg.meta_modify_ts] = encode_meta(now_date) kwargs["sys_meta"] = sys_meta if "mimetype" in kwargs: sys_meta[radon.cfg.meta_mimetype] = kwargs["mimetype"] del kwargs["mimetype"] if "username" in kwargs: username = kwargs["username"] del kwargs["username"] else: username = None self.node.update(**kwargs) resc = Resource.find(self.path) post_state = resc.mqtt_get_state() payload = resc.mqtt_payload(pre_state, post_state) Notification.update_resource(username, resc.path, payload)
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