Пример #1
0
def test_publish_deleted_published(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',
                '$schema': 'records/record-v1.0.0.json'
            },
            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()
        rec = 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)

    with db.session.begin_nested():
        published_record.delete()
        published_pid.status = PIDStatus.DELETED
        db.session.add(published_pid)

    with db.session.begin_nested():
        rec = TestDraftRecord.get_record(draft_uuid)
        draft_pid = PersistentIdentifier.get(pid_type='drecid', pid_value='1')
        with disable_test_authenticated():
            current_drafts.publish(
                RecordContext(record=rec, record_pid=draft_pid))

    with db.session.begin_nested():
        # draft should be gone
        draft_pid = PersistentIdentifier.get(pid_type='drecid', pid_value='1')
        assert draft_pid.status == PIDStatus.DELETED
        rec = TestDraftRecord.get_record(draft_uuid, with_deleted=True)
        assert rec.model.json is None

        published_pid = PersistentIdentifier.get(pid_type='recid',
                                                 pid_value='1')
        assert published_pid.status == PIDStatus.REGISTERED
        rec = TestPublishedRecord.get_record(published_pid.object_uuid)
        assert rec['title'] == '22'
        # revision 0 original, 1 deleted, 2 temporarily reverted to orig, 3 published
        assert rec.revision_id == 3
Пример #2
0
def test_publish_record_marshmallow(app, db, schemas):
    TestDraftRecord.schema = schemas['draft']
    TestPublishedRecord.schema = schemas['published']
    with db.session.begin_nested():
        draft_uuid = uuid.uuid4()

        rec = TestDraftRecord.create({'id': '1'}, id_=draft_uuid)
        draft_pid = PersistentIdentifier.create(pid_type='drecid',
                                                pid_value='1',
                                                status=PIDStatus.REGISTERED,
                                                object_type='rec',
                                                object_uuid=draft_uuid)

        with pytest.raises(InvalidRecordException):
            # title is required but not in rec, so should fail
            with disable_test_authenticated():
                current_drafts.publish(
                    RecordContext(record=rec, record_pid=draft_pid))

        with pytest.raises(PIDDoesNotExistError):
            # no record should be created
            PersistentIdentifier.get(pid_type='recid', pid_value='1')

        # make the record valid
        rec['title'] = 'blah'
        rec.commit()

        assert rec['invenio_draft_validation']['valid']

        # and publish it again
        with disable_test_authenticated():
            current_drafts.publish(
                RecordContext(record=rec, record_pid=draft_pid))

        # draft should be gone
        draft_pid = PersistentIdentifier.get(pid_type='drecid', pid_value='1')
        assert draft_pid.status == PIDStatus.DELETED
        rec = TestDraftRecord.get_record(draft_uuid, with_deleted=True)
        assert rec.model.json is None

        published_pid = PersistentIdentifier.get(pid_type='recid',
                                                 pid_value='1')
        assert published_pid.status == PIDStatus.REGISTERED
        rec = TestPublishedRecord.get_record(published_pid.object_uuid)
        assert rec.model.json is not None
Пример #3
0
def test_draft_record_deleted_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.DELETED
        db.session.add(draft_pid)

    with db.session.begin_nested():
        with disable_test_authenticated():
            current_drafts.edit(
                RecordContext(record=published_record,
                              record_pid=published_pid))

        # published version should be there unchanged
        published_pid = PersistentIdentifier.get(pid_type='recid',
                                                 pid_value='1')
        assert published_pid.status == PIDStatus.REGISTERED
        rec = TestDraftRecord.get_record(published_uuid, with_deleted=True)
        assert rec['title'] == '11'
        assert rec.revision_id == 0

        # draft version should be there unchanged
        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'] == '11'
        assert rec.revision_id == 4
Пример #4
0
 def post(self, pid, record, **kwargs):
     current_drafts.publish(RecordContext(record=record, record_pid=pid))
     current_search_client.indices.refresh()
     current_search_client.indices.flush()
     endpoint = 'invenio_records_rest.{0}_item'.format(
         self.published_endpoint_name)
     return redirect(url_for(endpoint,
                             pid_value=pid.pid_value,
                             _external=True),
                     code=302)
