Skip to content

Commit

Permalink
versions: set the next index to the max index of all record versions
Browse files Browse the repository at this point in the history
  • Loading branch information
zzacharo committed Oct 9, 2023
1 parent daef7f0 commit a64f7e9
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion invenio_drafts_resources/records/systemfields/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ def parent_id(self):
"""Get versions state management model."""
return self._record.model.parent_id

@property
def record_model_cls(self):
"""Get model cls of the record/draft."""
return self._record.model_cls

@property
def index(self):
"""Get the version index of the record/draft."""
Expand Down Expand Up @@ -94,7 +99,16 @@ def is_latest_draft(self):
@property
def next_index(self):
"""Get the next parent index."""
return self.latest_index + 1 if self.latest_index is not None else 1
latest_index_by_parent = None
with db.session.no_autoflush:
rec_model = (
self.record_model_cls.query.filter_by(parent_id=self.parent_id)
.order_by(self.record_model_cls.index.desc())
.first()
)
if rec_model:
latest_index_by_parent = rec_model.index
return latest_index_by_parent + 1 if latest_index_by_parent is not None else 1

#
# State management methods
Expand Down

0 comments on commit a64f7e9

Please sign in to comment.