From 9542bb8613e4c84455560fd2dc759aa5bfad7763 Mon Sep 17 00:00:00 2001 From: anikachurilova Date: Thu, 9 Nov 2023 10:09:14 +0100 Subject: [PATCH] versions: calculate record's active versions * closes https://github.com/zenodo/rdm-project/issues/515 --- invenio_drafts_resources/records/api.py | 25 +++++++++++++++++++ .../records/systemfields/versions.py | 18 ++++++++++++- .../services/records/schema.py | 1 + 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/invenio_drafts_resources/records/api.py b/invenio_drafts_resources/records/api.py index c5d6bbf2..e000c72d 100644 --- a/invenio_drafts_resources/records/api.py +++ b/invenio_drafts_resources/records/api.py @@ -27,6 +27,9 @@ from invenio_db import db from invenio_pidstore.models import PIDStatus from invenio_pidstore.providers.recordid_v2 import RecordIdProviderV2 +from invenio_rdm_records.records.systemfields.deletion_status import ( + RecordDeletionStatusEnum, +) from invenio_records.systemfields import ModelField from invenio_records_resources.records import Record as RecordBase from invenio_records_resources.records.systemfields import PIDField, PIDStatusCheckField @@ -90,6 +93,14 @@ class Record(RecordBase): #: Version relationship versions = VersionsField(create=True, set_latest=True) + @property + def versions_count(self): + """Get number of record's active versions""" + records = list( + self.get_record_versions(parent_id=self.parent.id, include_deleted=False) + ) + return len(records) + @classmethod def get_records_by_parent(cls, parent, with_deleted=True, ids_only=False): """Get all sibling records for the specified parent record.""" @@ -106,6 +117,20 @@ def get_records_by_parent(cls, parent, with_deleted=True, ids_only=False): for rec_model in rec_models ) + @classmethod + def get_record_versions(cls, parent_id, include_deleted=True): + """Get record's versions.""" + from invenio_rdm_records.records.models import RDMRecordMetadata + + with db.session.no_autoflush: + rec_models = cls.model_cls.query.filter_by(parent_id=parent_id) + if not include_deleted and cls.model_cls is RDMRecordMetadata: + rec_models = rec_models.filter_by( + deletion_status=RecordDeletionStatusEnum.PUBLISHED.value + ) + + return (rec_model.id for rec_model in rec_models) + @classmethod def get_latest_by_parent(cls, parent, id_only=False): """Get the latest record for the specified parent record. diff --git a/invenio_drafts_resources/records/systemfields/versions.py b/invenio_drafts_resources/records/systemfields/versions.py index d0b9f835..1ca81cbb 100644 --- a/invenio_drafts_resources/records/systemfields/versions.py +++ b/invenio_drafts_resources/records/systemfields/versions.py @@ -29,6 +29,7 @@ def __init__(self, record, dump=None): """Initialize the versions manager.""" self._record = record self._state = None + self.versions_count = None if dump is not None: self.load(dump) @@ -88,6 +89,18 @@ def is_latest(self): """Check if the record/draft id is the latest published record id.""" return self.latest_id == self._record.id + @property + def versions_count(self): + """Get number of record's active versions.""" + if self._versions_count is None: + self._versions_count = self._record.versions_count + return self._versions_count + + @versions_count.setter + def versions_count(self, value): + """Set number of record's active versions.""" + self._versions_count = value + @property def is_latest_draft(self): """Check if the record/draft id is the latest draft id.""" @@ -153,6 +166,7 @@ def dump(self): next_draft_id=self.next_draft_id, is_latest=self.is_latest, is_latest_draft=self.is_latest_draft, + versions_count=self.versions_count, index=self.index, ) @@ -166,6 +180,7 @@ def load(self, dump): ) if self.index != dump["index"]: self._record.model.index = dump["index"] + self.versions_count = dump.get("versions_count", None) or dump["index"] def __repr__(self): """Return repr(self).""" @@ -173,7 +188,8 @@ def __repr__(self): f"<{type(self).__name__} (parent_id: {self.parent_id}, " f"index: {self.index}, latest_id: {self.latest_id}, " f"latest_index: {self.latest_index}, " - f"next_draft_id: {self.next_draft_id})>" + f"next_draft_id: {self.next_draft_id}, " + f"versions_count: {self.versions_count})>" ) diff --git a/invenio_drafts_resources/services/records/schema.py b/invenio_drafts_resources/services/records/schema.py index 94f8b06e..8469a4a3 100644 --- a/invenio_drafts_resources/services/records/schema.py +++ b/invenio_drafts_resources/services/records/schema.py @@ -19,6 +19,7 @@ class VersionsSchema(Schema): is_latest = fields.Boolean() is_latest_draft = fields.Boolean() index = fields.Integer() + versions_count = fields.Integer() class ParentSchema(Schema):