Пример #5
0
def test_collect_for_edit(app, db, schemas):
    with db.session.begin_nested():
        published_uuid = uuid.uuid4()

        rec1 = PublishedRecord.create({
            'id': '1',
            'title': 'rec1'
        },
                                      id_=published_uuid)
        published1_pid = PersistentIdentifier.create(
            pid_type='recid',
            pid_value='1',
            status=PIDStatus.REGISTERED,
            object_type='rec',
            object_uuid=published_uuid)

        draft_uuid = uuid.uuid4()
        draft = DraftRecord.create({
            'id': '3',
            'title': 'rec1a'
        },
                                   id_=draft_uuid)
        draft_pid = PersistentIdentifier.create(pid_type='drecid',
                                                pid_value='3',
                                                status=PIDStatus.REGISTERED,
                                                object_type='rec',
                                                object_uuid=draft_uuid)

        published2_uuid = uuid.uuid4()
        rec2 = PublishedRecord.create(
            {
                'id': '2',
                'title': 'rec2',
                'ref': {
                    '$ref': 'http://localhost/api/records/1'
                },
                'ref_pub': {
                    '$ref': 'http://localhost/api/draft/records/3'
                }
            },
            id_=published2_uuid)
        published2_pid = PersistentIdentifier.create(
            pid_type='recid',
            pid_value='2',
            status=PIDStatus.REGISTERED,
            object_type='rec',
            object_uuid=published2_uuid)

    collected = list(
        collect_referenced_records(
            None, RecordContext(record=rec2, record_pid=published2_pid),
            CollectAction.EDIT))
    assert len(collected) == 1
    assert collected[0].record_pid == published1_pid
    assert collected[0].record.model.id == rec1.model.id
Пример #6
0
def test_publish_record_with_previous_version(app, db, schemas):
    TestDraftRecord.schema = schemas['draft']
    TestPublishedRecord.schema = schemas['published']
    with db.session.begin_nested():
        published_uuid = uuid.uuid4()
        PersistentIdentifier.create(pid_type='recid',
                                    pid_value='1',
                                    status=PIDStatus.REGISTERED,
                                    object_type='rec',
                                    object_uuid=published_uuid)
        published_record = TestPublishedRecord.create(
            {
                'id': '1',
                'title': '11'
            }, id_=published_uuid)
        assert published_record.revision_id == 0

        draft_uuid = uuid.uuid4()
        draft_pid = PersistentIdentifier.create(pid_type='drecid',
                                                pid_value='1',
                                                status=PIDStatus.REGISTERED,
                                                object_type='rec',
                                                object_uuid=draft_uuid)
        draft_record = TestDraftRecord.create({
            'id': '1',
            'title': '22'
        },
                                              id_=draft_uuid)
        assert draft_record.revision_id == 0

        print(draft_record['invenio_draft_validation'])

        # and publish it again
        with disable_test_authenticated():
            current_drafts.publish(
                RecordContext(record=draft_record, record_pid=draft_pid))

        # draft should be gone
        draft_pid = PersistentIdentifier.get(pid_type='drecid', pid_value='1')
        assert draft_pid.status == PIDStatus.DELETED
        rec = TestDraftRecord.get_record(draft_uuid, with_deleted=True)
        assert rec.model.json is None

        published_pid = PersistentIdentifier.get(pid_type='recid',
                                                 pid_value='1')
        assert published_pid.status == PIDStatus.REGISTERED
        rec = TestPublishedRecord.get_record(published_pid.object_uuid)
        assert rec.model.json is not None
        assert rec['title'] == '22'
        assert rec.revision_id == 1
Пример #7
0
def fill_record_urls(sender, record: RecordContext = None, action=None):
    # set the record url if not initially set
    record_pid_type = record.record_pid.pid_type
    if not getattr(record, 'record_url', None):
        # add the external url of the record
        if action == CollectAction.PUBLISH:
            endpoint = current_drafts.\
                find_endpoint_by_pid_type(record_pid_type).draft_endpoint
        else:
            endpoint = current_drafts.\
                find_endpoint_by_pid_type(record_pid_type).published_endpoint

        view_name = 'invenio_records_rest.' + \
                    RecordResource.view_name.format(endpoint['endpoint'])
        record.record_url = url_for(view_name,
                                    _external=True,
                                    pid_value=record.record_pid.pid_value)

    # add the external published and draft urls of the record
    if action == CollectAction.PUBLISH:
        record.draft_record_url = record.record_url
        endpoint = current_drafts.\
            find_endpoint_by_pid_type(record_pid_type).published_endpoint
        view_name = 'invenio_records_rest.' + \
                    RecordResource.view_name.format(endpoint['endpoint'])
        record.published_record_url = url_for(
            view_name, _external=True, pid_value=record.record_pid.pid_value)
    else:
        record.published_record_url = record.record_url

        endpoint = current_drafts.\
            find_endpoint_by_pid_type(record_pid_type).draft_endpoint
        view_name = 'invenio_records_rest.' + \
                    RecordResource.view_name.format(endpoint['endpoint'])
        record.draft_record_url = url_for(
            view_name, _external=True, pid_value=record.record_pid.pid_value)
