Skip to content

Commit

Permalink
service: fix restore/delete of specific record version
Browse files Browse the repository at this point in the history
  • Loading branch information
zzacharo committed Oct 9, 2023
1 parent ac008d7 commit 485c2a9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
36 changes: 36 additions & 0 deletions invenio_rdm_records/records/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from flask import g
from invenio_communities.records.records.systemfields import CommunitiesField
from invenio_db import db
from invenio_drafts_resources.records import Draft, Record
from invenio_drafts_resources.records.api import ParentRecord as ParentRecordBase
from invenio_drafts_resources.services.records.components.media_files import (
Expand Down Expand Up @@ -40,6 +41,10 @@
from invenio_vocabularies.records.api import Vocabulary
from invenio_vocabularies.records.systemfields.relations import CustomFieldsRelation

from invenio_rdm_records.records.systemfields.deletion_status import (
RecordDeletionStatusEnum,
)

from . import models
from .dumpers import (
EDTFDumperExt,
Expand Down Expand Up @@ -445,6 +450,37 @@ class RDMRecord(CommonFieldsMixin, Record):

tombstone = TombstoneField()

@classmethod
def next_latest_published_record_by_parent(cls, parent):
"""Get the next latest published record.
This method gives back the next published latest record by parent or None if all
records are deleted i.e `record.deletion_status != 'P'`.
:param parent: parent record.
:param excluded_latest: latest record to exclude find next published version
"""

with db.session.no_autoflush:
rec_model_query = (
cls.model_cls.query.filter_by(parent_id=parent.id)
.filter(
cls.model_cls.deletion_status
== RecordDeletionStatusEnum.PUBLISHED.value
)
.order_by(cls.model_cls.index.desc())
)
current_latest_id = cls.get_latest_by_parent(parent, id_only=True)
if current_latest_id:
rec_model_query.filter(cls.model_cls.id != current_latest_id)

rec_model = rec_model_query.first()
return (
cls(rec_model.data, model=rec_model, parent=parent)
if rec_model
else None
)


RDMFileRecord.record_cls = RDMRecord

Expand Down
29 changes: 29 additions & 0 deletions invenio_rdm_records/services/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,24 @@ def delete_record(
"delete_record", identity, data=data, record=record, uow=uow
)

new_record_latest_version = None
if record.versions.is_latest == True:
# set latest to the previous non deleted record
new_record_latest_version = (
self.record_cls.next_latest_published_record_by_parent(record.parent)
)
if new_record_latest_version:
new_record_latest_version.versions.set_latest()

# Commit and reindex record
uow.register(RecordCommitOp(record, indexer=self.indexer))

# Commit and reindex new latest record
if new_record_latest_version:
uow.register(
RecordCommitOp(new_record_latest_version, indexer=self.indexer)
)

return self.result_item(
self,
identity,
Expand Down Expand Up @@ -258,9 +273,23 @@ def restore_record(self, identity, id_, expand=False, uow=None):
# Run components
self.run_components("restore_record", identity, record=record, uow=uow)

# set latest to the previous non deleted record
latest_record_version = self.record_cls.get_latest_by_parent(record.parent)

if not latest_record_version:
# if all records were deleted then make the restored record latest
record.versions.set_latest()
elif record.versions.index > latest_record_version.versions.index:
# set current restored record as latest
record.versions.set_latest()

# Commit and reindex record
uow.register(RecordCommitOp(record, indexer=self.indexer))

# commit and reindex the old latest record
if latest_record_version and record.id != latest_record_version.id:
uow.register(RecordCommitOp(latest_record_version, indexer=self.indexer))

return self.result_item(
self,
identity,
Expand Down

0 comments on commit 485c2a9

Please sign in to comment.