Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

versions: calculate record's active versions #274

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we reuse get_records_by_parent with with_deleted=False, ids_only=True?

This would fix the tests which are failing due to the import from invenio_rdm_records which we cannot do in invenio-drafts-resources.
Not sure what is the difference between is_deleted and deletion_status in the model.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After checking, I can confirm that we need to use the deletion_status and not is_deleted=False as is_deleted is checking for empty json (see here)

You are right tho, that we can not import invenio_rdm_records... I could hardcode the value "P" of the RecordDeletionStatusEnum.PUBLISHED..

)
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
Loading