From a64f7e9dc176b7726295608a22c2bd00563ba9a1 Mon Sep 17 00:00:00 2001 From: Zacharias Zacharodimos Date: Fri, 6 Oct 2023 18:20:22 +0200 Subject: [PATCH] versions: set the next index to the max index of all record versions --- .../records/systemfields/versions.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/invenio_drafts_resources/records/systemfields/versions.py b/invenio_drafts_resources/records/systemfields/versions.py index 9e0a079..d0b9f83 100644 --- a/invenio_drafts_resources/records/systemfields/versions.py +++ b/invenio_drafts_resources/records/systemfields/versions.py @@ -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.""" @@ -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