diff --git a/invenio_drafts_resources/records/api.py b/invenio_drafts_resources/records/api.py index 2694008..c5d6bbf 100644 --- a/invenio_drafts_resources/records/api.py +++ b/invenio_drafts_resources/records/api.py @@ -24,6 +24,7 @@ import uuid from datetime import datetime, timedelta +from invenio_db import db from invenio_pidstore.models import PIDStatus from invenio_pidstore.providers.recordid_v2 import RecordIdProviderV2 from invenio_records.systemfields import ModelField @@ -92,17 +93,18 @@ class Record(RecordBase): @classmethod def get_records_by_parent(cls, parent, with_deleted=True, ids_only=False): """Get all sibling records for the specified parent record.""" - rec_models = cls.model_cls.query.filter_by(parent_id=parent.id) - if not with_deleted: - rec_models = rec_models.filter_by(is_deleted=False) - - if ids_only: - return (rec_model.id for rec_model in rec_models) - else: - return ( - cls(rec_model.data, model=rec_model, parent=parent) - for rec_model in rec_models - ) + with db.session.no_autoflush: + rec_models = cls.model_cls.query.filter_by(parent_id=parent.id) + if not with_deleted: + rec_models = rec_models.filter_by(is_deleted=False) + + if ids_only: + return (rec_model.id for rec_model in rec_models) + else: + return ( + cls(rec_model.data, model=rec_model, parent=parent) + for rec_model in rec_models + ) @classmethod def get_latest_by_parent(cls, parent, id_only=False): @@ -110,18 +112,19 @@ def get_latest_by_parent(cls, parent, id_only=False): It might return None if there is no latest published version yet. """ - version = cls.versions_model_cls.query.filter_by( - parent_id=parent.id - ).one_or_none() - has_latest = version and version.latest_id - if not has_latest: - return None - - rec_model = cls.model_cls.query.filter_by(id=version.latest_id).one() - if id_only: - return rec_model.id - else: - return cls(rec_model.data, model=rec_model, parent=parent) + with db.session.no_autoflush: + version = cls.versions_model_cls.query.filter_by( + parent_id=parent.id + ).one_or_none() + has_latest = version and version.latest_id + if not has_latest: + return None + + rec_model = cls.model_cls.query.filter_by(id=version.latest_id).one() + if id_only: + return rec_model.id + else: + return cls(rec_model.data, model=rec_model, parent=parent) @classmethod def publish(cls, draft):