def publish(self, pid=None, id_=None, user_id=None, sip_agent=None, spam_check=True): """Publish the Zenodo deposit.""" self['owners'] = self['_deposit']['owners'] self.validate_publish() if spam_check: self.spam_check() is_first_publishing = not self.is_published() deposit = super(ZenodoDeposit, self).publish(pid, id_) recid, record = deposit.fetch_published() pv = PIDVersioning(child=recid) is_new_version = pv.children.count() > 1 # a) Fetch the last SIP from the previous version if it's a new version # b) Fetch the previous SIP if publishing the metadata edit if is_new_version or (not is_first_publishing): if is_new_version: sip_recid = pv.children.all()[-2] else: # (not is_first_publishing) sip_recid = recid # Get the last SIP of the relevant recid, i.e.: either last # version or the current one sip_patch_of = (db.session.query(SIPModel).join( RecordSIPModel, RecordSIPModel.sip_id == SIPModel.id).filter( RecordSIPModel.pid_id == sip_recid.id).order_by( SIPModel.created.desc()).first()) else: sip_patch_of = None recordsip = RecordSIP.create(recid, record, archivable=True, create_sip_files=is_first_publishing, user_id=user_id, agent=sip_agent) archiver = BagItArchiver( recordsip.sip, include_all_previous=(not is_first_publishing), patch_of=sip_patch_of) archiver.save_bagit_metadata() return deposit
def publish(self, pid=None, id_=None, user_id=None, sip_agent=None): """Publish the Zenodo deposit.""" self['owners'] = self['_deposit']['owners'] self.validate_publish() is_first_publishing = not self.is_published() deposit = super(ZenodoDeposit, self).publish(pid, id_) recid, record = deposit.fetch_published() pv = PIDVersioning(child=recid) is_new_version = pv.children.count() > 1 # a) Fetch the last SIP from the previous version if it's a new version # b) Fetch the previous SIP if publishing the metadata edit if is_new_version or (not is_first_publishing): if is_new_version: sip_recid = pv.children.all()[-2] else: # (not is_first_publishing) sip_recid = recid # Get the last SIP of the relevant recid, i.e.: either last # version or the current one sip_patch_of = ( db.session.query(SIPModel) .join(RecordSIPModel, RecordSIPModel.sip_id == SIPModel.id) .filter(RecordSIPModel.pid_id == sip_recid.id) .order_by(SIPModel.created.desc()) .first() ) else: sip_patch_of = None recordsip = RecordSIP.create( recid, record, archivable=True, create_sip_files=is_first_publishing, user_id=user_id, agent=sip_agent) archiver = BagItArchiver( recordsip.sip, include_all_previous=(not is_first_publishing), patch_of=sip_patch_of) archiver.save_bagit_metadata() return deposit
def test_save_bagit_metadata(sips): """Test saving of bagit metadata.""" sip = sips[0] assert not BagItArchiver.get_bagit_metadata(sip) archiver = BagItArchiver(sip) archiver.save_bagit_metadata() bmeta = BagItArchiver.get_bagit_metadata(sip, as_dict=True) file_m = next(f for f in bmeta['files'] if 'sipfilepath' in f) assert file_m['sipfilepath'] == 'foobar.txt' assert file_m['filepath'] == 'data/files/foobar.txt' sip.model.sip_files[0].filepath = 'changed.txt' with pytest.raises(Exception) as excinfo: archiver.save_bagit_metadata() assert 'Attempting to save' in str(excinfo.value) archiver.save_bagit_metadata(overwrite=True) bmeta = BagItArchiver.get_bagit_metadata(sip, as_dict=True) file_m = next(f for f in bmeta['files'] if 'sipfilepath' in f) assert file_m['sipfilepath'] == 'changed.txt' assert file_m['filepath'] == 'data/files/changed.txt'