def post(self, pid, record, **kwargs): with db.session.begin_nested(): current_drafts.unpublish( RecordContext(record=record, record_pid=pid)) current_search_client.indices.refresh() current_search_client.indices.flush() endpoint = 'invenio_records_rest.{0}_item'.format( self.draft_endpoint_name) return redirect(url_for(endpoint, pid_value=pid.pid_value, _external=True), code=302)
def test_unpublish_record_existing_draft(app, db, schemas): TestDraftRecord.schema = schemas['draft'] TestPublishedRecord.schema = schemas['published'] with db.session.begin_nested(): published_uuid = uuid.uuid4() published_record = TestPublishedRecord.create( { 'id': '1', 'title': '11' }, id_=published_uuid) published_pid = PersistentIdentifier.create( pid_type='recid', pid_value='1', status=PIDStatus.REGISTERED, object_type='rec', object_uuid=published_uuid) assert published_record.revision_id == 0 draft_uuid = uuid.uuid4() draft_record = TestDraftRecord.create({ 'id': '1', 'title': '22' }, id_=draft_uuid) draft_pid = PersistentIdentifier.create(pid_type='drecid', pid_value='1', status=PIDStatus.REGISTERED, object_type='rec', object_uuid=draft_uuid) assert draft_record.revision_id == 0 with disable_test_authenticated(): current_drafts.unpublish( RecordContext(record=published_record, record_pid=published_pid)) # published version should be gone published_pid = PersistentIdentifier.get(pid_type='recid', pid_value='1') assert published_pid.status == PIDStatus.DELETED rec = TestDraftRecord.get_record(published_uuid, with_deleted=True) assert rec.model.json is None draft_pid = PersistentIdentifier.get(pid_type='drecid', pid_value='1') assert draft_pid.status == PIDStatus.REGISTERED rec = TestDraftRecord.get_record(draft_pid.object_uuid) assert rec.model.json is not None assert rec['title'] == '22' # should not be changed on a newer record assert rec.revision_id == 1
def test_unpublish_record_redirected_draft(app, db, schemas): TestDraftRecord.schema = schemas['draft'] TestPublishedRecord.schema = schemas['published'] with db.session.begin_nested(): published_uuid = uuid.uuid4() published_record = TestPublishedRecord.create( { 'id': '1', 'title': '11' }, id_=published_uuid) published_pid = PersistentIdentifier.create( pid_type='recid', pid_value='1', status=PIDStatus.REGISTERED, object_type='rec', object_uuid=published_uuid) assert published_record.revision_id == 0 draft_uuid = uuid.uuid4() draft_record = TestDraftRecord.create({ 'id': '1', 'title': '22' }, id_=draft_uuid) draft_pid = PersistentIdentifier.create(pid_type='drecid', pid_value='1', status=PIDStatus.REGISTERED, object_type='rec', object_uuid=draft_uuid) assert draft_record.revision_id == 0 with db.session.begin_nested(): draft_record.delete() draft_pid.status = PIDStatus.REDIRECTED db.session.add(draft_pid) with db.session.begin_nested(): with pytest.raises(NotImplementedError): with disable_test_authenticated(): current_drafts.unpublish( RecordContext(record=published_record, record_pid=published_pid))
def test_unpublish(app, db, schemas, mappings, prepare_es): with disable_test_authenticated(): with db.session.begin_nested(): published_uuid1 = uuid.uuid4() published_rec1 = PublishedRecord.create( { 'id': '1', 'title': 'rec1' }, id_=published_uuid1) published_pid1 = PersistentIdentifier.create( pid_type='recid', pid_value='1', status=PIDStatus.REGISTERED, object_type='rec', object_uuid=published_uuid1) published_uuid2 = uuid.uuid4() published_rec2 = PublishedRecord.create( { 'id': '2', 'title': 'rec2' }, id_=published_uuid2) published_pid2 = PersistentIdentifier.create( pid_type='recid', pid_value='2', status=PIDStatus.REGISTERED, object_type='rec', object_uuid=published_uuid2) published_uuid3 = uuid.uuid4() published_rec3 = PublishedRecord.create( { 'id': '3', 'title': 'rec3', 'ref': { '$ref': 'http://localhost/records/1' }, 'ref_pub': { '$ref': 'http://localhost/records/2' } }, id_=published_uuid3) published_pid3 = PersistentIdentifier.create( pid_type='recid', pid_value='3', status=PIDStatus.REGISTERED, object_type='rec', object_uuid=published_uuid3) RecordIndexer().index(published_rec1) RecordIndexer().index(published_rec2) RecordIndexer().index(published_rec3) current_search_client.indices.refresh() current_search_client.indices.flush() es = RecordsSearch(index='draft-records-record-v1.0.0'). \ get_record(published_pid3.object_uuid).execute() assert len(es.hits) == 0 es = RecordsSearch(index='records-record-v1.0.0'). \ get_record(published_pid3.object_uuid).execute() assert len(es.hits) == 1 current_drafts.unpublish( RecordContext(record=published_rec3, record_pid=published_pid3)) assert PersistentIdentifier.get( pid_type='recid', pid_value=published_pid3.pid_value).status == PIDStatus.DELETED assert PersistentIdentifier.get( pid_type='recid', pid_value=published_pid2.pid_value).status == PIDStatus.DELETED assert PersistentIdentifier.get( pid_type='recid', pid_value=published_pid1.pid_value).status == PIDStatus.DELETED draft_pid3 = PersistentIdentifier.get( pid_type='drecid', pid_value=published_pid3.pid_value) pr = DraftRecord.get_record(draft_pid3.object_uuid) assert pr.dumps() == { '$schema': 'https://localhost/schemas/draft/records/record-v1.0.0.json', 'id': '3', 'invenio_draft_validation': { 'valid': True }, 'ref': { '$ref': 'http://localhost/drafts/records/1' }, 'ref_pub': { '$ref': 'http://localhost/drafts/records/2' }, 'title': 'rec3' } draft_pid2 = PersistentIdentifier.get( pid_type='drecid', pid_value=published_pid2.pid_value) pr = DraftRecord.get_record(draft_pid2.object_uuid) assert pr.dumps() == { '$schema': 'https://localhost/schemas/draft/records/record-v1.0.0.json', 'id': '2', 'invenio_draft_validation': { 'valid': True }, 'title': 'rec2' } draft_pid1 = PersistentIdentifier.get( pid_type='drecid', pid_value=published_pid1.pid_value) pr = DraftRecord.get_record(draft_pid1.object_uuid) assert pr.dumps() == { '$schema': 'https://localhost/schemas/draft/records/record-v1.0.0.json', 'id': '1', 'invenio_draft_validation': { 'valid': True }, 'title': 'rec1' } current_search_client.indices.refresh() current_search_client.indices.flush() es = RecordsSearch(index='draft-records-record-v1.0.0'). \ get_record(draft_pid3.object_uuid).execute() assert len(es.hits) == 1 es = es.hits[0].to_dict() es.pop('_created') es.pop('_updated') assert es == { '$schema': 'https://localhost/schemas/draft/records/record-v1.0.0.json', 'id': '3', 'invenio_draft_validation': { 'valid': True }, 'ref': { 'draft': '1' }, 'ref_pub': { 'draft': '2' }, 'title': 'rec3' } es = RecordsSearch(index='records-record-v1.0.0'). \ get_record(published_pid3.object_uuid).execute() assert len(es.hits) == 0 es = RecordsSearch(index='draft-records-record-v1.0.0'). \ get_record(draft_pid1.object_uuid).execute() assert len(es.hits) == 1 es = es.hits[0].to_dict() es.pop('_created') es.pop('_updated') assert es == { '$schema': 'https://localhost/schemas/draft/records/record-v1.0.0.json', 'id': '1', 'invenio_draft_validation': { 'valid': True }, 'title': 'rec1' } es = RecordsSearch(index='records-record-v1.0.0'). \ get_record(published_pid1.object_uuid).execute() assert len(es.hits) == 0 es = RecordsSearch(index='records-record-v1.0.0'). \ get_record(published_pid3.object_uuid).execute() assert len(es.hits) == 0 es = RecordsSearch(index='draft-records-record-v1.0.0'). \ get_record(draft_pid2.object_uuid).execute() assert len(es.hits) == 1 es = es.hits[0].to_dict() es.pop('_created') es.pop('_updated') assert es == { '$schema': 'https://localhost/schemas/draft/records/record-v1.0.0.json', 'id': '2', 'invenio_draft_validation': { 'valid': True }, 'title': 'rec2' } es = RecordsSearch(index='records-record-v1.0.0'). \ get_record(published_pid2.object_uuid).execute() assert len(es.hits) == 0