Skip to content

Commit

Permalink
versions: calculate record's active versions
Browse files Browse the repository at this point in the history
  • Loading branch information
anikachurilova committed Aug 14, 2024
1 parent a5bb49b commit 9542bb8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
25 changes: 25 additions & 0 deletions invenio_drafts_resources/records/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand All @@ -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.
Expand Down
18 changes: 17 additions & 1 deletion invenio_drafts_resources/records/systemfields/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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,
)

Expand All @@ -166,14 +180,16 @@ 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)."""
return (
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})>"
)


Expand Down
1 change: 1 addition & 0 deletions invenio_drafts_resources/services/records/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 9542bb8

Please sign in to comment.