Пример #8
0
def collect_referenced_records(sender,
                               record: RecordContext = None,
                               action=None):
    # gather references inside the record (which point to draft or published record)
    for ref in RecordReference.query.filter_by(
            record_uuid=record.record.model.id):
        if not ref.reference_uuid:
            continue
        for pid in PersistentIdentifier.query.filter_by(
                object_type='rec', object_uuid=ref.reference_uuid):
            if ((action == CollectAction.PUBLISH
                 and current_drafts.is_draft(pid))
                    or (action != CollectAction.PUBLISH
                        and current_drafts.is_published(pid))):
                # this pid is the target pid
                yield RecordContext(record=current_drafts.get_record(pid),
                                    record_pid=pid,
                                    record_url=ref.reference)
                break
Пример #9
0
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))
Пример #10
0
def test_publish(app, db, schemas, mappings, prepare_es):
    with disable_test_authenticated():
        with db.session.begin_nested():
            draft_uuid = uuid.uuid4()

            rec1 = DraftRecord.create({
                'id': '1',
                'title': 'rec1'
            }, id_=draft_uuid)
            draft1_pid = PersistentIdentifier.create(
                pid_type='drecid', pid_value='1', status=PIDStatus.REGISTERED,
                object_type='rec', object_uuid=draft_uuid
            )

            published_uuid = uuid.uuid4()
            published = PublishedRecord.create({
                'id': '3',
                'title': 'rec1a'
            }, id_=published_uuid)
            published_pid = PersistentIdentifier.create(
                pid_type='recid', pid_value='3', status=PIDStatus.REGISTERED,
                object_type='rec', object_uuid=published_uuid
            )

            draft2_uuid = uuid.uuid4()
            rec2 = DraftRecord.create({
                'id': '2',
                'title': 'rec2',
                'ref': {'$ref': 'http://localhost/drafts/records/1'},
                'ref_pub': {'$ref': 'http://localhost/records/3'}
            }, id_=draft2_uuid)
            draft2_pid = PersistentIdentifier.create(
                pid_type='drecid', pid_value='2', status=PIDStatus.REGISTERED,
                object_type='rec', object_uuid=draft2_uuid
            )
            RecordIndexer().index(rec2)

        current_search_client.indices.refresh()
        current_search_client.indices.flush()

        es_draft2 = RecordsSearch(index='draft-records-record-v1.0.0').\
            get_record(draft2_pid.object_uuid).execute()
        assert len(es_draft2.hits) == 1

        current_drafts.publish(RecordContext(record=rec2, record_pid=draft2_pid))

        published2_pid = PersistentIdentifier.get(pid_type='recid', pid_value=draft2_pid.pid_value)
        pr = PublishedRecord.get_record(published2_pid.object_uuid)
        assert pr.dumps() == {
            '$schema': 'https://localhost/schemas/records/record-v1.0.0.json',
            'id': '2',
            'ref': {'$ref': 'http://localhost/records/1'},
            'ref_pub': {'$ref': 'http://localhost/records/3'},
            'title': 'rec2'
        }

        current_search_client.indices.refresh()
        current_search_client.indices.flush()

        es_published2 = RecordsSearch(index='records-record-v1.0.0').\
            get_record(published2_pid.object_uuid).execute()
        assert len(es_published2.hits) == 1
        es_published2 = es_published2.hits[0].to_dict()
        es_published2.pop('_created')
        es_published2.pop('_updated')
        assert es_published2 == {
            '$schema': 'https://localhost/schemas/records/record-v1.0.0.json',
            'id': '2',
            'ref': {'published': '1'},
            'ref_pub': {'published': '3'},
            'title': 'rec2'}

        es_draft2 = RecordsSearch(index='draft-records-record-v1.0.0').\
            get_record(draft2_pid.object_uuid).execute()
        assert len(es_draft2.hits) == 0
Пример #11
0
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