From 3df8f941e60e106c67f3f7a4a41512692b162763 Mon Sep 17 00:00:00 2001 From: Christoph Ladurner Date: Thu, 31 Oct 2024 21:38:18 +0100 Subject: [PATCH] refactor: use session.get where possible * recomended sqlalchemy syntax for sqlalchemy >= 2.0 --- invenio_banners/records/models.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/invenio_banners/records/models.py b/invenio_banners/records/models.py index fdae11a..05baa65 100644 --- a/invenio_banners/records/models.py +++ b/invenio_banners/records/models.py @@ -14,7 +14,6 @@ from flask import current_app from invenio_db import db from sqlalchemy import or_ -from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.sql import text from sqlalchemy_utils.models import Timestamp @@ -69,6 +68,9 @@ def create(cls, data): def update(cls, data, id): """Update an existing banner.""" with db.session.begin_nested(): + # NOTE: + # with db.session.get(cls, id) the model itself would be + # returned and this classmethod would be called db.session.query(cls).filter_by(id=id).update(data) db.session.commit() @@ -76,10 +78,10 @@ def update(cls, data, id): @classmethod def get(cls, id): """Get banner by its id.""" - try: - return db.session.query(cls).filter_by(id=id).one() - except NoResultFound: - raise BannerNotExistsError(id) + if banner := db.session.get(cls, id): + return banner + + raise BannerNotExistsError(id) @classmethod def delete(cls, banner):