def copy_resource(ori_res, new_res, user=None): """ Populate metadata and contents from ori_res object to new_res object to make new_res object as a copy of the ori_res object Args: ori_res: the original resource that is to be copied. new_res: the new_res to be populated with metadata and content from the original resource as a copy of the original resource. user: requesting user for the copy action. It is optional, if being passed in, quota is counted toward the user; otherwise, quota is not counted toward that user Returns: the new resource copied from the original resource """ # add files directly via irods backend file operation utils.copy_resource_files_and_AVUs(ori_res.short_id, new_res.short_id) utils.copy_and_create_metadata(ori_res, new_res) hs_identifier = ori_res.metadata.identifiers.all().filter(name="hydroShareIdentifier")[0] if hs_identifier: new_res.metadata.create_element('source', derived_from=hs_identifier.url) if ori_res.resource_type.lower() == "collectionresource": # clone contained_res list of original collection and add to new collection # note that new collection will not contain "deleted resources" new_res.resources = ori_res.resources.all() # create bag for the new resource hs_bagit.create_bag(new_res) # need to update quota usage for new_res quota holder if user: update_quota_usage(user=user) return new_res
def copy_resource(ori_res, new_res): """ Populate metadata and contents from ori_res object to new_res object to make new_res object as a copy of the ori_res object Args: ori_res: the original resource that is to be copied. new_res: the new_res to be populated with metadata and content from the original resource as a copy of the original resource. Returns: the new resource copied from the original resource """ # add files directly via irods backend file operation utils.copy_resource_files_and_AVUs(ori_res.short_id, new_res.short_id) utils.copy_and_create_metadata(ori_res, new_res) hs_identifier = ori_res.metadata.identifiers.all().filter(name="hydroShareIdentifier")[0] if hs_identifier: new_res.metadata.create_element('source', derived_from=hs_identifier.url) if ori_res.resource_type.lower() == "collectionresource": # clone contained_res list of original collection and add to new collection # note that new collection will not contain "deleted resources" new_res.resources = ori_res.resources.all() # create bag for the new resource hs_bagit.create_bag(new_res) return new_res
def copy_resource_task(ori_res_id, new_res_id=None, request_username=None): try: new_res = None if not new_res_id: new_res = create_empty_resource(ori_res_id, request_username, action='copy') new_res_id = new_res.short_id utils.copy_resource_files_and_AVUs(ori_res_id, new_res_id) ori_res = utils.get_resource_by_shortkey(ori_res_id) if not new_res: new_res = utils.get_resource_by_shortkey(new_res_id) utils.copy_and_create_metadata(ori_res, new_res) hs_identifier = ori_res.metadata.identifiers.all().filter( name="hydroShareIdentifier")[0] if hs_identifier: new_res.metadata.create_element('source', derived_from=hs_identifier.url) if ori_res.resource_type.lower() == "collectionresource": # clone contained_res list of original collection and add to new collection # note that new collection will not contain "deleted resources" new_res.resources = ori_res.resources.all() # create bag for the new resource create_bag(new_res) return new_res.get_absolute_url() except Exception as ex: if new_res: new_res.delete() raise utils.ResourceCopyException(str(ex))
def create_new_version_resource(ori_res, new_res, user): """ Populate metadata and contents from ori_res object to new_res object to make new_res object as a new version of the ori_res object Args: ori_res: the original resource that is to be versioned. new_res: the new_res to be populated with metadata and content from the original resource to make it a new version user: the requesting user Returns: the new versioned resource for the original resource and thus obsolete the original resource """ # newly created new resource version is private initially # add files directly via irods backend file operation utils.copy_resource_files_and_AVUs(ori_res.short_id, new_res.short_id) # copy metadata from source resource to target new-versioned resource except three elements utils.copy_and_create_metadata(ori_res, new_res) # add or update Relation element to link source and target resources hs_identifier = new_res.metadata.identifiers.all().filter( name="hydroShareIdentifier")[0] ori_res.metadata.create_element('relation', type='isReplacedBy', value=hs_identifier.url) if new_res.metadata.relations.all().filter(type='isVersionOf').exists(): # the original resource is already a versioned resource, and its isVersionOf relation # element is copied over to this new version resource, needs to delete this element so # it can be created to link to its original resource correctly eid = new_res.metadata.relations.all().filter( type='isVersionOf').first().id new_res.metadata.delete_element('relation', eid) hs_identifier = ori_res.metadata.identifiers.all().filter( name="hydroShareIdentifier")[0] new_res.metadata.create_element('relation', type='isVersionOf', value=hs_identifier.url) if ori_res.resource_type.lower() == "collectionresource": # clone contained_res list of original collection and add to new collection # note that new version collection will not contain "deleted resources" new_res.resources = ori_res.resources.all() # create bag for the new resource hs_bagit.create_bag(new_res) # since an isReplaceBy relation element is added to original resource, needs to call # resource_modified() for original resource utils.resource_modified(ori_res, user, overwrite_bag=False) # if everything goes well up to this point, set original resource to be immutable so that # obsoleted resources cannot be modified from REST API ori_res.raccess.immutable = True ori_res.raccess.save() return new_res
def create_new_version_resource_task(ori_res_id, username, new_res_id=None): """ Task for creating a new version of a resource Args: ori_res_id: the original resource id that is to be versioned. new_res_id: the new versioned resource id from the original resource. If None, a new resource will be created. username: the requesting user's username Returns: the new versioned resource url as the payload """ try: new_res = None if not new_res_id: new_res = create_empty_resource(ori_res_id, username) new_res_id = new_res.short_id utils.copy_resource_files_and_AVUs(ori_res_id, new_res_id) # copy metadata from source resource to target new-versioned resource except three elements ori_res = utils.get_resource_by_shortkey(ori_res_id) if not new_res: new_res = utils.get_resource_by_shortkey(new_res_id) utils.copy_and_create_metadata(ori_res, new_res) # add or update Relation element to link source and target resources hs_identifier = new_res.metadata.identifiers.all().filter( name="hydroShareIdentifier")[0] ori_res.metadata.create_element('relation', type='isReplacedBy', value=hs_identifier.url) if new_res.metadata.relations.all().filter( type='isVersionOf').exists(): # the original resource is already a versioned resource, and its isVersionOf relation # element is copied over to this new version resource, needs to delete this element so # it can be created to link to its original resource correctly eid = new_res.metadata.relations.all().filter( type='isVersionOf').first().id new_res.metadata.delete_element('relation', eid) hs_identifier = ori_res.metadata.identifiers.all().filter( name="hydroShareIdentifier")[0] new_res.metadata.create_element('relation', type='isVersionOf', value=hs_identifier.url) if ori_res.resource_type.lower() == "collectionresource": # clone contained_res list of original collection and add to new collection # note that new version collection will not contain "deleted resources" new_res.resources = ori_res.resources.all() # create bag for the new resource create_bag(new_res) # since an isReplaceBy relation element is added to original resource, needs to call # resource_modified() for original resource utils.resource_modified(ori_res, by_user=username, overwrite_bag=False) # if everything goes well up to this point, set original resource to be immutable so that # obsoleted resources cannot be modified from REST API ori_res.raccess.immutable = True ori_res.raccess.save() ori_res.save() return new_res.get_absolute_url() except Exception as ex: if new_res: new_res.delete() raise utils.ResourceVersioningException(str(ex)) finally: # release the lock regardless ori_res.locked_time = None ori_res.save()